~ppsspp/ppsspp/ppsspp-1.1.1

« back to all changes in this revision

Viewing changes to ffmpeg/libavcodec/qsvdec.c

  • Committer: Sérgio Benjamim
  • Date: 2015-10-17 01:37:55 UTC
  • Revision ID: sergio_br2@yahoo.com.br-20151017013755-avrlz2pt37kwt43x
PPSSPP 1.1.1 source.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Intel MediaSDK QSV codec-independent code
 
3
 *
 
4
 * copyright (c) 2013 Luca Barbato
 
5
 * copyright (c) 2015 Anton Khirnov <anton@khirnov.net>
 
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
#include <string.h>
 
25
#include <sys/types.h>
 
26
 
 
27
#include <mfx/mfxvideo.h>
 
28
 
 
29
#include "libavutil/common.h"
 
30
#include "libavutil/mem.h"
 
31
#include "libavutil/log.h"
 
32
#include "libavutil/pixfmt.h"
 
33
#include "libavutil/time.h"
 
34
 
 
35
#include "avcodec.h"
 
36
#include "internal.h"
 
37
#include "qsv_internal.h"
 
38
#include "qsvdec.h"
 
39
 
 
40
int ff_qsv_map_pixfmt(enum AVPixelFormat format)
 
41
{
 
42
    switch (format) {
 
43
    case AV_PIX_FMT_YUV420P:
 
44
    case AV_PIX_FMT_YUVJ420P:
 
45
        return AV_PIX_FMT_NV12;
 
46
    default:
 
47
        return AVERROR(ENOSYS);
 
48
    }
 
49
}
 
50
 
 
51
static int qsv_init_session(AVCodecContext *avctx, QSVContext *q, mfxSession session)
 
52
{
 
53
    if (!session) {
 
54
        if (!q->internal_session) {
 
55
            int ret = ff_qsv_init_internal_session(avctx, &q->internal_session);
 
56
            if (ret < 0)
 
57
                return ret;
 
58
        }
 
59
 
 
60
        q->session = q->internal_session;
 
61
    } else {
 
62
        q->session = session;
 
63
    }
 
64
 
 
65
    /* make sure the decoder is uninitialized */
 
66
    MFXVideoDECODE_Close(q->session);
 
67
 
 
68
    return 0;
 
69
}
 
70
 
 
71
int ff_qsv_decode_init(AVCodecContext *avctx, QSVContext *q, mfxSession session)
 
72
{
 
73
    mfxVideoParam param = { { 0 } };
 
74
    int ret;
 
75
 
 
76
    ret = qsv_init_session(avctx, q, session);
 
77
    if (ret < 0) {
 
78
        av_log(avctx, AV_LOG_ERROR, "Error initializing an MFX session\n");
 
79
        return ret;
 
80
    }
 
81
 
 
82
 
 
83
    ret = ff_qsv_codec_id_to_mfx(avctx->codec_id);
 
84
    if (ret < 0)
 
85
        return ret;
 
86
 
 
87
    param.mfx.CodecId      = ret;
 
88
    param.mfx.CodecProfile = avctx->profile;
 
89
    param.mfx.CodecLevel   = avctx->level;
 
90
 
 
91
    param.mfx.FrameInfo.BitDepthLuma   = 8;
 
92
    param.mfx.FrameInfo.BitDepthChroma = 8;
 
93
    param.mfx.FrameInfo.Shift          = 0;
 
94
    param.mfx.FrameInfo.FourCC         = MFX_FOURCC_NV12;
 
95
    param.mfx.FrameInfo.Width          = avctx->coded_width;
 
96
    param.mfx.FrameInfo.Height         = avctx->coded_height;
 
97
    param.mfx.FrameInfo.ChromaFormat   = MFX_CHROMAFORMAT_YUV420;
 
98
 
 
99
    param.IOPattern   = q->iopattern;
 
100
    param.AsyncDepth  = q->async_depth;
 
101
    param.ExtParam    = q->ext_buffers;
 
102
    param.NumExtParam = q->nb_ext_buffers;
 
103
 
 
104
    ret = MFXVideoDECODE_Init(q->session, &param);
 
105
    if (ret < 0) {
 
106
        av_log(avctx, AV_LOG_ERROR, "Error initializing the MFX video decoder\n");
 
107
        return ff_qsv_error(ret);
 
108
    }
 
109
 
 
110
    return 0;
 
111
}
 
