Browse code

Merge commit '72b7441a10f578a1d0be7083d8f5adf6a01921c2'

* commit '72b7441a10f578a1d0be7083d8f5adf6a01921c2':
lavc: add Intel libmfx-based H.264 encoder

Conflicts:
Changelog
libavcodec/version.h

Merged-by: Michael Niedermayer <michaelni@gmx.at>

Michael Niedermayer authored on 2015/03/28 09:33:27
Showing 10 changed files
... ...
@@ -10,6 +10,7 @@ version <next>:
10 10
 - Drop support for nvenc API before 5.0
11 11
 - nvenc H265 encoder
12 12
 - Detelecine filter
13
+- Intel QSV-accelerated H.264 encoding
13 14
 
14 15
 
15 16
 version 2.6:
... ...
@@ -1892,6 +1892,7 @@ CONFIG_EXTRA="
1892 1892
     qpeldsp
1893 1893
     qsv
1894 1894
     qsvdec
1895
+    qsvenc
1895 1896
     rangecoder
1896 1897
     riffdec
1897 1898
     riffenc
... ...
@@ -2075,6 +2076,7 @@ mpegaudiodsp_select="dct"
2075 2075
 mpegvideo_select="blockdsp h264chroma hpeldsp idctdsp me_cmp videodsp"
2076 2076
 mpegvideoenc_select="me_cmp mpegvideo pixblockdsp qpeldsp"
2077 2077
 qsvdec_select="qsv"
2078
+qsvenc_select="qsv"
2078 2079
 
2079 2080
 # decoders / encoders
2080 2081
 aac_decoder_select="imdct15 mdct sinewin"
... ...
@@ -2150,6 +2152,8 @@ h264_decoder_select="cabac golomb h264chroma h264dsp h264pred h264qpel startcode
2150 2150
 h264_decoder_suggest="error_resilience"
2151 2151
 h264_qsv_decoder_deps="libmfx"
2152 2152
 h264_qsv_decoder_select="h264_mp4toannexb_bsf h264_parser qsvdec h264_qsv_hwaccel"
2153
+h264_qsv_encoder_deps="libmfx"
2154
+h264_qsv_encoder_select="qsvenc"
2153 2155
 hevc_decoder_select="bswapdsp cabac golomb videodsp"
2154 2156
 huffyuv_decoder_select="bswapdsp huffyuvdsp llviddsp"
2155 2157
 huffyuv_encoder_select="bswapdsp huffman huffyuvencdsp llviddsp"
... ...
@@ -94,6 +94,7 @@ OBJS-$(CONFIG_PIXBLOCKDSP)             += pixblockdsp.o
94 94
 OBJS-$(CONFIG_QPELDSP)                 += qpeldsp.o
95 95
 OBJS-$(CONFIG_QSV)                     += qsv.o
96 96
 OBJS-$(CONFIG_QSVDEC)                  += qsvdec.o
97
+OBJS-$(CONFIG_QSVENC)                  += qsvenc.o
97 98
 OBJS-$(CONFIG_RANGECODER)              += rangecoder.o
98 99
 RDFT-OBJS-$(CONFIG_HARDCODED_TABLES)   += sin_tables.o
99 100
 OBJS-$(CONFIG_RDFT)                    += rdft.o $(RDFT-OBJS-yes)
... ...
@@ -270,6 +271,7 @@ OBJS-$(CONFIG_H264_DECODER)            += h264.o h264_cabac.o h264_cavlc.o \
270 270
                                           h264_refs.o h264_sei.o h264_slice.o
271 271
 OBJS-$(CONFIG_H264_VDA_DECODER)        += vda_h264_dec.o
272 272
 OBJS-$(CONFIG_H264_QSV_DECODER)        += qsvdec_h264.o
273
+OBJS-$(CONFIG_H264_QSV_ENCODER)        += qsvenc_h264.o
273 274
 OBJS-$(CONFIG_HEVC_DECODER)            += hevc.o hevc_mvs.o hevc_ps.o hevc_sei.o \
274 275
                                           hevc_cabac.o hevc_refs.o hevcpred.o    \
275 276
                                           hevcdsp.o hevc_filter.o
... ...
@@ -554,6 +554,7 @@ void avcodec_register_all(void)
554 554
     /* external libraries, that shouldn't be used by default if one of the
555 555
      * above is available */
556 556
     REGISTER_ENCODER(LIBOPENH264,       libopenh264);
