lavc/vaapi: Add VP8 decode hwaccel
[libav.git] / libavcodec / vaapi_vp8.c
1 /*
2 * This file is part of Libav.
3 *
4 * Libav is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * Libav is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with Libav; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include "vaapi_decode.h"
20 #include "vp8.h"
21
22 static VASurfaceID vaapi_vp8_surface_id(VP8Frame *vf)
23 {
24 if (vf)
25 return ff_vaapi_get_surface_id(vf->tf.f);
26 else
27 return VA_INVALID_SURFACE;
28 }
29
30 static int vaapi_vp8_start_frame(AVCodecContext *avctx,
31 av_unused const uint8_t *buffer,
32 av_unused uint32_t size)
33 {
34 const VP8Context *s = avctx->priv_data;
35 VAAPIDecodePicture *pic = s->framep[VP56_FRAME_CURRENT]->hwaccel_picture_private;
36 VAPictureParameterBufferVP8 pp;
37 VAProbabilityDataBufferVP8 prob;
38 VAIQMatrixBufferVP8 quant;
39 int err, i, j, k;
40
41 pic->output_surface = vaapi_vp8_surface_id(s->framep[VP56_FRAME_CURRENT]);
42
43 pp = (VAPictureParameterBufferVP8) {
44 .frame_width = avctx->width,
45 .frame_height = avctx->height,
46
47 .last_ref_frame = vaapi_vp8_surface_id(s->framep[VP56_FRAME_PREVIOUS]),
48 .golden_ref_frame = vaapi_vp8_surface_id(s->framep[VP56_FRAME_GOLDEN]),
49 .alt_ref_frame = vaapi_vp8_surface_id(s->framep[VP56_FRAME_GOLDEN2]),
50 .out_of_loop_frame = VA_INVALID_SURFACE,
51
52 .pic_fields.bits = {
53 .key_frame = !s->keyframe,
54 .version = s->profile,
55
56 .segmentation_enabled = s->segmentation.enabled,
57 .update_mb_segmentation_map = s->segmentation.update_map,
58 .update_segment_feature_data = s->segmentation.update_feature_data,
59
60 .filter_type = s->filter.simple,
61 .sharpness_level = s->filter.sharpness,
62
63 .loop_filter_adj_enable = s->lf_delta.enabled,
64 .mode_ref_lf_delta_update = s->lf_delta.update,
65
66 .sign_bias_golden = s->sign_bias[VP56_FRAME_GOLDEN],
67 .sign_bias_alternate = s->sign_bias[VP56_FRAME_GOLDEN2],
68
69 .mb_no_coeff_skip = s->mbskip_enabled,
70 .loop_filter_disable = s->filter.level == 0,
71 },
72
73 .prob_skip_false = s->prob->mbskip,
74 .prob_intra = s->prob->intra,
75 .prob_last = s->prob->last,
76 .prob_gf = s->prob->golden,
77 };
78
79 for (i = 0; i < 3; i++)
80 pp.mb_segment_tree_probs[i] = s->prob->segmentid[i];
81
82 for (i = 0; i < 4; i++) {
83 if (s->segmentation.enabled) {
84 pp.loop_filter_level[i] = s->segmentation.filter_level[i];
85 if (!s->segmentation.absolute_vals)
86 pp.loop_filter_level[i] += s->filter.level;
87 } else {
88 pp.loop_filter_level[i] = s->filter.level;
89 }
90 pp.loop_filter_level[i] = av_clip_uintp2(pp.loop_filter_level[i], 6);
91 }
92
93 for (i = 0; i < 4; i++) {
94 pp.loop_filter_deltas_ref_frame[i] = s->lf_delta.ref[i];
95 pp.loop_filter_deltas_mode[i] = s->lf_delta.mode[i + 4];
96 }
97
98 if (s->keyframe) {
99 static const uint8_t keyframe_y_mode_probs[4] = {
100 145, 156, 163, 128
101 };
102 static const uint8_t keyframe_uv_mode_probs[3] = {
103 142, 114, 183
104 };
105 memcpy(pp.y_mode_probs, keyframe_y_mode_probs, 4);
106 memcpy(pp.uv_mode_probs, keyframe_uv_mode_probs, 3);
107 } else {
108 for (i = 0; i < 4; i++)
109 pp.y_mode_probs[i] = s->prob->pred16x16[i];
110 for (i = 0; i < 3; i++)
111 pp.uv_mode_probs[i] = s->prob->pred8x8c[i];
112 }
113 for (i = 0; i < 2; i++)
114 for (j = 0; j < 19; j++)
115 pp.mv_probs[i][j] = s->prob->mvc[i][j];
116
117 pp.bool_coder_ctx.range = s->coder_state_at_header_end.range;
118 pp.bool_coder_ctx.value = s->coder_state_at_header_end.value;
119 pp.bool_coder_ctx.count = s->coder_state_at_header_end.bit_count;
120
121 err = ff_vaapi_decode_make_param_buffer(avctx, pic,
122 VAPictureParameterBufferType,
123 &pp, sizeof(pp));
124 if (err < 0)
125 goto fail;
126
127 for (i = 0; i < 4; i++) {
128 for (j = 0; j < 8; j++) {
129 static const int coeff_bands_inverse[8] = {
130 0, 1, 2, 3, 5, 6, 4, 15
131 };
132 int coeff_pos = coeff_bands_inverse[j];
133
134 for (k = 0; k < 3; k++) {
135 memcpy(prob.dct_coeff_probs[i][j][k],
136 s->prob->token[i][coeff_pos][k], 11);
137 }
138 }
139 }
140
141 err = ff_vaapi_decode_make_param_buffer(avctx, pic,
142 VAProbabilityBufferType,
143 &prob, sizeof(prob));
144 if (err < 0)
145 goto fail;
146
147 for (i = 0; i < 4; i++) {
148 int base_qi = s->segmentation.base_quant[i];
149 if (!s->segmentation.absolute_vals)
150 base_qi += s->quant.yac_qi;
151
152 quant.quantization_index[i][0] = av_clip_uintp2(base_qi, 7);
153 quant.quantization_index[i][1] = av_clip_uintp2(base_qi + s->quant.ydc_delta, 7);
154 quant.quantization_index[i][2] = av_clip_uintp2(base_qi + s->quant.y2dc_delta, 7);
155 quant.quantization_index[i][3] = av_clip_uintp2(base_qi + s->quant.y2ac_delta, 7);
156 quant.quantization_index[i][4] = av_clip_uintp2(base_qi + s->quant.uvdc_delta, 7);
157 quant.quantization_index[i][5] = av_clip_uintp2(base_qi + s->quant.uvac_delta, 7);
158 }
159
160 err = ff_vaapi_decode_make_param_buffer(avctx, pic,
161 VAIQMatrixBufferType,
162 &quant, sizeof(quant));
163 if (err < 0)
164 goto fail;
165
166 return 0;
167
168 fail:
169 ff_vaapi_decode_cancel(avctx, pic);
170 return err;
171 }
172
173 static int vaapi_vp8_end_frame(AVCodecContext *avctx)
174 {
175 const VP8Context *s = avctx->priv_data;
176 VAAPIDecodePicture *pic = s->framep[VP56_FRAME_CURRENT]->hwaccel_picture_private;
177
178 return ff_vaapi_decode_issue(avctx, pic);
179 }
180
181 static int vaapi_vp8_decode_slice(AVCodecContext *avctx,
182 const uint8_t *buffer,
183 uint32_t size)
184 {
185 const VP8Context *s = avctx->priv_data;
186 VAAPIDecodePicture *pic = s->framep[VP56_FRAME_CURRENT]->hwaccel_picture_private;
187 VASliceParameterBufferVP8 sp;
188 int err, i;
189
190 unsigned int header_size = 3 + 7 * s->keyframe;
191 const uint8_t *data = buffer + header_size;
192 unsigned int data_size = size - header_size;
193
194 sp = (VASliceParameterBufferVP8) {
195 .slice_data_size = data_size,
196 .slice_data_offset = 0,
197 .slice_data_flag = VA_SLICE_DATA_FLAG_ALL,
198
199 .macroblock_offset = (8 * (s->coder_state_at_header_end.input - data) -
200 s->coder_state_at_header_end.bit_count - 8),
201 .num_of_partitions = s->num_coeff_partitions + 1,
202 };
203
204 sp.partition_size[0] = s->header_partition_size - ((sp.macroblock_offset + 7) / 8);
205 for (i = 0; i < 8; i++)
206 sp.partition_size[i+1] = s->coeff_partition_size[i];
207
208 err = ff_vaapi_decode_make_slice_buffer(avctx, pic, &sp, sizeof(sp), data, data_size);
209 if (err)
210 goto fail;
211
212 return 0;
213
214 fail:
215 ff_vaapi_decode_cancel(avctx, pic);
216 return err;
217 }
218
219 AVHWAccel ff_vp8_vaapi_hwaccel = {
220 .name = "vp8_vaapi",
221 .type = AVMEDIA_TYPE_VIDEO,
222 .id = AV_CODEC_ID_VP8,
223 .pix_fmt = AV_PIX_FMT_VAAPI,
224 .start_frame = &vaapi_vp8_start_frame,
225 .end_frame = &vaapi_vp8_end_frame,
226 .decode_slice = &vaapi_vp8_decode_slice,
227 .frame_priv_data_size = sizeof(VAAPIDecodePicture),
228 .init = &ff_vaapi_decode_init,
229 .uninit = &ff_vaapi_decode_uninit,
230 .priv_data_size = sizeof(VAAPIDecodeContext),
231 };