1 /*
2  * Intel MediaSDK QSV based MPEG-2 encoder
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 
22 #include <stdint.h>
23 #include <sys/types.h>
24 
25 #include <mfx/mfxvideo.h>
26 
27 #include "libavutil/common.h"
28 #include "libavutil/opt.h"
29 
30 #include "avcodec.h"
31 #include "codec_internal.h"
32 #include "qsv.h"
33 #include "qsv_internal.h"
34 #include "qsvenc.h"
35 
36 typedef struct QSVMpeg2EncContext {
37     AVClass *class;
38     QSVEncContext qsv;
39 } QSVMpeg2EncContext;
40 
qsv_enc_init(AVCodecContext *avctx)41 static av_cold int qsv_enc_init(AVCodecContext *avctx)
42 {
43     QSVMpeg2EncContext *q = avctx->priv_data;
44 
45     return ff_qsv_enc_init(avctx, &q->qsv);
46 }
47 
qsv_enc_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)48 static int qsv_enc_frame(AVCodecContext *avctx, AVPacket *pkt,
49                          const AVFrame *frame, int *got_packet)
50 {
51     QSVMpeg2EncContext *q = avctx->priv_data;
52 
53     return ff_qsv_encode(avctx, &q->qsv, pkt, frame, got_packet);
54 }
55 
qsv_enc_close(AVCodecContext *avctx)56 static av_cold int qsv_enc_close(AVCodecContext *avctx)
57 {
58     QSVMpeg2EncContext *q = avctx->priv_data;
59 
60     return ff_qsv_enc_close(avctx, &q->qsv);
61 }
62 
63 #define OFFSET(x) offsetof(QSVMpeg2EncContext, x)
64 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
65 static const AVOption options[] = {
66     QSV_COMMON_OPTS
67     QSV_OPTION_RDO
68 
69     { "profile", NULL, OFFSET(qsv.profile), AV_OPT_TYPE_INT, { .i64 = MFX_PROFILE_UNKNOWN }, 0, INT_MAX, VE, "profile" },
70     { "unknown", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_PROFILE_UNKNOWN        }, INT_MIN, INT_MAX,     VE, "profile" },
71     { "simple",  NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_PROFILE_MPEG2_SIMPLE   }, INT_MIN, INT_MAX,     VE, "profile" },
72     { "main",    NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_PROFILE_MPEG2_MAIN     }, INT_MIN, INT_MAX,     VE, "profile" },
73     { "high",    NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_PROFILE_MPEG2_HIGH     }, INT_MIN, INT_MAX,     VE, "profile" },
74 
75     { NULL },
76 };
77 
78 static const AVClass class = {
79     .class_name = "mpeg2_qsv encoder",
80     .item_name  = av_default_item_name,
81     .option     = options,
82     .version    = LIBAVUTIL_VERSION_INT,
83 };
84 
85 static const FFCodecDefault qsv_enc_defaults[] = {
86     { "b",         "1M"    },
87     { "refs",      "0"     },
88     // same as the x264 default
89     { "g",         "250"   },
90     { "bf",        "3"     },
91     { "trellis",   "-1"    },
92     { "flags",     "+cgop" },
93     { NULL },
94 };
95 
96 const FFCodec ff_mpeg2_qsv_encoder = {
97     .p.name         = "mpeg2_qsv",
98     .p.long_name    = NULL_IF_CONFIG_SMALL("MPEG-2 video (Intel Quick Sync Video acceleration)"),
99     .priv_data_size = sizeof(QSVMpeg2EncContext),
100     .p.type         = AVMEDIA_TYPE_VIDEO,
101     .p.id           = AV_CODEC_ID_MPEG2VIDEO,
102     .init           = qsv_enc_init,
103     FF_CODEC_ENCODE_CB(qsv_enc_frame),
104     .close          = qsv_enc_close,
105     .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HYBRID,
106     .p.pix_fmts     = (const enum AVPixelFormat[]){ AV_PIX_FMT_NV12,
107                                                     AV_PIX_FMT_QSV,
108                                                     AV_PIX_FMT_NONE },
109     .p.priv_class   = &class,
110     .defaults       = qsv_enc_defaults,
111     .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
112     .p.wrapper_name = "qsv",
113     .hw_configs     = ff_qsv_enc_hw_configs,
114 };
115