Libav
vaapi_mpeg4.c
Go to the documentation of this file.
1 /*
2  * MPEG-4 / H.263 HW decode acceleration through VA API
3  *
4  * Copyright (C) 2008-2009 Splitted-Desktop Systems
5  *
6  * This file is part of Libav.
7  *
8  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include "vaapi_internal.h"
24 #include "h263.h"
25 #include "mpeg4video.h"
26 
29 {
30  switch (s->intra_dc_threshold) {
31  case 99: return 0;
32  case 13: return 1;
33  case 15: return 2;
34  case 17: return 3;
35  case 19: return 4;
36  case 21: return 5;
37  case 23: return 6;
38  case 0: return 7;
39  }
40  return 0;
41 }
42 
44 {
45  Mpeg4DecContext *ctx = avctx->priv_data;
46  MpegEncContext * const s = &ctx->m;
47  struct vaapi_context * const vactx = avctx->hwaccel_context;
48  VAPictureParameterBufferMPEG4 *pic_param;
49  VAIQMatrixBufferMPEG4 *iq_matrix;
50  int i;
51 
52  av_dlog(avctx, "vaapi_mpeg4_start_frame()\n");
53 
54  vactx->slice_param_size = sizeof(VASliceParameterBufferMPEG4);
55 
56  /* Fill in VAPictureParameterBufferMPEG4 */
57  pic_param = ff_vaapi_alloc_pic_param(vactx, sizeof(VAPictureParameterBufferMPEG4));
58  if (!pic_param)
59  return -1;
60  pic_param->vop_width = s->width;
61  pic_param->vop_height = s->height;
62  pic_param->forward_reference_picture = VA_INVALID_ID;
63  pic_param->backward_reference_picture = VA_INVALID_ID;
64  pic_param->vol_fields.value = 0; /* reset all bits */
65  pic_param->vol_fields.bits.short_video_header = avctx->codec->id == AV_CODEC_ID_H263;
66  pic_param->vol_fields.bits.chroma_format = CHROMA_420;
67  pic_param->vol_fields.bits.interlaced = !s->progressive_sequence;
68  pic_param->vol_fields.bits.obmc_disable = 1;
69  pic_param->vol_fields.bits.sprite_enable = ctx->vol_sprite_usage;
70  pic_param->vol_fields.bits.sprite_warping_accuracy = s->sprite_warping_accuracy;
71  pic_param->vol_fields.bits.quant_type = s->mpeg_quant;
72  pic_param->vol_fields.bits.quarter_sample = s->quarter_sample;
73  pic_param->vol_fields.bits.data_partitioned = s->data_partitioning;
74  pic_param->vol_fields.bits.reversible_vlc = ctx->rvlc;
75  pic_param->vol_fields.bits.resync_marker_disable = !ctx->resync_marker;
76  pic_param->no_of_sprite_warping_points = ctx->num_sprite_warping_points;
77  for (i = 0; i < ctx->num_sprite_warping_points && i < 3; i++) {
78  pic_param->sprite_trajectory_du[i] = ctx->sprite_traj[i][0];
79  pic_param->sprite_trajectory_dv[i] = ctx->sprite_traj[i][1];
80  }
81  pic_param->quant_precision = s->quant_precision;
82  pic_param->vop_fields.value = 0; /* reset all bits */
83  pic_param->vop_fields.bits.vop_coding_type = s->pict_type - AV_PICTURE_TYPE_I;
84  pic_param->vop_fields.bits.backward_reference_vop_coding_type = s->pict_type == AV_PICTURE_TYPE_B ? s->next_picture.f->pict_type - AV_PICTURE_TYPE_I : 0;
85  pic_param->vop_fields.bits.vop_rounding_type = s->no_rounding;
86  pic_param->vop_fields.bits.intra_dc_vlc_thr = mpeg4_get_intra_dc_vlc_thr(ctx);
87  pic_param->vop_fields.bits.top_field_first = s->top_field_first;
88  pic_param->vop_fields.bits.alternate_vertical_scan_flag = s->alternate_scan;
89  pic_param->vop_fcode_forward = s->f_code;
90  pic_param->vop_fcode_backward = s->b_code;
91  pic_param->vop_time_increment_resolution = avctx->time_base.den;
92  pic_param->num_macroblocks_in_gob = s->mb_width * ff_h263_get_gob_height(s);
93  pic_param->num_gobs_in_vop = (s->mb_width * s->mb_height) / pic_param->num_macroblocks_in_gob;
94  pic_param->TRB = s->pb_time;
95  pic_param->TRD = s->pp_time;
96 
98  pic_param->backward_reference_picture = ff_vaapi_get_surface_id(s->next_picture.f);
99  if (s->pict_type != AV_PICTURE_TYPE_I)
100  pic_param->forward_reference_picture = ff_vaapi_get_surface_id(s->last_picture.f);
101 
102  /* Fill in VAIQMatrixBufferMPEG4 */
103  /* Only the first inverse quantisation method uses the weighting matrices */
104  if (pic_param->vol_fields.bits.quant_type) {
105  iq_matrix = ff_vaapi_alloc_iq_matrix(vactx, sizeof(VAIQMatrixBufferMPEG4));
106  if (!iq_matrix)
107  return -1;
108  iq_matrix->load_intra_quant_mat = 1;
109  iq_matrix->load_non_intra_quant_mat = 1;
110 
111  for (i = 0; i < 64; i++) {
112  int n = s->idsp.idct_permutation[ff_zigzag_direct[i]];
113  iq_matrix->intra_quant_mat[i] = s->intra_matrix[n];
114  iq_matrix->non_intra_quant_mat[i] = s->inter_matrix[n];
115  }
116  }
117  return 0;
118 }
119 
120 static int vaapi_mpeg4_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
121 {
122  MpegEncContext * const s = avctx->priv_data;
123  VASliceParameterBufferMPEG4 *slice_param;
124 
125  av_dlog(avctx, "vaapi_mpeg4_decode_slice(): buffer %p, size %d\n", buffer, size);
126 
127  /* video_plane_with_short_video_header() contains all GOBs
128  * in-order, and this is what VA API (Intel backend) expects: only
129  * a single slice param. So fake macroblock_number for Libav so
130  * that we don't call vaapi_mpeg4_decode_slice() again
131  */
132  if (avctx->codec->id == AV_CODEC_ID_H263)
133  size = s->gb.buffer_end - buffer;
134 
135  /* Fill in VASliceParameterBufferMPEG4 */
136  slice_param = (VASliceParameterBufferMPEG4 *)ff_vaapi_alloc_slice(avctx->hwaccel_context, buffer, size);
137  if (!slice_param)
138  return -1;
139  slice_param->macroblock_offset = get_bits_count(&s->gb) % 8;
140  slice_param->macroblock_number = s->mb_y * s->mb_width + s->mb_x;
141  slice_param->quant_scale = s->qscale;
142 
143  if (avctx->codec->id == AV_CODEC_ID_H263)
144  s->mb_y = s->mb_height;
145 
146  return 0;
147 }
148 
149 #if CONFIG_MPEG4_VAAPI_HWACCEL
150 AVHWAccel ff_mpeg4_vaapi_hwaccel = {
151  .name = "mpeg4_vaapi",
152  .type = AVMEDIA_TYPE_VIDEO,
153  .id = AV_CODEC_ID_MPEG4,
154  .pix_fmt = AV_PIX_FMT_VAAPI_VLD,
155  .start_frame = vaapi_mpeg4_start_frame,
156  .end_frame = ff_vaapi_mpeg_end_frame,
157  .decode_slice = vaapi_mpeg4_decode_slice,
158 };
159 #endif
160 
161 #if CONFIG_H263_VAAPI_HWACCEL
162 AVHWAccel ff_h263_vaapi_hwaccel = {
163  .name = "h263_vaapi",
164  .type = AVMEDIA_TYPE_VIDEO,
165  .id = AV_CODEC_ID_H263,
166  .pix_fmt = AV_PIX_FMT_VAAPI_VLD,
167  .start_frame = vaapi_mpeg4_start_frame,
168  .end_frame = ff_vaapi_mpeg_end_frame,
169  .decode_slice = vaapi_mpeg4_decode_slice,
170 };
171 #endif
uint16_t sprite_traj[4][2]
sprite trajectory points
Definition: mpeg4video.h:72
IDCTDSPContext idsp
Definition: mpegvideo.h:354
const struct AVCodec * codec
Definition: avcodec.h:1059
int size
int sprite_warping_accuracy
Definition: mpegvideo.h:516
int resync_marker
could this stream contain resync markers
Definition: mpeg4video.h:79
HW decoding through VA API, Picture.data[3] contains a vaapi_render_state struct which contains the b...
Definition: pixfmt.h:127
av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (%s)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic?ac->func_descr_generic:ac->func_descr)
This structure is used to share data between the Libav library and the client video application...
Definition: vaapi.h:50
int qscale
QP.
Definition: mpegvideo.h:332
int quant_precision
Definition: mpegvideo.h:513
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1175
int vol_sprite_usage
Definition: mpeg4video.h:68
uint8_t
void * hwaccel_context
Hardware accelerator context.
Definition: avcodec.h:2460
int no_rounding
apply no rounding to motion compensation (MPEG4, msmpeg4, ...) for b-frames rounding mode is always 0...
Definition: mpegvideo.h:406
#define CHROMA_420
Definition: mpegvideo.h:582
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:194
uint16_t pp_time
time distance between the last 2 p,s,i frames
Definition: mpegvideo.h:505
int mb_height
number of MBs horizontally & vertically
Definition: mpegvideo.h:255
int intra_dc_threshold
QP above which the ac VLC should be used for intra dc.
Definition: mpeg4video.h:88
MpegEncContext m
Definition: mpeg4video.h:63
enum AVCodecID id
Definition: avcodec.h:2826
static int vaapi_mpeg4_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
Definition: vaapi_mpeg4.c:120
void * ff_vaapi_alloc_pic_param(struct vaapi_context *vactx, unsigned int size)
Allocate a new picture parameter buffer.
Definition: vaapi.c:133
int quarter_sample
1->qpel, 0->half pel ME/MC
Definition: mpegvideo.h:514
GetBitContext gb
Definition: mpegvideo.h:558
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:196
int top_field_first
Definition: mpegvideo.h:574
const char * name
Name of the hardware accelerated codec.
Definition: avcodec.h:2915
int ff_h263_get_gob_height(MpegEncContext *s)
Get the GOB height based on picture height.
Definition: h263.c:375
int alternate_scan
Definition: mpegvideo.h:578
static VASurfaceID ff_vaapi_get_surface_id(AVFrame *pic)
Extract VASurfaceID from an AVFrame.
unsigned int slice_param_size
Size of a VASliceParameterBuffer element.
Definition: vaapi.h:137
static char buffer[20]
Definition: seek-test.c:31
uint8_t idct_permutation[64]
IDCT input permutation.
Definition: idctdsp.h:94
if(ac->has_optimized_func)
uint16_t inter_matrix[64]
Definition: mpegvideo.h:424
AVHWAccel.
Definition: avcodec.h:2909
main external API structure.
Definition: avcodec.h:1050
int height
picture size. must be a multiple of 16
Definition: mpegvideo.h:223
VASliceParameterBufferBase * ff_vaapi_alloc_slice(struct vaapi_context *vactx, const uint8_t *buffer, uint32_t size)
Allocate a new slice descriptor for the input slice.
Definition: vaapi.c:148
int data_partitioning
data partitioning flag from header
Definition: mpegvideo.h:517
int progressive_sequence
Definition: mpegvideo.h:566
struct AVFrame * f
Definition: mpegvideo.h:100
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:115
int f_code
forward MV resolution
Definition: mpegvideo.h:362
int pict_type
AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
Definition: mpegvideo.h:339
static int mpeg4_get_intra_dc_vlc_thr(Mpeg4DecContext *s)
Reconstruct bitstream intra_dc_vlc_thr.
Definition: vaapi_mpeg4.c:28
MpegEncContext.
Definition: mpegvideo.h:204
static int vaapi_mpeg4_start_frame(AVCodecContext *avctx, av_unused const uint8_t *buffer, av_unused uint32_t size)
Definition: vaapi_mpeg4.c:43
const uint8_t * buffer_end
Definition: get_bits.h:54
Picture last_picture
copy of the previous picture structure.
Definition: mpegvideo.h:288
void * ff_vaapi_alloc_iq_matrix(struct vaapi_context *vactx, unsigned int size)
Allocate a new IQ matrix buffer.
Definition: vaapi.c:138
Bi-dir predicted.
Definition: avutil.h:255
int den
denominator
Definition: rational.h:45
void * priv_data
Definition: avcodec.h:1092
Picture next_picture
copy of the next picture structure.
Definition: mpegvideo.h:294
uint16_t intra_matrix[64]
matrix transmitted in the bitstream
Definition: mpegvideo.h:422
int ff_vaapi_mpeg_end_frame(AVCodecContext *avctx)
int num_sprite_warping_points
Definition: mpeg4video.h:70
int b_code
backward MV resolution for B Frames (mpeg4)
Definition: mpegvideo.h:363
#define av_unused
Definition: attributes.h:86
uint16_t pb_time
time distance between the last b and p,s,i frame
Definition: mpegvideo.h:506