112
 
 
113
static int alloc_frame(AVCodecContext *avctx, QSVFrame *frame)
 
114
{
 
115
    int ret;
 
116
 
 
117
    ret = ff_get_buffer(avctx, frame->frame, AV_GET_BUFFER_FLAG_REF);
 
118
    if (ret < 0)
 
119
        return ret;
 
120
 
 
121
    if (frame->frame->format == AV_PIX_FMT_QSV) {
 
122
        frame->surface = (mfxFrameSurface1*)frame->frame->data[3];
 
123
    } else {
 
124
        frame->surface_internal.Info.BitDepthLuma   = 8;
 
125
        frame->surface_internal.Info.BitDepthChroma = 8;
 
126
        frame->surface_internal.Info.FourCC         = MFX_FOURCC_NV12;
 
127
        frame->surface_internal.Info.Width          = avctx->coded_width;
 
128
        frame->surface_internal.Info.Height         = avctx->coded_height;
 
129
        frame->surface_internal.Info.ChromaFormat   = MFX_CHROMAFORMAT_YUV420;
 
130
 
 
131
        frame->surface_internal.Data.PitchLow = frame->frame->linesize[0];
 
132
        frame->surface_internal.Data.Y        = frame->frame->data[0];
 
133
        frame->surface_internal.Data.UV       = frame->frame->data[1];
 
134
 
 
135
        frame->surface = &frame->surface_internal;
 
136
    }
 
137
 
 
138
    return 0;
 
139
}
 
140
 
 
141
static void qsv_clear_unused_frames(QSVContext *q)
 
142
{
 
143
    QSVFrame *cur = q->work_frames;
 
144
    while (cur) {
 
145
        if (cur->surface && !cur->surface->Data.Locked) {
 
146
            cur->surface = NULL;
 
147
            av_frame_unref(cur->frame);
 
148
        }
 
149
        cur = cur->next;
 
150
    }
 
151
}
 
152
 
 
153
static int get_surface(AVCodecContext *avctx, QSVContext *q, mfxFrameSurface1 **surf)
 
154
{
 
155
    QSVFrame *frame, **last;
 
156
    int ret;
 
157
 
 
158
    qsv_clear_unused_frames(q);
 
159
 
 
160
    frame = q->work_frames;
 
161
    last  = &q->work_frames;
 
162
    while (frame) {
 
163
        if (!frame->surface) {
 
164
            ret = alloc_frame(avctx, frame);
 
165
            if (ret < 0)
 
166
                return ret;
 
167
            *surf = frame->surface;
 
168
            return 0;
 
169
        }
 
170
 
 
171
        last  = &frame->next;
 
172
        frame = frame->next;
 
173
    }
 
174
 
 
175
    frame = av_mallocz(sizeof(*frame));
 
176
    if (!frame)
 
177
        return AVERROR(ENOMEM);
 
178
    frame->frame = av_frame_alloc();
 
179
    if (!frame->frame) {
 
180
        av_freep(&frame);
 
181
        return AVERROR(ENOMEM);
 
182
    }
 
183
    *last = frame;
 
184
 
 
185
    ret = alloc_frame(avctx, frame);
 
186
    if (ret < 0)
 
187
        return ret;
 
188
 
 
189
    *surf = frame->surface;
 
190
 
 
191
    return 0;
 
192
}
 
193
 
 
194
static AVFrame *find_frame(QSVContext *q, mfxFrameSurface1 *surf)
 
