Path: blob/21.2-virgl/src/gallium/frontends/va/picture_mpeg4.c
4561 views
/**************************************************************************1*2* Copyright 2014 Advanced Micro Devices, Inc.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/2627#include "va_private.h"2829void vlVaHandlePictureParameterBufferMPEG4(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf)30{31static const uint8_t default_intra_quant_matrix[64] = { 0 };32static const uint8_t default_non_intra_quant_matrix[64] = { 0 };3334VAPictureParameterBufferMPEG4 *mpeg4 = buf->data;35unsigned i;3637assert(buf->size >= sizeof(VAPictureParameterBufferMPEG4) && buf->num_elements == 1);3839context->mpeg4.pps = *mpeg4;4041/* vop_width */42/* vop_height */43/* forward_reference_picture */44/* backward_reference_picture */45context->desc.mpeg4.short_video_header =46mpeg4->vol_fields.bits.short_video_header;47/* chroma_format */48context->desc.mpeg4.interlaced = mpeg4->vol_fields.bits.interlaced;49/* obmc_disable */50/* sprite_enable */51/* sprite_warping_accuracy */52context->desc.mpeg4.quant_type = mpeg4->vol_fields.bits.quant_type;53context->desc.mpeg4.quarter_sample = mpeg4->vol_fields.bits.quarter_sample;54/* data_partitioned */55/* reversible_vlc */56context->desc.mpeg4.resync_marker_disable =57mpeg4->vol_fields.bits.resync_marker_disable;58/* no_of_sprite_warping_points */59/* sprite_trajectory_du */60/* sprite_trajectory_dv */61/* quant_precision */62context->desc.mpeg4.vop_coding_type = mpeg4->vop_fields.bits.vop_coding_type;63/* backward_reference_vop_coding_type */64/* vop_rounding_type */65/* intra_dc_vlc_thr */66context->desc.mpeg4.top_field_first =67mpeg4->vop_fields.bits.top_field_first;68context->desc.mpeg4.alternate_vertical_scan_flag =69mpeg4->vop_fields.bits.alternate_vertical_scan_flag;70context->desc.mpeg4.vop_fcode_forward = mpeg4->vop_fcode_forward;71context->desc.mpeg4.vop_fcode_backward = mpeg4->vop_fcode_backward;72context->desc.mpeg4.vop_time_increment_resolution =73mpeg4->vop_time_increment_resolution;74/* num_gobs_in_vop */75/* num_macroblocks_in_gob */76context->desc.mpeg4.trb[0] = mpeg4->TRB;77context->desc.mpeg4.trb[1] = mpeg4->TRB;78context->desc.mpeg4.trd[0] = mpeg4->TRD;79context->desc.mpeg4.trd[1] = mpeg4->TRD;8081/* default [non-]intra quant matrix because mpv does not set these82matrices */83if (!context->desc.mpeg4.intra_matrix)84context->desc.mpeg4.intra_matrix = default_intra_quant_matrix;85if (!context->desc.mpeg4.non_intra_matrix)86context->desc.mpeg4.non_intra_matrix = default_non_intra_quant_matrix;8788vlVaGetReferenceFrame(drv, mpeg4->forward_reference_picture, &context->desc.mpeg4.ref[0]);89vlVaGetReferenceFrame(drv, mpeg4->backward_reference_picture, &context->desc.mpeg4.ref[1]);9091context->mpeg4.vti_bits = 0;92for (i = context->desc.mpeg4.vop_time_increment_resolution; i > 0; i /= 2)93++context->mpeg4.vti_bits;94}9596void vlVaHandleIQMatrixBufferMPEG4(vlVaContext *context, vlVaBuffer *buf)97{98VAIQMatrixBufferMPEG4 *mpeg4 = buf->data;99100assert(buf->size >= sizeof(VAIQMatrixBufferMPEG4) && buf->num_elements == 1);101if (mpeg4->load_intra_quant_mat)102context->desc.mpeg4.intra_matrix = mpeg4->intra_quant_mat;103else104context->desc.mpeg4.intra_matrix = NULL;105106if (mpeg4->load_non_intra_quant_mat)107context->desc.mpeg4.non_intra_matrix = mpeg4->non_intra_quant_mat;108else109context->desc.mpeg4.non_intra_matrix = NULL;110}111112void vlVaHandleSliceParameterBufferMPEG4(vlVaContext *context, vlVaBuffer *buf)113{114VASliceParameterBufferMPEG4 *mpeg4 = buf->data;115116assert(buf->size >= sizeof(VASliceParameterBufferMPEG4) && buf->num_elements == 1);117context->mpeg4.quant_scale = mpeg4->quant_scale;118}119120struct bit_stream121{122uint8_t *data;123unsigned int length; /* bits */124unsigned int pos; /* bits */125};126127static inline void128write_bit(struct bit_stream *writer, unsigned int bit)129{130assert(writer->length > (writer)->pos);131writer->data[writer->pos>>3] |= ((bit & 1)<<(7 - (writer->pos & 7)));132writer->pos++;133}134135static inline void136write_bits(struct bit_stream *writer, unsigned int bits, unsigned int len)137{138int i;139assert(len <= sizeof(bits)*8);140for (i = len - 1; i >= 0; i--)141write_bit(writer, bits>>i);142}143144void vlVaDecoderFixMPEG4Startcode(vlVaContext *context)145{146uint8_t vop[] = { 0x00, 0x00, 0x01, 0xb6, 0x00, 0x00, 0x00, 0x00, 0x00 };147struct bit_stream bs_vop = {vop, sizeof(vop)*8, 32};148unsigned int vop_time_inc;149int mod_time;150unsigned int vop_size;151unsigned int vop_coding_type = context->desc.mpeg4.vop_coding_type;152153context->mpeg4.start_code_size = 0;154memset(context->mpeg4.start_code, 0, sizeof(context->mpeg4.start_code));155if (vop_coding_type+1 == PIPE_MPEG12_PICTURE_CODING_TYPE_I) {156unsigned int vop_time = context->mpeg4.frame_num/157context->desc.mpeg4.vop_time_increment_resolution;158unsigned int vop_hour = vop_time / 3600;159unsigned int vop_minute = (vop_time / 60) % 60;160unsigned int vop_second = vop_time % 60;161uint8_t group_of_vop[] = { 0x00, 0x00, 0x01, 0xb3, 0x00, 0x00, 0x00 };162struct bit_stream bs_gvop = {group_of_vop, sizeof(group_of_vop)*8, 32};163164write_bits(&bs_gvop, vop_hour, 5);165write_bits(&bs_gvop, vop_minute, 6);166write_bit(&bs_gvop, 1); /* marker_bit */167write_bits(&bs_gvop, vop_second, 6);168write_bit(&bs_gvop, 0); /* closed_gov */ /* TODO replace magic */169write_bit(&bs_gvop, 0); /* broken_link */170write_bit(&bs_gvop, 0); /* padding */171write_bits(&bs_gvop, 7, 3); /* padding */172173memcpy(context->mpeg4.start_code, group_of_vop, sizeof(group_of_vop));174context->mpeg4.start_code_size += sizeof(group_of_vop);175}176177write_bits(&bs_vop, vop_coding_type, 2);178mod_time = context->mpeg4.frame_num %179context->desc.mpeg4.vop_time_increment_resolution == 0 &&180vop_coding_type+1 != PIPE_MPEG12_PICTURE_CODING_TYPE_I;181while (mod_time--)182write_bit(&bs_vop, 1); /* modulo_time_base */183write_bit(&bs_vop, 0); /* modulo_time_base */184185write_bit(&bs_vop, 1); /* marker_bit */186vop_time_inc = context->mpeg4.frame_num %187context->desc.mpeg4.vop_time_increment_resolution;188write_bits(&bs_vop, vop_time_inc, context->mpeg4.vti_bits);189write_bit(&bs_vop, 1); /* marker_bit */190write_bit(&bs_vop, 1); /* vop_coded */191if (vop_coding_type+1 == PIPE_MPEG12_PICTURE_CODING_TYPE_P)192write_bit(&bs_vop, context->mpeg4.pps.vop_fields.bits.vop_rounding_type);193write_bits(&bs_vop, context->mpeg4.pps.vop_fields.bits.intra_dc_vlc_thr, 3);194if (context->mpeg4.pps.vol_fields.bits.interlaced) {195write_bit(&bs_vop, context->mpeg4.pps.vop_fields.bits.top_field_first);196write_bit(&bs_vop, context->mpeg4.pps.vop_fields.bits.alternate_vertical_scan_flag);197}198199write_bits(&bs_vop, context->mpeg4.quant_scale, context->mpeg4.pps.quant_precision);200if (vop_coding_type+1 != PIPE_MPEG12_PICTURE_CODING_TYPE_I)201write_bits(&bs_vop, context->desc.mpeg4.vop_fcode_forward, 3);202if (vop_coding_type+1 == PIPE_MPEG12_PICTURE_CODING_TYPE_B)203write_bits(&bs_vop, context->desc.mpeg4.vop_fcode_backward, 3);204205vop_size = bs_vop.pos/8;206memcpy(context->mpeg4.start_code + context->mpeg4.start_code_size, vop, vop_size);207context->mpeg4.start_code_size += vop_size;208}209210211