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 "qsvdec.h"
37 
38 typedef struct QSVH264Context {
39  AVClass *class;
41 
42  // the filter for converting to Annex B
44 
46 
48 {
49  QSVH264Context *s = avctx->priv_data;
50 
52 
54 
55  return 0;
56 }
57 
59 {
60  QSVH264Context *s = avctx->priv_data;
61  int ret;
62 
63  s->bsf = av_bitstream_filter_init("h264_mp4toannexb");
64  if (!s->bsf) {
65  ret = AVERROR(ENOMEM);
66  goto fail;
67  }
68 
69  return 0;
70 fail:
71  qsv_decode_close(avctx);
72  return ret;
73 }
74 
75 static int qsv_decode_frame(AVCodecContext *avctx, void *data,
76  int *got_frame, AVPacket *avpkt)
77 {
78  QSVH264Context *s = avctx->priv_data;
79  AVFrame *frame = data;
80  int ret;
81  uint8_t *p_filtered = NULL;
82  int n_filtered = NULL;
83  AVPacket pkt_filtered = { 0 };
84 
85  if (avpkt->size) {
86  if (avpkt->size > 3 && !avpkt->data[0] &&
87  !avpkt->data[1] && !avpkt->data[2] && 1==avpkt->data[3]) {
88  /* we already have annex-b prefix */
89  return ff_qsv_decode(avctx, &s->qsv, frame, got_frame, avpkt);
90 
91  } else {
92  /* no annex-b prefix. try to restore: */
93  ret = av_bitstream_filter_filter(s->bsf, avctx, NULL,
94  &p_filtered, &n_filtered,
95  avpkt->data, avpkt->size, 0);
96  if (ret>=0) {
97  pkt_filtered.pts = avpkt->pts;
98  pkt_filtered.data = p_filtered;
99  pkt_filtered.size = n_filtered;
100 
101  ret = ff_qsv_decode(avctx, &s->qsv, frame, got_frame, &pkt_filtered);
102 
103  if (p_filtered != avpkt->data)
104  av_free(p_filtered);
105  return ret > 0 ? avpkt->size : ret;
106  }
107  }
108  }
109 
110  return ff_qsv_decode(avctx, &s->qsv, frame, got_frame, avpkt);
111 }
112 
113 static void qsv_decode_flush(AVCodecContext *avctx)
114 {
115 // QSVH264Context *s = avctx->priv_data;
116  /* TODO: flush qsv engine if necessary */
117 }
118 
120  .name = "h264_qsv",
121  .type = AVMEDIA_TYPE_VIDEO,
122  .id = AV_CODEC_ID_H264,
123  .pix_fmt = AV_PIX_FMT_QSV,
124 };
125 
126 #define OFFSET(x) offsetof(QSVH264Context, x)
127 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
128 static const AVOption options[] = {
129  { "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 },
130  { NULL },
131 };
132 
133 static const AVClass class = {
134  .class_name = "h264_qsv",
135  .item_name = av_default_item_name,
136  .option = options,
138 };
139 
141  .name = "h264_qsv",
142  .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (Intel Quick Sync Video acceleration)"),
143  .priv_data_size = sizeof(QSVH264Context),
145  .id = AV_CODEC_ID_H264,
149  .close = qsv_decode_close,
150  .capabilities = CODEC_CAP_DELAY,
151  .priv_class = &class,
152 };
#define NULL
Definition: coverity.c:32
#define OFFSET(x)
Definition: qsvdec_h264.c:126
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:268
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
static void flush(AVCodecContext *avctx)
AVCodec ff_h264_qsv_decoder
Definition: qsvdec_h264.c:140
#define LIBAVUTIL_VERSION_INT
Definition: version.h:62
#define VD
Definition: qsvdec_h264.c:127
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int size
Definition: avcodec.h:1188
AVCodec.
Definition: avcodec.h:3239
static const AVOption options[]
Definition: qsvdec_h264.c:128
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:43
AVOptions.
int ff_qsv_decode_close(QSVContext *q)
Definition: qsvdec.c:390
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1187
#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:829
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:175
void av_bitstream_filter_close(AVBitStreamFilterContext *bsf)
Release bitstream filter context.
const char * name
Name of the codec implementation.
Definition: avcodec.h:3246
Libavcodec external API header.
#define fail()
Definition: checkasm.h:55
AVHWAccel ff_h264_qsv_hwaccel
Definition: qsvdec_h264.c:119
#define ASYNC_DEPTH_DEFAULT
Definition: qsv_internal.h:46
const char * name
Name of the hardware accelerated codec.
Definition: avcodec.h:3352
QSVContext qsv
Definition: qsvdec_h264.c:40
ret
Definition: avfilter.c:974
AVBitStreamFilterContext * av_bitstream_filter_init(const char *name)
Create and initialize a bitstream filter context given a bitstream filter name.
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.
main external API structure.
Definition: avcodec.h:1266
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
static int qsv_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: qsvdec_h264.c:75
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:47
static void qsv_decode_flush(AVCodecContext *avctx)
Definition: qsvdec_h264.c:113
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
Definition: ccaption_dec.c:522
common internal api header.
common internal and external API header
void * priv_data
Definition: avcodec.h:1308
#define av_free(p)
static av_cold int qsv_decode_init(AVCodecContext *avctx)
Definition: qsvdec_h264.c:58
This structure stores compressed data.
Definition: avcodec.h:1164
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1180