195
{
 
196
    QSVFrame *cur = q->work_frames;
 
197
    while (cur) {
 
198
        if (surf == cur->surface)
 
199
            return cur->frame;
 
200
        cur = cur->next;
 
201
    }
 
202
    return NULL;
 
203
}
 
204
 
 
205
int ff_qsv_decode(AVCodecContext *avctx, QSVContext *q,
 
206
                  AVFrame *frame, int *got_frame,
 
207
                  AVPacket *avpkt)
 
208
{
 
209
    mfxFrameSurface1 *insurf;
 
210
    mfxFrameSurface1 *outsurf;
 
211
    mfxSyncPoint sync;
 
212
    mfxBitstream bs = { { { 0 } } };
 
213
    int ret;
 
214
 
 
215
    if (avpkt->size) {
 
216
        bs.Data       = avpkt->data;
 
217
        bs.DataLength = avpkt->size;
 
218
        bs.MaxLength  = bs.DataLength;
 
219
        bs.TimeStamp  = avpkt->pts;
 
220
    }
 
221
 
 
222
    do {
 
223
        ret = get_surface(avctx, q, &insurf);
 
224
        if (ret < 0)
 
225
            return ret;
 
226
 
 
227
        ret = MFXVideoDECODE_DecodeFrameAsync(q->session, avpkt->size ? &bs : NULL,
 
228
                                              insurf, &outsurf, &sync);
 
229
        if (ret == MFX_WRN_DEVICE_BUSY)
 
230
            av_usleep(1);
 
231
 
 
232
    } while (ret == MFX_WRN_DEVICE_BUSY || ret == MFX_ERR_MORE_SURFACE);
 
233
 
 
234
    if (ret != MFX_ERR_NONE &&
 
235
        ret != MFX_ERR_MORE_DATA &&
 
236
        ret != MFX_WRN_VIDEO_PARAM_CHANGED &&
 
237
        ret != MFX_ERR_MORE_SURFACE) {
 
238
        av_log(avctx, AV_LOG_ERROR, "Error during QSV decoding.\n");
 
239
        return ff_qsv_error(ret);
 
240
    }
 
241
 
 
242
    if (sync) {
 
243
        AVFrame *src_frame;
 
244
 
 
245
        MFXVideoCORE_SyncOperation(q->session, sync, 60000);
 
246
 
 
247
        src_frame = find_frame(q, outsurf);
 
248
        if (!src_frame) {
 
249
            av_log(avctx, AV_LOG_ERROR,
 
250
                   "The returned surface does not correspond to any frame\n");
 
251
            return AVERROR_BUG;
 
252
        }
 
253
 
 
254
        ret = av_frame_ref(frame, src_frame);
 
255
        if (ret < 0)
 
256
            return ret;
 
257
 
 
258
        frame->pkt_pts = frame->pts = outsurf->Data.TimeStamp;
 
259
 
 
260
        frame->repeat_pict =
 
261
            outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_TRIPLING ? 4 :
 
262
            outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_DOUBLING ? 2 :
 
263
            outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_REPEATED ? 1 : 0;
 
264
        frame->top_field_first =
 
265
            outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_TFF;
 
266
        frame->interlaced_frame =
 
267
            !(outsurf->Info.PicStruct & MFX_PICSTRUCT_PROGRESSIVE);
 
268
 
 
269
        *got_frame = 1;
 
270
    }
 
271
 
 
272
    return bs.DataOffset;
 
273
}
 
274
 
 
275
int ff_qsv_decode_close(QSVContext *q)
 
276
{
 
277
    QSVFrame *cur = q->work_frames;
 
278
 
 
279
    while (cur) {
 
280
        q->work_frames = cur->next;
 
281
        av_frame_free(&cur->frame);
 
282
        av_freep(&cur);
 
283
        cur = q->work_frames;
 
284
    }
 
285
 
 
286
    if (q->internal_session)
 
287
        MFXClose(q->internal_session);
 
288
 
 
289
    return 0;
 
290
}