FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
qsvdec_h264.c
Go to the documentation of this file.
1 /*
2  * Intel MediaSDK QSV based H.264 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 "qsv_internal.h"
37 #include "qsvdec.h"
38 #include "qsv.h"
39 
40 typedef struct QSVH264Context {
41  AVClass *class;
43 
44  // the internal parser and codec context for parsing the data
48 
49  // the filter for converting to Annex B
51 
53 
58 
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  if (s->filtered_data != s->input_ref.data)
69  s->filtered_data = NULL;
71 }
72 
74 {
75  QSVH264Context *s = avctx->priv_data;
76 
78 
80 
82 
86 
87  return 0;
88 }
89 
91 {
92  QSVH264Context *s = avctx->priv_data;
93  int ret;
94 
96 
97  s->packet_fifo = av_fifo_alloc(sizeof(AVPacket));
98  if (!s->packet_fifo) {
99  ret = AVERROR(ENOMEM);
100  goto fail;
101  }
102 
103  s->bsf = av_bitstream_filter_init("h264_mp4toannexb");
104  if (!s->bsf) {
105  ret = AVERROR(ENOMEM);
106  goto fail;
107  }
108 
110  if (!s->avctx_internal) {
111  ret = AVERROR(ENOMEM);
112  goto fail;
113  }
114 
115  if (avctx->extradata) {
117  if (!s->avctx_internal->extradata) {
118  ret = AVERROR(ENOMEM);
119  goto fail;
120  }
121  memcpy(s->avctx_internal->extradata, avctx->extradata,
122  avctx->extradata_size);
124  }
125 
127  if (!s->parser) {
128  ret = AVERROR(ENOMEM);
129  goto fail;
130  }
132 
133  s->qsv.iopattern = MFX_IOPATTERN_OUT_SYSTEM_MEMORY;
134 
135  return 0;
136 fail:
137  qsv_decode_close(avctx);
138  return ret;
139 }
140 
142  int *got_frame, AVPacket *pkt)
143 {
144  QSVH264Context *s = avctx->priv_data;
145  uint8_t *dummy_data;
146  int dummy_size;
147  int ret;
148 
149  /* we assume the packets are already split properly and want
150  * just the codec parameters here */
152  &dummy_data, &dummy_size,
153  pkt->data, pkt->size, pkt->pts, pkt->dts,
154  pkt->pos);
155 
156  /* TODO: flush delayed frames on reinit */
157  if (s->parser->format != s->orig_pix_fmt ||
158  s->parser->coded_width != avctx->coded_width ||
159  s->parser->coded_height != avctx->coded_height) {
160  mfxSession session = NULL;
161 
162  enum AVPixelFormat pix_fmts[3] = { AV_PIX_FMT_QSV,
164  AV_PIX_FMT_NONE };
165  enum AVPixelFormat qsv_format;
166 
167  qsv_format = ff_qsv_map_pixfmt(s->parser->format);
168  if (qsv_format < 0) {
169  av_log(avctx, AV_LOG_ERROR,
170  "Only 8-bit YUV420 streams are supported.\n");
171  ret = AVERROR(ENOSYS);
172  goto reinit_fail;
173  }
174 
175  s->orig_pix_fmt = s->parser->format;
176  avctx->pix_fmt = pix_fmts[1] = qsv_format;
177  avctx->width = s->parser->width;
178  avctx->height = s->parser->height;
179  avctx->coded_width = s->parser->coded_width;
180  avctx->coded_height = s->parser->coded_height;
181  avctx->level = s->avctx_internal->level;
182  avctx->profile = s->avctx_internal->profile;
183 
184  ret = ff_get_format(avctx, pix_fmts);
185  if (ret < 0)
186  goto reinit_fail;
187 
188  avctx->pix_fmt = ret;
189 
190  if (avctx->hwaccel_context) {
191  AVQSVContext *user_ctx = avctx->hwaccel_context;
192  session = user_ctx->session;
193  s->qsv.iopattern = user_ctx->iopattern;
194  s->qsv.ext_buffers = user_ctx->ext_buffers;
195  s->qsv.nb_ext_buffers = user_ctx->nb_ext_buffers;
196  }
197 
198  ret = ff_qsv_decode_init(avctx, &s->qsv, session);
199  if (ret < 0)
200  goto reinit_fail;
201  }
202 
203  return ff_qsv_decode(avctx, &s->qsv, frame, got_frame, &s->pkt_filtered);
204 
205 reinit_fail:
206  s->orig_pix_fmt = s->parser->format = avctx->pix_fmt = AV_PIX_FMT_NONE;
207  return ret;
208 }
209 
210 static int qsv_decode_frame(AVCodecContext *avctx, void *data,
211  int *got_frame, AVPacket *avpkt)
212 {
213  QSVH264Context *s = avctx->priv_data;
214  AVFrame *frame = data;
215  int ret;
216 
217  /* buffer the input packet */
218  if (avpkt->size) {
219  AVPacket input_ref = { 0 };
220 
221  if (av_fifo_space(s->packet_fifo) < sizeof(input_ref)) {
222  ret = av_fifo_realloc2(s->packet_fifo,
223  av_fifo_size(s->packet_fifo) + sizeof(input_ref));
224  if (ret < 0)
225  return ret;
226  }
227 
228  ret = av_packet_ref(&input_ref, avpkt);
229  if (ret < 0)
230  return ret;
231  av_fifo_generic_write(s->packet_fifo, &input_ref, sizeof(input_ref), NULL);
232  }
233 
234  /* process buffered data */
235  while (!*got_frame) {
236  /* prepare the input data -- convert to Annex B if needed */
237  if (s->pkt_filtered.size <= 0) {
238  int size;
239 
240  /* no more data */
241  if (av_fifo_size(s->packet_fifo) < sizeof(AVPacket))
242  return avpkt->size ? avpkt->size : ff_qsv_decode(avctx, &s->qsv, frame, got_frame, avpkt);
243 
244  if (s->filtered_data != s->input_ref.data)
245  av_freep(&s->filtered_data);
246  s->filtered_data = NULL;
248 
250  ret = av_bitstream_filter_filter(s->bsf, avctx, NULL,
251  &s->filtered_data, &size,
252  s->input_ref.data, s->input_ref.size, 0);
253  if (ret < 0) {
254  s->filtered_data = s->input_ref.data;
255  size = s->input_ref.size;
256  }
257  s->pkt_filtered = s->input_ref;
259  s->pkt_filtered.size = size;
260  }
261 
262  ret = qsv_process_data(avctx, frame, got_frame, &s->pkt_filtered);
263  if (ret < 0)
264  return ret;
265 
266  s->pkt_filtered.size -= ret;
267  s->pkt_filtered.data += ret;
268  }
269 
270  return avpkt->size;
271 }
272 
273 static void qsv_decode_flush(AVCodecContext *avctx)
274 {
275  QSVH264Context *s = avctx->priv_data;
276 
279 }
280 
282  .name = "h264_qsv",
283  .type = AVMEDIA_TYPE_VIDEO,
284  .id = AV_CODEC_ID_H264,
285  .pix_fmt = AV_PIX_FMT_QSV,
286 };
287 
288 #define OFFSET(x) offsetof(QSVH264Context, x)
289 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
290 static const AVOption options[] = {
291  { "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 },
292  { NULL },
293 };
294 
295 static const AVClass class = {
296  .class_name = "h264_qsv",
297  .item_name = av_default_item_name,
298  .option = options,
300 };
301 
303  .name = "h264_qsv",
304  .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (Intel Quick Sync Video acceleration)"),
305  .priv_data_size = sizeof(QSVH264Context),
307  .id = AV_CODEC_ID_H264,
311  .close = qsv_decode_close,
312  .capabilities = CODEC_CAP_DELAY,
313  .priv_class = &class,
314 };
#define NULL
Definition: coverity.c:32
#define OFFSET(x)
Definition: qsvdec_h264.c:288
int iopattern
Definition: qsvdec.h:52
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:205
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
AVOption.
Definition: opt.h:255
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
AVCodec ff_h264_qsv_decoder
Definition: qsvdec_h264.c:302
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:1424
int ff_qsv_decode_init(AVCodecContext *avctx, QSVContext *q, mfxSession session)
Definition: qsvdec.c:71
#define LIBAVUTIL_VERSION_INT
Definition: version.h:62
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1187
#define VD
Definition: qsvdec_h264.c:289
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int width
Dimensions of the decoded video intended for presentation.
Definition: avcodec.h:4405
AVPacket pkt_filtered
Definition: qsvdec_h264.c:55
int size
Definition: avcodec.h:1163
int coded_width
Dimensions of the coded video.
Definition: avcodec.h:4411
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1444
mfxExtBuffer ** ext_buffers
Definition: qsvdec.h:54
enum AVPixelFormat orig_pix_fmt
Definition: qsvdec_h264.c:47
static AVPacket pkt
int profile
profile
Definition: avcodec.h:2835
AVCodec.
Definition: avcodec.h:3181
int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size, int(*func)(void *, void *, int))
Feed data from a user-supplied callback to an AVFifoBuffer.
Definition: fifo.c:122
static const AVOption options[]
Definition: qsvdec_h264.c:290
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
uint8_t
#define av_cold
Definition: attributes.h:74
AVBitStreamFilterContext * bsf
Definition: qsvdec_h264.c:50
void * hwaccel_context
Hardware accelerator context.
Definition: avcodec.h:2656
AVOptions.
int ff_qsv_decode_close(QSVContext *q)
Definition: qsvdec.c:275
int av_fifo_space(const AVFifoBuffer *f)
Return the amount of space in bytes in the AVFifoBuffer, that is the amount of data you can write int...
Definition: fifo.c:82
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1355
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1162
void av_fifo_free(AVFifoBuffer *f)
Free an AVFifoBuffer.
Definition: fifo.c:55
ptrdiff_t size
Definition: opengl_enc.c:101
#define av_log(a,...)
int av_packet_ref(AVPacket *dst, const AVPacket *src)
Setup a new reference to the data described by a given packet.
Definition: avpacket.c:559
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define 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:824
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:4281
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
uint8_t * filtered_data
Definition: qsvdec_h264.c:56
int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size, void(*func)(void *, void *, int))
Feed data from an AVFifoBuffer to a user-supplied callback.
Definition: fifo.c:150
void av_bitstream_filter_close(AVBitStreamFilterContext *bsf)
Release bitstream filter context.
const char * name
Name of the codec implementation.
Definition: avcodec.h:3188
int iopattern
Definition: qsv.h:28
Libavcodec external API header.
int nb_ext_buffers
Definition: qsv.h:31
AVHWAccel ff_h264_qsv_hwaccel
Definition: qsvdec_h264.c:281
#define ASYNC_DEPTH_DEFAULT
Definition: qsv_internal.h:31
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:630
const char * name
Name of the hardware accelerated codec.
Definition: avcodec.h:3294
QSVContext qsv
Definition: qsvdec_h264.c:42
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition: options.c:147
int av_parser_parse2(AVCodecParserContext *s, AVCodecContext *avctx, uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size, int64_t pts, int64_t dts, int64_t pos)
Parse a packet.
Definition: parser.c:131
ret
Definition: avfilter.c:974
int width
picture width / height.
Definition: avcodec.h:1414
AVBitStreamFilterContext * av_bitstream_filter_init(const char *name)
Create and initialize a bitstream filter context given a bitstream filter name.
AVCodecContext * avctx_internal
Definition: qsvdec_h264.c:46
void av_parser_close(AVCodecParserContext *s)
Definition: parser.c:221
int level
level
Definition: avcodec.h:2925
static void flush(AVCodecContext *avctx)
Definition: aacdec.c:514
int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
Select the (possibly hardware accelerated) pixel format.
Definition: utils.c:1198
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.
AVCodecParserContext * av_parser_init(int codec_id)
Definition: parser.c:50
void avcodec_free_context(AVCodecContext **avctx)
Free the codec context and everything associated with it and write NULL to the provided pointer...
Definition: options.c:162
int av_fifo_size(const AVFifoBuffer *f)
Return the amount of data in bytes in the AVFifoBuffer, that is the amount of data you can read from ...
Definition: fifo.c:77
mfxExtBuffer ** ext_buffers
Definition: qsv.h:30
int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size)
Resize an AVFifoBuffer.
Definition: fifo.c:87
main external API structure.
Definition: avcodec.h:1241
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:550
AVFifoBuffer * packet_fifo
Definition: qsvdec_h264.c:52
AVPacket input_ref
Definition: qsvdec_h264.c:54
a very simple circular buffer FIFO implementation
GLint GLenum type
Definition: opengl_enc.c:105
int extradata_size
Definition: avcodec.h:1356
int coded_height
Definition: avcodec.h:1424
AVCodecParserContext * parser
Definition: qsvdec_h264.c:45
Describe the class of an AVClass context structure.
Definition: log.h:67
static int qsv_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: qsvdec_h264.c:210
HW acceleration through QSV, data[3] contains a pointer to the mfxFrameSurface1 structure.
Definition: pixfmt.h:249
static av_cold int qsv_decode_close(AVCodecContext *avctx)
Definition: qsvdec_h264.c:73
static void qsv_decode_flush(AVCodecContext *avctx)
Definition: qsvdec_h264.c:273
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
Definition: ccaption_dec.c:522
static void qsv_clear_buffers(QSVH264Context *s)
Definition: qsvdec_h264.c:59
common internal api header.
common internal and external API header
int ff_qsv_map_pixfmt(enum AVPixelFormat format)
Definition: qsvdec.c:40
void * priv_data
Definition: avcodec.h:1283
AVFifoBuffer * av_fifo_alloc(unsigned int size)
Initialize an AVFifoBuffer.
Definition: fifo.c:43
static av_cold int qsv_decode_init(AVCodecContext *avctx)
Definition: qsvdec_h264.c:90
static int qsv_process_data(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
Definition: qsvdec_h264.c:141
int format
The format of the coded data, corresponds to enum AVPixelFormat for video and for enum AVSampleFormat...
Definition: avcodec.h:4422
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1161
#define av_freep(p)
AVPixelFormat
Pixel format.
Definition: pixfmt.h:61
This structure stores compressed data.
Definition: avcodec.h:1139
mfxSession session
Definition: qsv.h:27
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:250
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1155
int nb_ext_buffers
Definition: qsvdec.h:55