557
+    REGISTER_ENCODER(H264_QSV,          h264_qsv);
557 558
 
558 559
     /* parsers */
559 560
     REGISTER_PARSER(AAC,                aac);
... ...
@@ -21,11 +21,24 @@
21 21
 #ifndef AVCODEC_QSV_INTERNAL_H
22 22
 #define AVCODEC_QSV_INTERNAL_H
23 23
 
24
+#include <mfx/mfxvideo.h>
25
+
26
+#include "libavutil/frame.h"
27
+
24 28
 #define QSV_VERSION_MAJOR 1
25 29
 #define QSV_VERSION_MINOR 1
26 30
 
27 31
 #define ASYNC_DEPTH_DEFAULT 4       // internal parallelism
28 32
 
33
+typedef struct QSVFrame {
34
+    AVFrame *frame;
35
+    mfxFrameSurface1 *surface;
36
+
37
+    mfxFrameSurface1 surface_internal;
38
+
39
+    struct QSVFrame *next;
40
+} QSVFrame;
41
+
29 42
 /**
30 43
  * Convert a libmfx error code into a libav error code.
31 44
  */
... ...
@@ -32,15 +32,7 @@
32 32
 #include "libavutil/pixfmt.h"
33 33
 
34 34
 #include "avcodec.h"
35
-
36
-typedef struct QSVFrame {
37
-    AVFrame *frame;
38
-    mfxFrameSurface1 *surface;
39
-
40
-    mfxFrameSurface1 surface_internal;
41
-
42
-    struct QSVFrame *next;
43
-} QSVFrame;
35
+#include "qsv_internal.h"
44 36
 
