FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vaapi_vp9.c
Go to the documentation of this file.
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 
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 
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 
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 
120  if (ret < 0)
121  goto finish;
122 
123 finish:
125  return ret;
126 }
127 
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 
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 };
VASliceParameterBufferBase * ff_vaapi_alloc_slice(FFVAContext *vactx, const uint8_t *buffer, uint32_t size)
Allocate a new slice descriptor for the input slice.
Definition: vaapi.c:178
ThreadFrame tf
Definition: vp9.h:127
int ff_vaapi_context_fini(AVCodecContext *avctx)
Common AVHWAccel.uninit() implementation.
Definition: vaapi.c:66
uint8_t profile
Definition: vp9.h:139
uint8_t parallelmode
Definition: vp9.h:150
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2266
uint8_t update_map
Definition: vp9.h:175
int ff_vaapi_render_picture(FFVAContext *vactx, VASurfaceID surface)
Definition: vaapi.c:71
uint8_t temporal
Definition: vp9.h:173
AVFrame * f
Definition: thread.h:36
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:367
VP9BitstreamHeader h
Definition: vp9.h:202
uint8_t prob[7]
Definition: vp9.h:176
uint8_t framectxid
Definition: vp9.h:151
struct VP9BitstreamHeader::@150::@152 feat[MAX_SEGMENT]
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:92
unsigned log2_tile_rows
Definition: vp9.h:193
int uncompressed_header_size
Definition: vp9.h:197
enum FilterMode filtermode
Definition: vp9.h:147
uint8_t resetctx
Definition: vp9.h:144
uint8_t
unsigned int slice_param_size
Size of a slice parameter element.
VP9Frame frames[3]
Definition: vp9.h:208
uint8_t errorres
Definition: vp9.h:142
static void finish(void)
Definition: movenc.c:344
#define CUR_FRAME
Definition: vp9.h:205
uint8_t enabled
Definition: vp9.h:162
static FFVAContext * ff_vaapi_get_context(AVCodecContext *avctx)
Extract vaapi_context from an AVCodecContext.
ptrdiff_t size
Definition: opengl_enc.c:101
unsigned log2_tile_cols
Definition: vp9.h:193
uint8_t refidx[3]
Definition: vp9.h:153
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:101
void ff_vaapi_common_end_frame(AVCodecContext *avctx)
Common AVHWAccel.end_frame() implementation.
Definition: vaapi.c:209
static int vaapi_vp9_end_frame(AVCodecContext *avctx)
Definition: vaapi_vp9.c:109
int ff_vaapi_context_init(AVCodecContext *avctx)
Common AVHWAccel.init() implementation.
Definition: vaapi.c:45
uint8_t signbias[3]
Definition: vp9.h:154
uint8_t intraonly
Definition: vp9.h:143
uint8_t refreshctx
Definition: vp9.h:149
struct VP9BitstreamHeader::@150 segmentation
static void fill_picture_parameters(AVCodecContext *avctx, const VP9SharedContext *h, VADecPictureParameterBufferVP9 *pp)
Definition: vaapi_vp9.c:27
const char * name
Name of the hardware accelerated codec.
Definition: avcodec.h:3726
uint8_t keyframe
Definition: vp9.h:140
int width
picture width / height.
Definition: avcodec.h:1863
static VASurfaceID ff_vaapi_get_surface_id(AVFrame *pic)
Extract VASurfaceID from an AVFrame.
uint8_t level
Definition: vp9.h:158
int8_t sharpness
Definition: vp9.h:159
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
main external API structure.
Definition: avcodec.h:1676
struct VP9BitstreamHeader::@151 tiling
int ff_vaapi_commit_slices(FFVAContext *vactx)
Definition: vaapi.c:111
void * ff_vaapi_alloc_pic_param(FFVAContext *vactx, unsigned int size)
Allocate a new picture parameter buffer.
Definition: vaapi.c:163
struct AVCodecContext * avctx
Definition: mpegvideo.h:95
if(ret< 0)
Definition: vf_mcdeint.c:282
AVHWAccel ff_vp9_vaapi_hwaccel
Definition: vaapi_vp9.c:157
static int vaapi_vp9_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
Definition: vaapi_vp9.c:128
void * priv_data
Definition: avcodec.h:1718
uint8_t invisible
Definition: vp9.h:141
ThreadFrame refs[8]
Definition: vp9.h:204
uint8_t pred_prob[3]
Definition: vp9.h:177
struct VP9BitstreamHeader::@148 filter
uint8_t highprecisionmvs
Definition: vp9.h:146
uint8_t lossless
Definition: vp9.h:169
enum AVPixelFormat sw_pix_fmt
Nominal unaccelerated pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:3382
GLuint buffer
Definition: opengl_enc.c:102
#define av_unused
Definition: attributes.h:126
int compressed_header_size
Definition: vp9.h:198
static int vaapi_vp9_start_frame(AVCodecContext *avctx, av_unused const uint8_t *buffer, av_unused uint32_t size)
Definition: vaapi_vp9.c:91