Browse code

vaapi: Add MJPEG decode hwaccel

Mark Thompson authored on 2017/11/24 08:47:52
Showing 8 changed files
... ...
@@ -21,7 +21,7 @@ version <next>:
21 21
 - video mix filter
22 22
 - video normalize filter
23 23
 - audio lv2 wrapper filter
24
-- VAAPI VP8 decoding
24
+- VAAPI MJPEG and VP8 decoding
25 25
 - AMD AMF H.264 and HEVC encoders
26 26
 - video fillborders filter
27 27
 - video setrange filter
... ...
@@ -2714,6 +2714,8 @@ hevc_vdpau_hwaccel_deps="vdpau VdpPictureInfoHEVC"
2714 2714
 hevc_vdpau_hwaccel_select="hevc_decoder"
2715 2715
 hevc_videotoolbox_hwaccel_deps="videotoolbox"
2716 2716
 hevc_videotoolbox_hwaccel_select="hevc_decoder"
2717
+mjpeg_vaapi_hwaccel_deps="vaapi"
2718
+mjpeg_vaapi_hwaccel_select="mjpeg_decoder"
2717 2719
 mpeg_xvmc_hwaccel_deps="xvmc"
2718 2720
 mpeg_xvmc_hwaccel_select="mpeg2video_decoder"
2719 2721
 mpeg1_nvdec_hwaccel_deps="nvdec"
... ...
@@ -854,6 +854,7 @@ OBJS-$(CONFIG_HEVC_NVDEC_HWACCEL)         += nvdec_hevc.o
854 854
 OBJS-$(CONFIG_HEVC_QSV_HWACCEL)           += qsvdec_h2645.o
855 855
 OBJS-$(CONFIG_HEVC_VAAPI_HWACCEL)         += vaapi_hevc.o
856 856
 OBJS-$(CONFIG_HEVC_VDPAU_HWACCEL)         += vdpau_hevc.o
857
+OBJS-$(CONFIG_MJPEG_VAAPI_HWACCEL)        += vaapi_mjpeg.o
857 858
 OBJS-$(CONFIG_MPEG1_NVDEC_HWACCEL)        += nvdec_mpeg12.o
858 859
 OBJS-$(CONFIG_MPEG1_VDPAU_HWACCEL)        += vdpau_mpeg12.o
859 860
 OBJS-$(CONFIG_MPEG1_VIDEOTOOLBOX_HWACCEL) += videotoolbox.o
... ...
@@ -37,6 +37,7 @@ extern const AVHWAccel ff_hevc_nvdec_hwaccel;
37 37
 extern const AVHWAccel ff_hevc_vaapi_hwaccel;
38 38
 extern const AVHWAccel ff_hevc_vdpau_hwaccel;
39 39
 extern const AVHWAccel ff_hevc_videotoolbox_hwaccel;
40
+extern const AVHWAccel ff_mjpeg_vaapi_hwaccel;
40 41
 extern const AVHWAccel ff_mpeg1_nvdec_hwaccel;
41 42
 extern const AVHWAccel ff_mpeg1_vdpau_hwaccel;
42 43
 extern const AVHWAccel ff_mpeg1_videotoolbox_hwaccel;
... ...
@@ -650,6 +650,9 @@ unk_pixfmt:
650 650
         s->avctx->pix_fmt = s->hwaccel_pix_fmt;
