FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
qsvdec_mpeg2.c
Go to the documentation of this file.
1 /*
2  * Intel MediaSDK QSV based MPEG-2 decoder
3  *
4  * copyright (c) 2015 Anton Khirnov
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 
24 #include <stdint.h>
25 #include <string.h>
26 
27 #include <mfx/mfxvideo.h>
28 
29 #include "libavutil/common.h"
30 #include "libavutil/fifo.h"
31 #include "libavutil/opt.h"
32 
33 #include "avcodec.h"
34 #include "internal.h"
35 #include "qsv_internal.h"
36 #include "qsvdec.h"
37 #include "qsv.h"
38 
39 typedef struct QSVMPEG2Context {
40  AVClass *class;
42 
44 
47 
49 {
50  AVPacket pkt;
51  while (av_fifo_size(s->packet_fifo) >= sizeof(pkt)) {
52  av_fifo_generic_read(s->packet_fifo, &pkt, sizeof(pkt), NULL);
53  av_packet_unref(&pkt);
54  }
55 
57 }
58 
60 {
61  QSVMPEG2Context *s = avctx->priv_data;
62 
64 
66 
68 
69  return 0;
70 }
71 
73 {
74  QSVMPEG2Context *s = avctx->priv_data;
75  int ret;
76 
77  s->packet_fifo = av_fifo_alloc(sizeof(AVPacket));
78  if (!s->packet_fifo) {
79  ret = AVERROR(ENOMEM);
80  goto fail;
81  }
82 
83  return 0;
84 fail:
85  qsv_decode_close(avctx);
86  return ret;
87 }
88 
89 static int qsv_decode_frame(AVCodecContext *avctx, void *data,
90  int *got_frame, AVPacket *avpkt)
91 {
92  QSVMPEG2Context *s = avctx->priv_data;
93  AVFrame *frame = data;
94  int ret;
95 
96  /* buffer the input packet */
97  if (avpkt->size) {
98  AVPacket input_ref = { 0 };
99 
100  if (av_fifo_space(s->packet_fifo) < sizeof(input_ref)) {
101  ret = av_fifo_realloc2(s->packet_fifo,
102  av_fifo_size(s->packet_fifo) + sizeof(input_ref));
103  if (ret < 0)
104  return ret;
105  }
106 
107  ret = av_packet_ref(&input_ref, avpkt);
108  if (ret < 0)
109  return ret;
110  av_fifo_generic_write(s->packet_fifo, &input_ref, sizeof(input_ref), NULL);
111  }
112 
113  /* process buffered data */
114  while (!*got_frame) {
115  if (s->input_ref.size <= 0) {
116  /* no more data */
117  if (av_fifo_size(s->packet_fifo) < sizeof(AVPacket))
118  return avpkt->size ? avpkt->size : ff_qsv_process_data(avctx, &s->qsv, frame, got_frame, avpkt);
119 
122  }
123 
124  ret = ff_qsv_process_data(avctx, &s->qsv, frame, got_frame, &s->input_ref);
125  if (ret < 0)
126  return ret;
127 
128  s->input_ref.size -= ret;
129  s->input_ref.data += ret;
130  }
131 
132  return avpkt->size;
133 }
134 
135 static void qsv_decode_flush(AVCodecContext *avctx)
136 {
137  QSVMPEG2Context *s = avctx->priv_data;
138 
140  ff_qsv_decode_flush(avctx, &s->qsv);
141 }
142 
144  .name = "mpeg2_qsv",
145  .type = AVMEDIA_TYPE_VIDEO,
147  .pix_fmt = AV_PIX_FMT_QSV,
148 };
149 
150 #define OFFSET(x) offsetof(QSVMPEG2Context, x)
151 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
152 static const AVOption options[] = {
153  { "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 },
154  { NULL },
155 };
156 
157 static const AVClass class = {
158  .class_name = "mpeg2_qsv",
159  .item_name = av_default_item_name,
160  .option = options,
162 };
163 
165  .name = "mpeg2_qsv",
166  .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video (Intel Quick Sync Video acceleration)"),
167  .priv_data_size = sizeof(QSVMPEG2Context),
173  .close = qsv_decode_close,
175  .priv_class = &class,
176  .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_NV12,
178  AV_PIX_FMT_NONE },
179 };
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
This structure describes decoded (raw) audio or video data.
Definition: frame.h:187
AVOption.
Definition: opt.h:246
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
static void flush(AVCodecContext *avctx)
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int size
Definition: avcodec.h:1648
AVPacket input_ref
Definition: qsvdec_mpeg2.c:45
static AVPacket pkt
AVCodec.
Definition: avcodec.h:3671
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
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:1019
#define av_cold
Definition: attributes.h:82
AVOptions.
int ff_qsv_decode_close(QSVContext *q)
Definition: qsvdec.c:402
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
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1647
void av_fifo_free(AVFifoBuffer *f)
Free an AVFifoBuffer.
Definition: fifo.c:55
static void qsv_decode_flush(AVCodecContext *avctx)
Definition: qsvdec_mpeg2.c:135
int av_packet_ref(AVPacket *dst, const AVPacket *src)
Setup a new reference to the data described by a given packet.
Definition: avpacket.c:584
static const AVOption options[]
Definition: qsvdec_mpeg2.c:152
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
AVCodec ff_mpeg2_qsv_decoder
Definition: qsvdec_mpeg2.c:164
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:180
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:213
QSVContext qsv
Definition: qsvdec_mpeg2.c:41
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
#define VD
Definition: qsvdec_mpeg2.c:151
const char * name
Name of the codec implementation.
Definition: avcodec.h:3678
#define fail()
Definition: checkasm.h:89
AVHWAccel ff_mpeg2_qsv_hwaccel
Definition: qsvdec_mpeg2.c:143
void ff_qsv_decode_flush(AVCodecContext *avctx, QSVContext *q)
Definition: qsvdec.c:517
#define ASYNC_DEPTH_DEFAULT
Definition: qsv_internal.h:33
static av_cold int qsv_decode_init(AVCodecContext *avctx)
Definition: qsvdec_mpeg2.c:72
const char * name
Name of the hardware accelerated codec.
Definition: avcodec.h:3797
AVFifoBuffer * packet_fifo
Definition: qsvdec_mpeg2.c:43
static av_cold int qsv_decode_close(AVCodecContext *avctx)
Definition: qsvdec_mpeg2.c:59
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:219
Libavcodec external API header.
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
int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size)
Resize an AVFifoBuffer.
Definition: fifo.c:87
main external API structure.
Definition: avcodec.h:1722
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:575
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:236
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:262
common internal api header.
common internal and external API header
void * priv_data
Definition: avcodec.h:1764
#define OFFSET(x)
Definition: qsvdec_mpeg2.c:150
AVFifoBuffer * av_fifo_alloc(unsigned int size)
Initialize an AVFifoBuffer.
Definition: fifo.c:43
static int qsv_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: qsvdec_mpeg2.c:89
#define AV_CODEC_CAP_AVOID_PROBING
Decoder is not a preferred choice for probing.
Definition: avcodec.h:1083
static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
Definition: ffmpeg.c:2246
static void qsv_clear_buffers(QSVMPEG2Context *s)
Definition: qsvdec_mpeg2.c:48
int ff_qsv_process_data(AVCodecContext *avctx, QSVContext *q, AVFrame *frame, int *got_frame, AVPacket *pkt)
Definition: qsvdec.c:441
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
This structure stores compressed data.
Definition: avcodec.h:1624
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:994