45 37
 typedef struct QSVContext {
46 38
     // the session used for decoding
47 39
new file mode 100644
... ...
@@ -0,0 +1,444 @@
0
+/*
1
+ * Intel MediaSDK QSV encoder utility functions
2
+ *
3
+ * copyright (c) 2013 Yukinori Yamazoe
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
+#include <string.h>
24
+#include <sys/types.h>
25
+#include <mfx/mfxvideo.h>
26
+
27
+#include "libavutil/common.h"
28
+#include "libavutil/mem.h"
29
+#include "libavutil/log.h"
30
+#include "libavutil/time.h"
31
+#include "libavutil/imgutils.h"
32
+
33
+#include "avcodec.h"
34
+#include "internal.h"
35
+#include "qsv.h"
36
+#include "qsv_internal.h"
37
+#include "qsvenc.h"
38
+
39
+static int init_video_param(AVCodecContext *avctx, QSVEncContext *q)
40
+{
41
+    const char *ratecontrol_desc;
42
+
43
+    float quant;
44
+    int ret;
45
+
46
+    ret = ff_qsv_codec_id_to_mfx(avctx->codec_id);
47
+    if (ret < 0)
48
+        return AVERROR_BUG;
49
+    q->param.mfx.CodecId = ret;
50
+
51
+    if (avctx->level > 0)
52
+        q->param.mfx.CodecLevel = avctx->level;
53
+
54
+    q->param.mfx.CodecProfile       = q->profile;
55
+    q->param.mfx.TargetUsage        = q->preset;
56
+    q->param.mfx.GopPicSize         = FFMAX(0, avctx->gop_size);
57
+    q->param.mfx.GopRefDist         = FFMAX(-1, avctx->max_b_frames) + 1;
58
+    q->param.mfx.GopOptFlag         = avctx->flags & CODEC_FLAG_CLOSED_GOP ?
59
+                                      MFX_GOP_CLOSED : 0;
60
+    q->param.mfx.IdrInterval        = q->idr_interval;
61
+    q->param.mfx.NumSlice           = avctx->slices;
62
+    q->param.mfx.NumRefFrame        = FFMAX(0, avctx->refs);
63
+    q->param.mfx.EncodedOrder       = 0;
64
+    q->param.mfx.BufferSizeInKB     = 0;
65
+
66
+    q->param.mfx.FrameInfo.FourCC         = MFX_FOURCC_NV12;
67
+    q->param.mfx.FrameInfo.Width          = FFALIGN(avctx->width, 16);
68
+    q->param.mfx.FrameInfo.Height         = FFALIGN(avctx->height, 32);
69
+    q->param.mfx.FrameInfo.CropX          = 0;
70
+    q->param.mfx.FrameInfo.CropY          = 0;
71
+    q->param.mfx.FrameInfo.CropW          = avctx->width;
72
+    q->param.mfx.FrameInfo.CropH          = avctx->height;
73
+    q->param.mfx.FrameInfo.AspectRatioW   = avctx->sample_aspect_ratio.num;
74
+    q->param.mfx.FrameInfo.AspectRatioH   = avctx->sample_aspect_ratio.den;
75
+    q->param.mfx.FrameInfo.PicStruct      = MFX_PICSTRUCT_PROGRESSIVE;
76
+    q->param.mfx.FrameInfo.ChromaFormat   = MFX_CHROMAFORMAT_YUV420;
77
+    q->param.mfx.FrameInfo.BitDepthLuma   = 8;
78
+    q->param.mfx.FrameInfo.BitDepthChroma = 8;
79
+
80
+    if (avctx->framerate.den > 0 && avctx->framerate.num > 0) {
81
+        q->param.mfx.FrameInfo.FrameRateExtN = avctx->framerate.num;
82
+        q->param.mfx.FrameInfo.FrameRateExtD = avctx->framerate.den;
83
+    } else {
84
+        q->param.mfx.FrameInfo.FrameRateExtN  = avctx->time_base.den;
85
+        q->param.mfx.FrameInfo.FrameRateExtD  = avctx->time_base.num;
86
+    }
87
+
88
+    if (avctx->flags & CODEC_FLAG_QSCALE) {
89
+        q->param.mfx.RateControlMethod = MFX_RATECONTROL_CQP;
90
+        ratecontrol_desc = "constant quantization parameter (CQP)";
91
+    } else if (avctx->rc_max_rate == avctx->bit_rate) {
92
+        q->param.mfx.RateControlMethod = MFX_RATECONTROL_CBR;
93
+        ratecontrol_desc = "constant bitrate (CBR)";
94
+    } else if (!avctx->rc_max_rate) {
95
+        q->param.mfx.RateControlMethod = MFX_RATECONTROL_AVBR;
96
+        ratecontrol_desc = "average variable bitrate (AVBR)";
97
+    } else {
98
+        q->param.mfx.RateControlMethod = MFX_RATECONTROL_VBR;
99
+        ratecontrol_desc = "variable bitrate (VBR)";
100
+    }
101
+
102
+    av_log(avctx, AV_LOG_VERBOSE, "Using the %s ratecontrol method\n", ratecontrol_desc);
103
+
104
+    switch (q->param.mfx.RateControlMethod) {
105
+    case MFX_RATECONTROL_CBR:
106
+    case MFX_RATECONTROL_VBR:
107
+        q->param.mfx.InitialDelayInKB = avctx->rc_initial_buffer_occupancy / 1000;
108
+        q->param.mfx.TargetKbps       = avctx->bit_rate / 1000;
109
+        q->param.mfx.MaxKbps          = avctx->bit_rate / 1000;
110
+        break;
111
+    case MFX_RATECONTROL_CQP:
112
+        quant = avctx->global_quality / FF_QP2LAMBDA;
113
+
114
+        q->param.mfx.QPI = av_clip(quant * fabs(avctx->i_quant_factor) + avctx->i_quant_offset, 0, 51);
115
+        q->param.mfx.QPP = av_clip(quant, 0, 51);
116
+        q->param.mfx.QPB = av_clip(quant * fabs(avctx->b_quant_factor) + avctx->b_quant_offset, 0, 51);
117
+
118
+        break;
119
+    case MFX_RATECONTROL_AVBR:
120
+        q->param.mfx.TargetKbps  = avctx->bit_rate / 1000;
121
+        q->param.mfx.Convergence = q->avbr_convergence;
122
+        q->param.mfx.Accuracy    = q->avbr_accuracy;
123
+        break;
124
+    }
125
+
126
+    q->extco.Header.BufferId      = MFX_EXTBUFF_CODING_OPTION;
127
+    q->extco.Header.BufferSz      = sizeof(q->extco);
128
+    q->extco.CAVLC                = avctx->coder_type == FF_CODER_TYPE_VLC ?
129
+                                    MFX_CODINGOPTION_ON : MFX_CODINGOPTION_UNKNOWN;
130
+
131
+    q->extparam[0] = (mfxExtBuffer *)&q->extco;
132
+
133
+    q->param.ExtParam    = q->extparam;
134
+    q->param.NumExtParam = FF_ARRAY_ELEMS(q->extparam);
135
+
136
+    return 0;
137
+}
138
+
139
+static int qsv_retrieve_enc_params(AVCodecContext *avctx, QSVEncContext *q)
140
+{
141
+    uint8_t sps_buf[128];
142
+    uint8_t pps_buf[128];
143
+
144
+    mfxExtCodingOptionSPSPPS extradata = {
145
+        .Header.BufferId = MFX_EXTBUFF_CODING_OPTION_SPSPPS,
146
+        .Header.BufferSz = sizeof(extradata),
147
+        .SPSBuffer = sps_buf, .SPSBufSize = sizeof(sps_buf),
148
+        .PPSBuffer = pps_buf, .PPSBufSize = sizeof(pps_buf)
149
+    };
150
+
151
+    mfxExtBuffer *ext_buffers[] = {
152
+        (mfxExtBuffer*)&extradata,
153
+    };
154
+
155
+    int ret;
156
+
157
+    q->param.ExtParam    = ext_buffers;
158
+    q->param.NumExtParam = FF_ARRAY_ELEMS(ext_buffers);
159
+
160
+    ret = MFXVideoENCODE_GetVideoParam(q->session, &q->param);
161
+    if (ret < 0)
162
+        return ff_qsv_error(ret);
163
+
164
+    q->packet_size = q->param.mfx.BufferSizeInKB * 1000;
165
+
166
+    if (!extradata.SPSBufSize || !extradata.PPSBufSize) {
167
+        av_log(avctx, AV_LOG_ERROR, "No extradata returned from libmfx.\n");
168
+        return AVERROR_UNKNOWN;
169
+    }
170
+
171
+    avctx->extradata = av_malloc(extradata.SPSBufSize + extradata.PPSBufSize +
172
+                                 FF_INPUT_BUFFER_PADDING_SIZE);
173
+    if (!avctx->extradata)
174
+        return AVERROR(ENOMEM);
175
+
176
+    memcpy(avctx->extradata,                        sps_buf, extradata.SPSBufSize);
177
+    memcpy(avctx->extradata + extradata.SPSBufSize, pps_buf, extradata.PPSBufSize);
178
+    avctx->extradata_size = extradata.SPSBufSize + extradata.PPSBufSize;
179
+    memset(avctx->extradata + avctx->extradata_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
180
+
181
+    return 0;
182
+}
183
+
184
+int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q)
185
+{
186
+    int ret;
187
+
188
+    q->param.IOPattern  = MFX_IOPATTERN_IN_SYSTEM_MEMORY;
189
+    q->param.AsyncDepth = q->async_depth;
190
+
191
+    if (avctx->hwaccel_context) {
192
+        AVQSVContext *qsv = avctx->hwaccel_context;
193
+
194
+        q->session         = qsv->session;
195
+        q->param.IOPattern = qsv->iopattern;
196
+    }
197
+
198
+    if (!q->session) {
199
+        ret = ff_qsv_init_internal_session(avctx, &q->internal_session);
200
+        if (ret < 0)
201
+            return ret;
202
+
203
+        q->session = q->internal_session;
204
+    }
205
+
206
+    ret = init_video_param(avctx, q);
207
+    if (ret < 0)
208
+        return ret;
209
+
210
+    ret = MFXVideoENCODE_QueryIOSurf(q->session, &q->param, &q->req);
211
+    if (ret < 0) {
212
+        av_log(avctx, AV_LOG_ERROR, "Error querying the encoding parameters\n");
213
+        return ff_qsv_error(ret);
214
+    }
215
+
216
+    ret = MFXVideoENCODE_Init(q->session, &q->param);
217
+    if (ret < 0) {
218
+        av_log(avctx, AV_LOG_ERROR, "Error initializing the encoder\n");
219
+        return ff_qsv_error(ret);
220
+    }
221
+
222
+    ret = qsv_retrieve_enc_params(avctx, q);
223
+    if (ret < 0) {
224
+        av_log(avctx, AV_LOG_ERROR, "Error retrieving encoding parameters.\n");
225
+        return ret;
226
+    }
227
+
228
+    avctx->coded_frame = av_frame_alloc();
229
+    if (!avctx->coded_frame)
230
+        return AVERROR(ENOMEM);
231
+
232
+    q->avctx = avctx;
233
+
234
+    return 0;
235
+}
236
+
237
+static void clear_unused_frames(QSVEncContext *q)
238
+{
239
+    QSVFrame *cur = q->work_frames;
240
+    while (cur) {
241
+        if (cur->surface && !cur->surface->Data.Locked) {
242
+            cur->surface = NULL;
243
+            av_frame_unref(cur->frame);
244
+        }
245
+        cur = cur->next;
246
+    }
247
+}
248
+
249
+static int get_free_frame(QSVEncContext *q, QSVFrame **f)
250
+{
251
+    QSVFrame *frame, **last;
252
+
253
+    clear_unused_frames(q);
254
+
255
+    frame = q->work_frames;
256
+    last  = &q->work_frames;
257
+    while (frame) {
258
+        if (!frame->surface) {
259
+            *f = frame;
260
+            return 0;
261
+        }
262
+
263
+        last  = &frame->next;
264
+        frame = frame->next;
265
+    }
266
+
267
+    frame = av_mallocz(sizeof(*frame));
268
+    if (!frame)
269
+        return AVERROR(ENOMEM);
270
+    frame->frame = av_frame_alloc();
271
+    if (!frame->frame) {
272
+        av_freep(&frame);
273
+        return AVERROR(ENOMEM);
274
+    }
275
+    *last = frame;
276
+
277
+    *f = frame;
278
+
279
+    return 0;
280
+}
281
+
282
+static int submit_frame(QSVEncContext *q, const AVFrame *frame,
283
+                        mfxFrameSurface1 **surface)
284
+{
285
+    QSVFrame *qf;
286
+    int ret;
287
+
288
+    ret = get_free_frame(q, &qf);
289
+    if (ret < 0)
290
+        return ret;
291
+
292
+    if (frame->format == AV_PIX_FMT_QSV) {
293
+        ret = av_frame_ref(qf->frame, frame);
294
+        if (ret < 0)
295
+            return ret;
296
+
297
+        qf->surface = (mfxFrameSurface1*)qf->frame->data[3];
298
+        *surface = qf->surface;
299
+        return 0;
300
+    }
301
+
302
+    /* make a copy if the input is not padded as libmfx requires */
303
+    if (frame->height & 31 || frame->linesize[0] & 15) {
304
+        qf->frame->height = FFALIGN(frame->height, 32);
305
+        qf->frame->width  = FFALIGN(frame->width, 16);
306
+
307
+        ret = ff_get_buffer(q->avctx, qf->frame, AV_GET_BUFFER_FLAG_REF);
308
+        if (ret < 0)
309
+            return ret;
310
+
311
+        qf->frame->height = frame->height;
312
+        qf->frame->width  = frame->width;
313
+        ret = av_frame_copy(qf->frame, frame);
314
+        if (ret < 0) {
315
+            av_frame_unref(qf->frame);
316
+            return ret;
317
+        }
318
+    } else {
319
+        ret = av_frame_ref(qf->frame, frame);
320
+        if (ret < 0)
321
+            return ret;
322
+    }
323
+
324
+    qf->surface_internal.Info = q->param.mfx.FrameInfo;
325
+
326
+    qf->surface_internal.Info.PicStruct =
327
+        !frame->interlaced_frame ? MFX_PICSTRUCT_PROGRESSIVE :
328
+        frame->top_field_first   ? MFX_PICSTRUCT_FIELD_TFF :
329
+                                   MFX_PICSTRUCT_FIELD_BFF;
330
+    if (frame->repeat_pict == 1)
331
+        qf->surface_internal.Info.PicStruct |= MFX_PICSTRUCT_FIELD_REPEATED;
332
+    else if (frame->repeat_pict == 2)
333
+        qf->surface_internal.Info.PicStruct |= MFX_PICSTRUCT_FRAME_DOUBLING;
334
+    else if (frame->repeat_pict == 4)
335
+        qf->surface_internal.Info.PicStruct |= MFX_PICSTRUCT_FRAME_TRIPLING;
336
+
337
+    qf->surface_internal.Data.PitchLow  = qf->frame->linesize[0];
338
+    qf->surface_internal.Data.Y         = qf->frame->data[0];
339
+    qf->surface_internal.Data.UV        = qf->frame->data[1];
340
+    qf->surface_internal.Data.TimeStamp = av_rescale_q(frame->pts, q->avctx->time_base, (AVRational){1, 90000});
341
+
342
+    qf->surface = &qf->surface_internal;
343
+
344
+    *surface = qf->surface;
345
+
346
+    return 0;
347
+}
348
+
349
+static void print_interlace_msg(AVCodecContext *avctx, QSVEncContext *q)
350
+{
351
+    if (q->param.mfx.CodecId == MFX_CODEC_AVC) {
352
+        if (q->param.mfx.CodecProfile == MFX_PROFILE_AVC_BASELINE ||
353
+            q->param.mfx.CodecLevel < MFX_LEVEL_AVC_21 ||
354
+            q->param.mfx.CodecLevel > MFX_LEVEL_AVC_41)
355
+            av_log(avctx, AV_LOG_WARNING,
356
+                   "Interlaced coding is supported"
357
+                   " at Main/High Profile Level 2.1-4.1\n");
358
+    }
359
+}
360
+
361
+int ff_qsv_encode(AVCodecContext *avctx, QSVEncContext *q,
362
+                  AVPacket *pkt, const AVFrame *frame, int *got_packet)
363
+{
364
+    mfxBitstream bs = { { { 0 } } };
365
+
366
+    mfxFrameSurface1 *surf = NULL;
367
+    mfxSyncPoint sync      = NULL;
368
+    int ret;
369
+
370
+    if (frame) {
371
+        ret = submit_frame(q, frame, &surf);
372
+        if (ret < 0) {
373
+            av_log(avctx, AV_LOG_ERROR, "Error submitting the frame for encoding.\n");
374
+            return ret;
375
+        }
376
+    }
377
+
378
+    ret = ff_alloc_packet(pkt, q->packet_size);
379
+    if (ret < 0) {
380
+        av_log(avctx, AV_LOG_ERROR, "Error allocating the output packet\n");
381
+        return ret;
382
+    }
383
+    bs.Data      = pkt->data;
384
+    bs.MaxLength = pkt->size;
385
+
386
+    do {
387
+        ret = MFXVideoENCODE_EncodeFrameAsync(q->session, NULL, surf, &bs, &sync);
388
+        if (ret == MFX_WRN_DEVICE_BUSY)
389
+            av_usleep(1);
390
+    } while (ret > 0);
391
+
392
+    if (ret < 0)
393
+        return (ret == MFX_ERR_MORE_DATA) ? 0 : ff_qsv_error(ret);
394
+
395
+    if (ret == MFX_WRN_INCOMPATIBLE_VIDEO_PARAM && frame->interlaced_frame)
396
+        print_interlace_msg(avctx, q);
397
+
398
+    if (sync) {
399
+        MFXVideoCORE_SyncOperation(q->session, sync, 60000);
400
+
401
+        if (bs.FrameType & MFX_FRAMETYPE_I || bs.FrameType & MFX_FRAMETYPE_xI)
402
+            avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
403
+        else if (bs.FrameType & MFX_FRAMETYPE_P || bs.FrameType & MFX_FRAMETYPE_xP)
404
+            avctx->coded_frame->pict_type = AV_PICTURE_TYPE_P;
405
+        else if (bs.FrameType & MFX_FRAMETYPE_B || bs.FrameType & MFX_FRAMETYPE_xB)
406
+            avctx->coded_frame->pict_type = AV_PICTURE_TYPE_B;
407
+
408
+        pkt->dts  = av_rescale_q(bs.DecodeTimeStamp, (AVRational){1, 90000}, avctx->time_base);
409
+        pkt->pts  = av_rescale_q(bs.TimeStamp,       (AVRational){1, 90000}, avctx->time_base);
410
+        pkt->size = bs.DataLength;
411
+
412
+        if (bs.FrameType & MFX_FRAMETYPE_IDR ||
413
+            bs.FrameType & MFX_FRAMETYPE_xIDR)
414
+            pkt->flags |= AV_PKT_FLAG_KEY;
415
+
416
+        *got_packet = 1;
417
+    }
418
+
419
+    return 0;
420
+}
421
+
422
+int ff_qsv_enc_close(AVCodecContext *avctx, QSVEncContext *q)
423
+{
424
+    QSVFrame *cur;
425
+
426
+    MFXVideoENCODE_Close(q->session);
427
+    if (q->internal_session)
428
+        MFXClose(q->internal_session);
429
+    q->session          = NULL;
430
+    q->internal_session = NULL;
431
+
432
+    cur = q->work_frames;
433
+    while (cur) {
434
+        q->work_frames = cur->next;
435
+        av_frame_free(&cur->frame);
436
+        av_freep(&cur);
437
+        cur = q->work_frames;
438
+    }
439
+
440
+    av_frame_free(&avctx->coded_frame);
441
+
442
+    return 0;
443
+}
0 444
new file mode 100644
... ...
@@ -0,0 +1,68 @@
0
+/*
1
+ * Intel MediaSDK QSV encoder utility functions
2
+ *
3
+ * copyright (c) 2013 Yukinori Yamazoe
4
+ *
5
+ * This file is part of FFmpeg.
6
+ *
7
+ * FFmpeg is free software; you can redistribute it and/or
8
+ * modify it under the terms of the GNU Lesser General Public
9
+ * License as published by the Free Software Foundation; either
10
+ * version 2.1 of the License, or (at your option) any later version.
11
+ *
12
+ * FFmpeg is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
+ * Lesser General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU Lesser General Public
18
+ * License along with FFmpeg; if not, write to the Free Software
19
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
+ */
21
+
22
+#ifndef AVCODEC_QSVENC_H
23
+#define AVCODEC_QSVENC_H
24
+
25
+#include <stdint.h>
26
+#include <sys/types.h>
27
+
28
+#include <mfx/mfxvideo.h>
29
+
30
+#include "libavutil/avutil.h"
31
+
32
+#include "avcodec.h"
33
+#include "qsv_internal.h"
34
+
35
+typedef struct QSVEncContext {
36
+    AVCodecContext *avctx;
37
+
38
+    QSVFrame *work_frames;
39
+
40
+    mfxSession session;
41
+    mfxSession internal_session;
42
+
43
+    int packet_size;
44
+
45
+    mfxVideoParam param;
46
+    mfxFrameAllocRequest req;
47
+
48
+    mfxExtCodingOption  extco;
49
+    mfxExtBuffer *extparam[1];
50
+
51
+    // options set by the caller
52
+    int async_depth;
53
+    int idr_interval;
54
+    int profile;
55
+    int preset;
56
+    int avbr_accuracy;
57
+    int avbr_convergence;
58
+} QSVEncContext;
59
+
60
+int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q);
61
+
62
+int ff_qsv_encode(AVCodecContext *avctx, QSVEncContext *q,
63
+                  AVPacket *pkt, const AVFrame *frame, int *got_packet);
64
+
65
+int ff_qsv_enc_close(AVCodecContext *avctx, QSVEncContext *q);
66
+
67
+#endif /* AVCODEC_QSVENC_H */
0 68
new file mode 100644
... ...
@@ -0,0 +1,121 @@
0
+/*
1
+ * Intel MediaSDK QSV based H.264 enccoder
2
+ *
3
+ * copyright (c) 2013 Yukinori Yamazoe
4
+ *
5
+ * This file is part of FFmpeg.
6
+ *
7
+ * FFmpeg is free software; you can redistribute it and/or
8
+ * modify it under the terms of the GNU Lesser General Public
9
+ * License as published by the Free Software Foundation; either
10
+ * version 2.1 of the License, or (at your option) any later version.
11
+ *
12
+ * FFmpeg is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
+ * Lesser General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU Lesser General Public
18
+ * License along with FFmpeg; if not, write to the Free Software
19
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
+ */
21
+
22
+
23
+#include <stdint.h>
24
+#include <sys/types.h>
25
+
26
+#include <mfx/mfxvideo.h>
27
+
28
+#include "libavutil/opt.h"
29
+
30
+#include "avcodec.h"
31
+#include "internal.h"
32
+#include "h264.h"
33
+#include "qsv.h"
34
+#include "qsv_internal.h"
35
+#include "qsvenc.h"
36
+
37
+typedef struct QSVH264EncContext {
38
+    AVClass *class;
39
+    QSVEncContext qsv;
40
+} QSVH264EncContext;
41
+
42
+static av_cold int qsv_enc_init(AVCodecContext *avctx)
43
+{
44
+    QSVH264EncContext *q = avctx->priv_data;
45
+
46
+    return ff_qsv_enc_init(avctx, &q->qsv);
47
+}
48
+
49
+static int qsv_enc_frame(AVCodecContext *avctx, AVPacket *pkt,
50
+                         const AVFrame *frame, int *got_packet)
51
+{
52
+    QSVH264EncContext *q = avctx->priv_data;
53
+
54
+    return ff_qsv_encode(avctx, &q->qsv, pkt, frame, got_packet);
55
+}
56
+
57
+static av_cold int qsv_enc_close(AVCodecContext *avctx)
58
+{
59
+    QSVH264EncContext *q = avctx->priv_data;
60
+
61
+    return ff_qsv_enc_close(avctx, &q->qsv);
62
+}
63
+
64
+#define OFFSET(x) offsetof(QSVH264EncContext, x)
65
+#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
66
+static const AVOption options[] = {
67
+    { "async_depth", "Maximum processing parallelism", OFFSET(qsv.async_depth), AV_OPT_TYPE_INT, { .i64 = ASYNC_DEPTH_DEFAULT }, 0, INT_MAX, VE },
68
+    { "idr_interval", "Distance (in I-frames) between IDR frames", OFFSET(qsv.idr_interval), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE },
69
+    { "avbr_accuracy",    "Accuracy of the AVBR ratecontrol",    OFFSET(qsv.avbr_accuracy),    AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE },
70
+    { "avbr_convergence", "Convergence of the AVBR ratecontrol", OFFSET(qsv.avbr_convergence), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE },
71
+
72
+    { "profile", NULL, OFFSET(qsv.profile), AV_OPT_TYPE_INT, { .i64 = MFX_PROFILE_UNKNOWN }, 0, INT_MAX, VE, "profile" },
73
+    { "unknown" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_PROFILE_UNKNOWN      }, INT_MIN, INT_MAX,     VE, "profile" },
74
+    { "baseline", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_PROFILE_AVC_BASELINE }, INT_MIN, INT_MAX,     VE, "profile" },
75
+    { "main"    , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_PROFILE_AVC_MAIN     }, INT_MIN, INT_MAX,     VE, "profile" },
76
+    { "high"    , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_PROFILE_AVC_HIGH     }, INT_MIN, INT_MAX,     VE, "profile" },
77
+
78
+    { "preset", NULL, OFFSET(qsv.preset), AV_OPT_TYPE_INT, { .i64 = MFX_TARGETUSAGE_BALANCED }, 0, 7,   VE, "preset" },
79
+    { "fast",   NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_TARGETUSAGE_BEST_SPEED  },   INT_MIN, INT_MAX, VE, "preset" },
80
+    { "medium", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_TARGETUSAGE_BALANCED  },     INT_MIN, INT_MAX, VE, "preset" },
81
+    { "slow",   NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_TARGETUSAGE_BEST_QUALITY  }, INT_MIN, INT_MAX, VE, "preset" },
82
+
83
+    { NULL },
84
+};
85
+
86
+static const AVClass class = {
87
+    .class_name = "h264_qsv encoder",
88
+    .item_name  = av_default_item_name,
89
+    .option     = options,
90
+    .version    = LIBAVUTIL_VERSION_INT,
91
+};
92
+
93
+static const AVCodecDefault qsv_enc_defaults[] = {
94
+    { "b",         "1M"    },
95
+    { "refs",      "0"     },
96
+    // same as the x264 default
97
+    { "g",         "250"   },
98
+    { "bf",        "3"     },
99
+    { "coder",     "ac"    },
100
+
101
+    { "flags",     "+cgop" },
102
+    { NULL },
103
+};
104
+
105
+AVCodec ff_h264_qsv_encoder = {
106
+    .name           = "h264_qsv",
107
+    .long_name      = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (Intel Quick Sync Video acceleration)"),
108
+    .priv_data_size = sizeof(QSVH264EncContext),
109
+    .type           = AVMEDIA_TYPE_VIDEO,
110
+    .id             = AV_CODEC_ID_H264,
111
+    .init           = qsv_enc_init,
112
+    .encode2        = qsv_enc_frame,
113
+    .close          = qsv_enc_close,
114
+    .capabilities   = CODEC_CAP_DELAY,
115
+    .pix_fmts       = (const enum AVPixelFormat[]){ AV_PIX_FMT_NV12,
116
+                                                    AV_PIX_FMT_QSV,
117
+                                                    AV_PIX_FMT_NONE },
118
+    .priv_class     = &class,
119
+    .defaults       = qsv_enc_defaults,
120
+};
... ...
@@ -29,7 +29,7 @@
29 29
 #include "libavutil/version.h"
30 30
 
31 31
 #define LIBAVCODEC_VERSION_MAJOR 56
32
-#define LIBAVCODEC_VERSION_MINOR  30
32
+#define LIBAVCODEC_VERSION_MINOR  31
33 33
 #define LIBAVCODEC_VERSION_MICRO 100
34 34
 
35 35
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \