17ec681f3Smrg/************************************************************************** 27ec681f3Smrg * 37ec681f3Smrg * Copyright 2014 Advanced Micro Devices, Inc. 47ec681f3Smrg * All Rights Reserved. 57ec681f3Smrg * 67ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a 77ec681f3Smrg * copy of this software and associated documentation files (the 87ec681f3Smrg * "Software"), to deal in the Software without restriction, including 97ec681f3Smrg * without limitation the rights to use, copy, modify, merge, publish, 107ec681f3Smrg * distribute, sub license, and/or sell copies of the Software, and to 117ec681f3Smrg * permit persons to whom the Software is furnished to do so, subject to 127ec681f3Smrg * the following conditions: 137ec681f3Smrg * 147ec681f3Smrg * The above copyright notice and this permission notice (including the 157ec681f3Smrg * next paragraph) shall be included in all copies or substantial portions 167ec681f3Smrg * of the Software. 177ec681f3Smrg * 187ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 197ec681f3Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 207ec681f3Smrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 217ec681f3Smrg * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR 227ec681f3Smrg * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 237ec681f3Smrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 247ec681f3Smrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 257ec681f3Smrg * 267ec681f3Smrg **************************************************************************/ 277ec681f3Smrg 287ec681f3Smrg#include "va_private.h" 297ec681f3Smrg 307ec681f3Smrgvoid vlVaHandlePictureParameterBufferMPEG4(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf) 317ec681f3Smrg{ 327ec681f3Smrg static const uint8_t default_intra_quant_matrix[64] = { 0 }; 337ec681f3Smrg static const uint8_t default_non_intra_quant_matrix[64] = { 0 }; 347ec681f3Smrg 357ec681f3Smrg VAPictureParameterBufferMPEG4 *mpeg4 = buf->data; 367ec681f3Smrg unsigned i; 377ec681f3Smrg 387ec681f3Smrg assert(buf->size >= sizeof(VAPictureParameterBufferMPEG4) && buf->num_elements == 1); 397ec681f3Smrg 407ec681f3Smrg context->mpeg4.pps = *mpeg4; 417ec681f3Smrg 427ec681f3Smrg /* vop_width */ 437ec681f3Smrg /* vop_height */ 447ec681f3Smrg /* forward_reference_picture */ 457ec681f3Smrg /* backward_reference_picture */ 467ec681f3Smrg context->desc.mpeg4.short_video_header = 477ec681f3Smrg mpeg4->vol_fields.bits.short_video_header; 487ec681f3Smrg /* chroma_format */ 497ec681f3Smrg context->desc.mpeg4.interlaced = mpeg4->vol_fields.bits.interlaced; 507ec681f3Smrg /* obmc_disable */ 517ec681f3Smrg /* sprite_enable */ 527ec681f3Smrg /* sprite_warping_accuracy */ 537ec681f3Smrg context->desc.mpeg4.quant_type = mpeg4->vol_fields.bits.quant_type; 547ec681f3Smrg context->desc.mpeg4.quarter_sample = mpeg4->vol_fields.bits.quarter_sample; 557ec681f3Smrg /* data_partitioned */ 567ec681f3Smrg /* reversible_vlc */ 577ec681f3Smrg context->desc.mpeg4.resync_marker_disable = 587ec681f3Smrg mpeg4->vol_fields.bits.resync_marker_disable; 597ec681f3Smrg /* no_of_sprite_warping_points */ 607ec681f3Smrg /* sprite_trajectory_du */ 617ec681f3Smrg /* sprite_trajectory_dv */ 627ec681f3Smrg /* quant_precision */ 637ec681f3Smrg context->desc.mpeg4.vop_coding_type = mpeg4->vop_fields.bits.vop_coding_type; 647ec681f3Smrg /* backward_reference_vop_coding_type */ 657ec681f3Smrg /* vop_rounding_type */ 667ec681f3Smrg /* intra_dc_vlc_thr */ 677ec681f3Smrg context->desc.mpeg4.top_field_first = 687ec681f3Smrg mpeg4->vop_fields.bits.top_field_first; 697ec681f3Smrg context->desc.mpeg4.alternate_vertical_scan_flag = 707ec681f3Smrg mpeg4->vop_fields.bits.alternate_vertical_scan_flag; 717ec681f3Smrg context->desc.mpeg4.vop_fcode_forward = mpeg4->vop_fcode_forward; 727ec681f3Smrg context->desc.mpeg4.vop_fcode_backward = mpeg4->vop_fcode_backward; 737ec681f3Smrg context->desc.mpeg4.vop_time_increment_resolution = 747ec681f3Smrg mpeg4->vop_time_increment_resolution; 757ec681f3Smrg /* num_gobs_in_vop */ 767ec681f3Smrg /* num_macroblocks_in_gob */ 777ec681f3Smrg context->desc.mpeg4.trb[0] = mpeg4->TRB; 787ec681f3Smrg context->desc.mpeg4.trb[1] = mpeg4->TRB; 797ec681f3Smrg context->desc.mpeg4.trd[0] = mpeg4->TRD; 807ec681f3Smrg context->desc.mpeg4.trd[1] = mpeg4->TRD; 817ec681f3Smrg 827ec681f3Smrg /* default [non-]intra quant matrix because mpv does not set these 837ec681f3Smrg matrices */ 847ec681f3Smrg if (!context->desc.mpeg4.intra_matrix) 857ec681f3Smrg context->desc.mpeg4.intra_matrix = default_intra_quant_matrix; 867ec681f3Smrg if (!context->desc.mpeg4.non_intra_matrix) 877ec681f3Smrg context->desc.mpeg4.non_intra_matrix = default_non_intra_quant_matrix; 887ec681f3Smrg 897ec681f3Smrg vlVaGetReferenceFrame(drv, mpeg4->forward_reference_picture, &context->desc.mpeg4.ref[0]); 907ec681f3Smrg vlVaGetReferenceFrame(drv, mpeg4->backward_reference_picture, &context->desc.mpeg4.ref[1]); 917ec681f3Smrg 927ec681f3Smrg context->mpeg4.vti_bits = 0; 937ec681f3Smrg for (i = context->desc.mpeg4.vop_time_increment_resolution; i > 0; i /= 2) 947ec681f3Smrg ++context->mpeg4.vti_bits; 957ec681f3Smrg} 967ec681f3Smrg 977ec681f3Smrgvoid vlVaHandleIQMatrixBufferMPEG4(vlVaContext *context, vlVaBuffer *buf) 987ec681f3Smrg{ 997ec681f3Smrg VAIQMatrixBufferMPEG4 *mpeg4 = buf->data; 1007ec681f3Smrg 1017ec681f3Smrg assert(buf->size >= sizeof(VAIQMatrixBufferMPEG4) && buf->num_elements == 1); 1027ec681f3Smrg if (mpeg4->load_intra_quant_mat) 1037ec681f3Smrg context->desc.mpeg4.intra_matrix = mpeg4->intra_quant_mat; 1047ec681f3Smrg else 1057ec681f3Smrg context->desc.mpeg4.intra_matrix = NULL; 1067ec681f3Smrg 1077ec681f3Smrg if (mpeg4->load_non_intra_quant_mat) 1087ec681f3Smrg context->desc.mpeg4.non_intra_matrix = mpeg4->non_intra_quant_mat; 1097ec681f3Smrg else 1107ec681f3Smrg context->desc.mpeg4.non_intra_matrix = NULL; 1117ec681f3Smrg} 1127ec681f3Smrg 1137ec681f3Smrgvoid vlVaHandleSliceParameterBufferMPEG4(vlVaContext *context, vlVaBuffer *buf) 1147ec681f3Smrg{ 1157ec681f3Smrg VASliceParameterBufferMPEG4 *mpeg4 = buf->data; 1167ec681f3Smrg 1177ec681f3Smrg assert(buf->size >= sizeof(VASliceParameterBufferMPEG4) && buf->num_elements == 1); 1187ec681f3Smrg context->mpeg4.quant_scale = mpeg4->quant_scale; 1197ec681f3Smrg} 1207ec681f3Smrg 1217ec681f3Smrgstruct bit_stream 1227ec681f3Smrg{ 1237ec681f3Smrg uint8_t *data; 1247ec681f3Smrg unsigned int length; /* bits */ 1257ec681f3Smrg unsigned int pos; /* bits */ 1267ec681f3Smrg}; 1277ec681f3Smrg 1287ec681f3Smrgstatic inline void 1297ec681f3Smrgwrite_bit(struct bit_stream *writer, unsigned int bit) 1307ec681f3Smrg{ 1317ec681f3Smrg assert(writer->length > (writer)->pos); 1327ec681f3Smrg writer->data[writer->pos>>3] |= ((bit & 1)<<(7 - (writer->pos & 7))); 1337ec681f3Smrg writer->pos++; 1347ec681f3Smrg} 1357ec681f3Smrg 1367ec681f3Smrgstatic inline void 1377ec681f3Smrgwrite_bits(struct bit_stream *writer, unsigned int bits, unsigned int len) 1387ec681f3Smrg{ 1397ec681f3Smrg int i; 1407ec681f3Smrg assert(len <= sizeof(bits)*8); 1417ec681f3Smrg for (i = len - 1; i >= 0; i--) 1427ec681f3Smrg write_bit(writer, bits>>i); 1437ec681f3Smrg} 1447ec681f3Smrg 1457ec681f3Smrgvoid vlVaDecoderFixMPEG4Startcode(vlVaContext *context) 1467ec681f3Smrg{ 1477ec681f3Smrg uint8_t vop[] = { 0x00, 0x00, 0x01, 0xb6, 0x00, 0x00, 0x00, 0x00, 0x00 }; 1487ec681f3Smrg struct bit_stream bs_vop = {vop, sizeof(vop)*8, 32}; 1497ec681f3Smrg unsigned int vop_time_inc; 1507ec681f3Smrg int mod_time; 1517ec681f3Smrg unsigned int vop_size; 1527ec681f3Smrg unsigned int vop_coding_type = context->desc.mpeg4.vop_coding_type; 1537ec681f3Smrg 1547ec681f3Smrg context->mpeg4.start_code_size = 0; 1557ec681f3Smrg memset(context->mpeg4.start_code, 0, sizeof(context->mpeg4.start_code)); 1567ec681f3Smrg if (vop_coding_type+1 == PIPE_MPEG12_PICTURE_CODING_TYPE_I) { 1577ec681f3Smrg unsigned int vop_time = context->mpeg4.frame_num/ 1587ec681f3Smrg context->desc.mpeg4.vop_time_increment_resolution; 1597ec681f3Smrg unsigned int vop_hour = vop_time / 3600; 1607ec681f3Smrg unsigned int vop_minute = (vop_time / 60) % 60; 1617ec681f3Smrg unsigned int vop_second = vop_time % 60; 1627ec681f3Smrg uint8_t group_of_vop[] = { 0x00, 0x00, 0x01, 0xb3, 0x00, 0x00, 0x00 }; 1637ec681f3Smrg struct bit_stream bs_gvop = {group_of_vop, sizeof(group_of_vop)*8, 32}; 1647ec681f3Smrg 1657ec681f3Smrg write_bits(&bs_gvop, vop_hour, 5); 1667ec681f3Smrg write_bits(&bs_gvop, vop_minute, 6); 1677ec681f3Smrg write_bit(&bs_gvop, 1); /* marker_bit */ 1687ec681f3Smrg write_bits(&bs_gvop, vop_second, 6); 1697ec681f3Smrg write_bit(&bs_gvop, 0); /* closed_gov */ /* TODO replace magic */ 1707ec681f3Smrg write_bit(&bs_gvop, 0); /* broken_link */ 1717ec681f3Smrg write_bit(&bs_gvop, 0); /* padding */ 1727ec681f3Smrg write_bits(&bs_gvop, 7, 3); /* padding */ 1737ec681f3Smrg 1747ec681f3Smrg memcpy(context->mpeg4.start_code, group_of_vop, sizeof(group_of_vop)); 1757ec681f3Smrg context->mpeg4.start_code_size += sizeof(group_of_vop); 1767ec681f3Smrg } 1777ec681f3Smrg 1787ec681f3Smrg write_bits(&bs_vop, vop_coding_type, 2); 1797ec681f3Smrg mod_time = context->mpeg4.frame_num % 1807ec681f3Smrg context->desc.mpeg4.vop_time_increment_resolution == 0 && 1817ec681f3Smrg vop_coding_type+1 != PIPE_MPEG12_PICTURE_CODING_TYPE_I; 1827ec681f3Smrg while (mod_time--) 1837ec681f3Smrg write_bit(&bs_vop, 1); /* modulo_time_base */ 1847ec681f3Smrg write_bit(&bs_vop, 0); /* modulo_time_base */ 1857ec681f3Smrg 1867ec681f3Smrg write_bit(&bs_vop, 1); /* marker_bit */ 1877ec681f3Smrg vop_time_inc = context->mpeg4.frame_num % 1887ec681f3Smrg context->desc.mpeg4.vop_time_increment_resolution; 1897ec681f3Smrg write_bits(&bs_vop, vop_time_inc, context->mpeg4.vti_bits); 1907ec681f3Smrg write_bit(&bs_vop, 1); /* marker_bit */ 1917ec681f3Smrg write_bit(&bs_vop, 1); /* vop_coded */ 1927ec681f3Smrg if (vop_coding_type+1 == PIPE_MPEG12_PICTURE_CODING_TYPE_P) 1937ec681f3Smrg write_bit(&bs_vop, context->mpeg4.pps.vop_fields.bits.vop_rounding_type); 1947ec681f3Smrg write_bits(&bs_vop, context->mpeg4.pps.vop_fields.bits.intra_dc_vlc_thr, 3); 1957ec681f3Smrg if (context->mpeg4.pps.vol_fields.bits.interlaced) { 1967ec681f3Smrg write_bit(&bs_vop, context->mpeg4.pps.vop_fields.bits.top_field_first); 1977ec681f3Smrg write_bit(&bs_vop, context->mpeg4.pps.vop_fields.bits.alternate_vertical_scan_flag); 1987ec681f3Smrg } 1997ec681f3Smrg 2007ec681f3Smrg write_bits(&bs_vop, context->mpeg4.quant_scale, context->mpeg4.pps.quant_precision); 2017ec681f3Smrg if (vop_coding_type+1 != PIPE_MPEG12_PICTURE_CODING_TYPE_I) 2027ec681f3Smrg write_bits(&bs_vop, context->desc.mpeg4.vop_fcode_forward, 3); 2037ec681f3Smrg if (vop_coding_type+1 == PIPE_MPEG12_PICTURE_CODING_TYPE_B) 2047ec681f3Smrg write_bits(&bs_vop, context->desc.mpeg4.vop_fcode_backward, 3); 2057ec681f3Smrg 2067ec681f3Smrg vop_size = bs_vop.pos/8; 2077ec681f3Smrg memcpy(context->mpeg4.start_code + context->mpeg4.start_code_size, vop, vop_size); 2087ec681f3Smrg context->mpeg4.start_code_size += vop_size; 2097ec681f3Smrg} 210