vaapi: Add VP9 hwaccell support
[ffmpeg.git] / libavcodec / vaapi_vp9.c
1 /*
2  * VP9 HW decode acceleration through VA API
3  *
4  * Copyright (C) 2015 Timo Rothenpieler <timo@rothenpieler.org>
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 "libavutil/pixdesc.h"
24 #include "vaapi_internal.h"
25 #include "vp9.h"
26
27 static void fill_picture_parameters(AVCodecContext                 *avctx,
28                                     const VP9SharedContext         *h,
29                                     VADecPictureParameterBufferVP9 *pp)
30 {
31     const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
32     int i;
33
34     pp->frame_width = avctx->width;
35     pp->frame_height = avctx->height;
36
37     pp->frame_header_length_in_bytes = h->h.uncompressed_header_size;
38     pp->first_partition_size = h->h.compressed_header_size;
39
40     pp->profile = h->h.profile;
41
42     pp->filter_level = h->h.filter.level;
43     pp->sharpness_level = h->h.filter.sharpness;
44     pp->log2_tile_rows = h->h.tiling.log2_tile_rows;
45     pp->log2_tile_columns = h->h.tiling.log2_tile_cols;
46
47     pp->pic_fields.bits.subsampling_x = pixdesc->log2_chroma_w;
48     pp->pic_fields.bits.subsampling_y = pixdesc->log2_chroma_h;
49     pp->pic_fields.bits.frame_type = !h->h.keyframe;
50     pp->pic_fields.bits.show_frame = !h->h.invisible;
51     pp->pic_fields.bits.error_resilient_mode = h->h.errorres;
52     pp->pic_fields.bits.intra_only = h->h.intraonly;
53     pp->pic_fields.bits.allow_high_precision_mv = h->h.keyframe ? 0 : h->h.highprecisionmvs;
54     pp->pic_fields.bits.mcomp_filter_type = h->h.filtermode ^ (h->h.filtermode <= 1);
55     pp->pic_fields.bits.frame_parallel_decoding_mode = h->h.parallelmode;
56     pp->pic_fields.bits.reset_frame_context = h->h.resetctx;
57     pp->pic_fields.bits.refresh_frame_context = h->h.refreshctx;
58     pp->pic_fields.bits.frame_context_idx = h->h.framectxid;
59
60     pp->pic_fields.bits.segmentation_enabled = h->h.segmentation.enabled;
61     pp->pic_fields.bits.segmentation_temporal_update = h->h.segmentation.temporal;
62     pp->pic_fields.bits.segmentation_update_map = h->h.segmentation.update_map;
63
64     pp->pic_fields.bits.last_ref_frame = h->h.refidx[0];
65     pp->pic_fields.bits.last_ref_frame_sign_bias = h->h.signbias[0];
66     pp->pic_fields.bits.golden_ref_frame = h->h.refidx[1];
67     pp->pic_fields.bits.golden_ref_frame_sign_bias = h->h.signbias[1];
68     pp->pic_fields.bits.alt_ref_frame = h->h.refidx[2];
69     pp->pic_fields.bits.alt_ref_frame_sign_bias = h->h.signbias[2];
70     pp->pic_fields.bits.lossless_flag = h->h.lossless;
71
72     for (i = 0; i < 7; i++)
73         pp->mb_segment_tree_probs[i] = h->h.segmentation.prob[i];
74
75     if (h->h.segmentation.temporal) {
76         for (i = 0; i < 3; i++)
77             pp->segment_pred_probs[i] = h->h.segmentation.pred_prob[i];
78     } else {
79         memset(pp->segment_pred_probs, 255, sizeof(pp->segment_pred_probs));
80     }
81
82     for (i = 0; i < 8; i++) {
83         if (h->refs[i].f->buf[0]) {
84             pp->reference_frames[i] = ff_vaapi_get_surface_id(h->refs[i].f);
85         } else {
86             pp->reference_frames[i] = VA_INVALID_ID;
87         }
88     }
89 }
90
91 static int vaapi_vp9_start_frame(AVCodecContext          *avctx,
92                                  av_unused const uint8_t *buffer,
93                                  av_unused uint32_t       size)
94 {
95     const VP9SharedContext *h = avctx->priv_data;
96     FFVAContext * const vactx = ff_vaapi_get_context(avctx);
97     VADecPictureParameterBufferVP9 *pic_param;
98
99     vactx->slice_param_size = sizeof(VASliceParameterBufferVP9);
100
101     pic_param = ff_vaapi_alloc_pic_param(vactx, sizeof(VADecPictureParameterBufferVP9));
102     if (!pic_param)
103         return -1;
104     fill_picture_parameters(avctx, h, pic_param);
105
106     return 0;
107 }
108
109 static int vaapi_vp9_end_frame(AVCodecContext *avctx)
110 {
111     FFVAContext * const vactx = ff_vaapi_get_context(avctx);
112     const VP9SharedContext *h = avctx->priv_data;
113     int ret;
114
115     ret = ff_vaapi_commit_slices(vactx);
116     if (ret < 0)
117         goto finish;
118
119     ret = ff_vaapi_render_picture(vactx, ff_vaapi_get_surface_id(h->frames[CUR_FRAME].tf.f));
120     if (ret < 0)
121         goto finish;
122
123 finish:
124     ff_vaapi_common_end_frame(avctx);
125     return ret;
126 }
127
128 static int vaapi_vp9_decode_slice(AVCodecContext *avctx,
129                                   const uint8_t  *buffer,
130                                   uint32_t        size)
131 {
132     FFVAContext * const vactx = ff_vaapi_get_context(avctx);
133     const VP9SharedContext *h = avctx->priv_data;
134     VASliceParameterBufferVP9 *slice_param;
135     int i;
136
137     slice_param = (VASliceParameterBufferVP9*)ff_vaapi_alloc_slice(vactx, buffer, size);
138     if (!slice_param)
139         return -1;
140
141     for (i = 0; i < 8; i++) {
142         slice_param->seg_param[i].segment_flags.fields.segment_reference_enabled = h->h.segmentation.feat[i].ref_enabled;
143         slice_param->seg_param[i].segment_flags.fields.segment_reference = h->h.segmentation.feat[i].ref_val;
144         slice_param->seg_param[i].segment_flags.fields.segment_reference_skipped = h->h.segmentation.feat[i].skip_enabled;
145
146         memcpy(slice_param->seg_param[i].filter_level, h->h.segmentation.feat[i].lflvl, sizeof(slice_param->seg_param[i].filter_level));
147
148         slice_param->seg_param[i].luma_dc_quant_scale = h->h.segmentation.feat[i].qmul[0][0];
149         slice_param->seg_param[i].luma_ac_quant_scale = h->h.segmentation.feat[i].qmul[0][1];
150         slice_param->seg_param[i].chroma_dc_quant_scale = h->h.segmentation.feat[i].qmul[1][0];
151         slice_param->seg_param[i].chroma_ac_quant_scale = h->h.segmentation.feat[i].qmul[1][1];
152     }
153
154     return 0;
155 }
156
157 AVHWAccel ff_vp9_vaapi_hwaccel = {
158     .name                 = "vp9_vaapi",
159     .type                 = AVMEDIA_TYPE_VIDEO,
160     .id                   = AV_CODEC_ID_VP9,
161     .pix_fmt              = AV_PIX_FMT_VAAPI,
162     .start_frame          = vaapi_vp9_start_frame,
163     .end_frame            = vaapi_vp9_end_frame,
164     .decode_slice         = vaapi_vp9_decode_slice,
165     .init                 = ff_vaapi_context_init,
166     .uninit               = ff_vaapi_context_fini,
167     .priv_data_size       = sizeof(FFVAContext),
168 };