651 651
     } else {
652 652
         enum AVPixelFormat pix_fmts[] = {
653
+#if CONFIG_MJPEG_VAAPI_HWACCEL
654
+            AV_PIX_FMT_VAAPI,
655
+#endif
653 656
             s->avctx->pix_fmt,
654 657
             AV_PIX_FMT_NONE,
655 658
         };
... ...
@@ -2777,6 +2780,9 @@ AVCodec ff_mjpeg_decoder = {
2777 2777
     .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE |
2778 2778
                       FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
2779 2779
     .hw_configs     = (const AVCodecHWConfigInternal*[]) {
2780
+#if CONFIG_MJPEG_VAAPI_HWACCEL
2781
+                        HWACCEL_VAAPI(mjpeg),
2782
+#endif
2780 2783
                         NULL
2781 2784
                     },
2782 2785
 };
... ...
@@ -379,6 +379,8 @@ static const struct {
379 379
     MAP(HEVC,        HEVC_MAIN,       HEVCMain    ),
380 380
     MAP(HEVC,        HEVC_MAIN_10,    HEVCMain10  ),
381 381
 #endif
382
+    MAP(MJPEG,       MJPEG_HUFFMAN_BASELINE_DCT,
383
+                                      JPEGBaseline),
382 384
     MAP(WMV3,        VC1_SIMPLE,      VC1Simple   ),
383 385
     MAP(WMV3,        VC1_MAIN,        VC1Main     ),
384 386
     MAP(WMV3,        VC1_COMPLEX,     VC1Advanced ),
385 387
new file mode 100644
... ...
@@ -0,0 +1,159 @@
0
+/*
1
+ * This file is part of FFmpeg.
2
+ *
3
+ * FFmpeg is free software; you can redistribute it and/or
4
+ * modify it under the terms of the GNU Lesser General Public
5
+ * License as published by the Free Software Foundation; either
6
+ * version 2.1 of the License, or (at your option) any later version.
7
+ *
8
+ * FFmpeg is distributed in the hope that it will be useful,
9
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11
+ * Lesser General Public License for more details.
12
+ *
13
+ * You should have received a copy of the GNU Lesser General Public
14
+ * License along with FFmpeg; if not, write to the Free Software
15
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
+ */
17
+
18
+#include <va/va.h>
19
+#include <va/va_dec_jpeg.h>
20
+
21
+#include "hwaccel.h"
22
+#include "vaapi_decode.h"
23
+#include "mjpegdec.h"
24
+
25
+static int vaapi_mjpeg_start_frame(AVCodecContext          *avctx,
26
+                                   av_unused const uint8_t *buffer,
27
+                                   av_unused uint32_t       size)
28
+{
29
+    const MJpegDecodeContext *s = avctx->priv_data;
30
+    VAAPIDecodePicture *pic = s->hwaccel_picture_private;
31
+    VAPictureParameterBufferJPEGBaseline pp;
32
+    int err, i;
33
+
34
+    pic->output_surface = ff_vaapi_get_surface_id(s->picture_ptr);
35
+
36
+    pp = (VAPictureParameterBufferJPEGBaseline) {
37
+        .picture_width  = avctx->width,
38
+        .picture_height = avctx->height,
39
+
40
+        .num_components = s->nb_components,
41
+    };
42
+
43
+    for (i = 0; i < s->nb_components; i++) {
44
+        pp.components[i].component_id             = s->component_id[i];
45
+        pp.components[i].h_sampling_factor        = s->h_count[i];
46
+        pp.components[i].v_sampling_factor        = s->v_count[i];
47
+        pp.components[i].quantiser_table_selector = s->quant_index[i];
48
+    }
49
+
50
+    err = ff_vaapi_decode_make_param_buffer(avctx, pic,
51
+                                            VAPictureParameterBufferType,
52
+                                            &pp, sizeof(pp));
53
+    if (err < 0)
54
+        goto fail;
55
+
56
+    return 0;
57
+
58
+fail:
59
+    ff_vaapi_decode_cancel(avctx, pic);
60
+    return err;
61
+}
62
+
63
+static int vaapi_mjpeg_end_frame(AVCodecContext *avctx)
64
+{
65
+    const MJpegDecodeContext *s = avctx->priv_data;
66
+    VAAPIDecodePicture *pic = s->hwaccel_picture_private;
67
+
68
+    return ff_vaapi_decode_issue(avctx, pic);
69
+}
70
+
71
+static int vaapi_mjpeg_decode_slice(AVCodecContext *avctx,
72
+                                    const uint8_t  *buffer,
73
+                                    uint32_t        size)
74
+{
75
+    const MJpegDecodeContext *s = avctx->priv_data;
76
+    VAAPIDecodePicture *pic = s->hwaccel_picture_private;
77
+    VAHuffmanTableBufferJPEGBaseline huff;
78
+    VAIQMatrixBufferJPEGBaseline quant;
79
+    VASliceParameterBufferJPEGBaseline sp;
80
+    int err, i, j;
81
+
82
+    memset(&huff, 0, sizeof(huff));
83
+    for (i = 0; i < 2; i++) {
84
+        huff.load_huffman_table[i] = 1;
85
+        for (j = 0; j < 16; j++)
86
+            huff.huffman_table[i].num_dc_codes[j] = s->raw_huffman_lengths[0][i][j];
87
+        for (j = 0; j < 12; j++)
88
+            huff.huffman_table[i].dc_values[j] = s->raw_huffman_values[0][i][j];
89
+        for (j = 0; j < 16; j++)
90
+            huff.huffman_table[i].num_ac_codes[j] = s->raw_huffman_lengths[1][i][j];
91
+        for (j = 0; j < 162; j++)
92
+            huff.huffman_table[i].ac_values[j] = s->raw_huffman_values[1][i][j];
93
+    }
94
+
95
+    err = ff_vaapi_decode_make_param_buffer(avctx, pic,
96
+                                            VAHuffmanTableBufferType,
97
+                                            &huff, sizeof(huff));
98
+    if (err < 0)
99
+        goto fail;
100
+
101
+    memset(&quant, 0, sizeof(quant));
102
+    for (i = 0; i < 4; i++) {
103
+        quant.load_quantiser_table[i] = 1;
104
+        for (j = 0; j < 64; j++)
105
+            quant.quantiser_table[i][j] = s->quant_matrixes[i][j];
106
+    }
107
+
108
+    err = ff_vaapi_decode_make_param_buffer(avctx, pic,
109
+                                            VAIQMatrixBufferType,
110
+                                            &quant, sizeof(quant));
111
+    if (err < 0)
112
+        goto fail;
113
+
114
+    sp = (VASliceParameterBufferJPEGBaseline) {
115
+        .slice_data_size   = size,
116
+        .slice_data_offset = 0,
117
+        .slice_data_flag   = VA_SLICE_DATA_FLAG_ALL,
118
+
119
+        .slice_horizontal_position = 0,
120
+        .slice_vertical_position   = 0,
121
+
122
+        .restart_interval          = s->restart_interval,
123
+        .num_mcus                  = s->mb_width * s->mb_height,
124
+    };
125
+
126
+    sp.num_components = s->nb_components;
127
+    for (i = 0; i < s->nb_components; i++) {
128
+        sp.components[i].component_selector = s->component_id[s->comp_index[i]];
129
+        sp.components[i].dc_table_selector  = s->dc_index[i];
130
+        sp.components[i].ac_table_selector  = s->ac_index[i];
131
+    }
132
+
133
+    err = ff_vaapi_decode_make_slice_buffer(avctx, pic, &sp, sizeof(sp), buffer, size);
134
+    if (err)
135
+        goto fail;
136
+
137
+    return 0;
138
+
139
+fail:
140
+    ff_vaapi_decode_cancel(avctx, pic);
141
+    return err;
142
+}
143
+
144
+const AVHWAccel ff_mjpeg_vaapi_hwaccel = {
145
+    .name                 = "mjpeg_vaapi",
146
+    .type                 = AVMEDIA_TYPE_VIDEO,
147
+    .id                   = AV_CODEC_ID_MJPEG,
148
+    .pix_fmt              = AV_PIX_FMT_VAAPI,
149
+    .start_frame          = &vaapi_mjpeg_start_frame,
150
+    .end_frame            = &vaapi_mjpeg_end_frame,
151
+    .decode_slice         = &vaapi_mjpeg_decode_slice,
152
+    .frame_priv_data_size = sizeof(VAAPIDecodePicture),
153
+    .init                 = &ff_vaapi_decode_init,
154
+    .uninit               = &ff_vaapi_decode_uninit,
155
+    .frame_params         = &ff_vaapi_common_frame_params,
156
+    .priv_data_size       = sizeof(VAAPIDecodeContext),
157
+    .caps_internal        = HWACCEL_CAP_ASYNC_SAFE,
158
+};
... ...
@@ -29,7 +29,7 @@
29 29
 
30 30
 #define LIBAVCODEC_VERSION_MAJOR  58
31 31
 #define LIBAVCODEC_VERSION_MINOR  12
32
-#define LIBAVCODEC_VERSION_MICRO 100
32
+#define LIBAVCODEC_VERSION_MICRO 101
33 33
 
34 34
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
35 35
                                                LIBAVCODEC_VERSION_MINOR, \