You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
7.5KB

  1. /*
  2. * Video Decode and Presentation API for UNIX (VDPAU) is used for
  3. * HW decode acceleration for MPEG-1/2, H.264 and VC-1.
  4. *
  5. * Copyright (c) 2008 NVIDIA.
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include <limits.h>
  24. #include "avcodec.h"
  25. #include "h264.h"
  26. #undef NDEBUG
  27. #include <assert.h>
  28. #include "vdpau.h"
  29. #include "vdpau_internal.h"
  30. /**
  31. * \addtogroup VDPAU_Decoding
  32. *
  33. * @{
  34. */
  35. static void vdpau_h264_set_reference_frames(H264Context *h)
  36. {
  37. MpegEncContext * s = &h->s;
  38. struct vdpau_render_state * render, * render_ref;
  39. VdpReferenceFrameH264 * rf, * rf2;
  40. Picture * pic;
  41. int i, list, pic_frame_idx;
  42. render = (struct vdpau_render_state*)s->current_picture_ptr->data[0];
  43. assert(render);
  44. rf = &render->info.h264.referenceFrames[0];
  45. #define H264_RF_COUNT FF_ARRAY_ELEMS(render->info.h264.referenceFrames)
  46. for (list = 0; list < 2; ++list) {
  47. Picture **lp = list ? h->long_ref : h->short_ref;
  48. int ls = list ? h->long_ref_count : h->short_ref_count;
  49. for (i = 0; i < ls; ++i) {
  50. pic = lp[i];
  51. if (!pic || !pic->reference)
  52. continue;
  53. pic_frame_idx = pic->long_ref ? pic->pic_id : pic->frame_num;
  54. render_ref = (struct vdpau_render_state*)pic->data[0];
  55. assert(render_ref);
  56. rf2 = &render->info.h264.referenceFrames[0];
  57. while (rf2 != rf) {
  58. if (
  59. (rf2->surface == render_ref->surface)
  60. && (rf2->is_long_term == pic->long_ref)
  61. && (rf2->frame_idx == pic_frame_idx)
  62. )
  63. break;
  64. ++rf2;
  65. }
  66. if (rf2 != rf) {
  67. rf2->top_is_reference |= (pic->reference & PICT_TOP_FIELD) ? VDP_TRUE : VDP_FALSE;
  68. rf2->bottom_is_reference |= (pic->reference & PICT_BOTTOM_FIELD) ? VDP_TRUE : VDP_FALSE;
  69. continue;
  70. }
  71. if (rf >= &render->info.h264.referenceFrames[H264_RF_COUNT])
  72. continue;
  73. rf->surface = render_ref->surface;
  74. rf->is_long_term = pic->long_ref;
  75. rf->top_is_reference = (pic->reference & PICT_TOP_FIELD) ? VDP_TRUE : VDP_FALSE;
  76. rf->bottom_is_reference = (pic->reference & PICT_BOTTOM_FIELD) ? VDP_TRUE : VDP_FALSE;
  77. rf->field_order_cnt[0] = pic->field_poc[0];
  78. rf->field_order_cnt[1] = pic->field_poc[1];
  79. rf->frame_idx = pic_frame_idx;
  80. ++rf;
  81. }
  82. }
  83. for (; rf < &render->info.h264.referenceFrames[H264_RF_COUNT]; ++rf) {
  84. rf->surface = VDP_INVALID_HANDLE;
  85. rf->is_long_term = 0;
  86. rf->top_is_reference = 0;
  87. rf->bottom_is_reference = 0;
  88. rf->field_order_cnt[0] = 0;
  89. rf->field_order_cnt[1] = 0;
  90. rf->frame_idx = 0;
  91. }
  92. }
  93. void ff_vdpau_h264_add_data_chunk(H264Context *h, const uint8_t *buf, int buf_size)
  94. {
  95. MpegEncContext * s = &h->s;
  96. struct vdpau_render_state * render;
  97. render = (struct vdpau_render_state*)s->current_picture_ptr->data[0];
  98. assert(render);
  99. if (!render->bitstream_buffers_used)
  100. vdpau_h264_set_reference_frames(h);
  101. render->bitstream_buffers= av_fast_realloc(
  102. render->bitstream_buffers,
  103. &render->bitstream_buffers_allocated,
  104. sizeof(*render->bitstream_buffers)*(render->bitstream_buffers_used + 1)
  105. );
  106. render->bitstream_buffers[render->bitstream_buffers_used].struct_version = VDP_BITSTREAM_BUFFER_VERSION;
  107. render->bitstream_buffers[render->bitstream_buffers_used].bitstream = buf;
  108. render->bitstream_buffers[render->bitstream_buffers_used].bitstream_bytes = buf_size;
  109. render->bitstream_buffers_used++;
  110. }
  111. void ff_vdpau_h264_picture_complete(H264Context *h)
  112. {
  113. MpegEncContext * s = &h->s;
  114. struct vdpau_render_state * render;
  115. render = (struct vdpau_render_state*)s->current_picture_ptr->data[0];
  116. assert(render);
  117. render->info.h264.slice_count = h->slice_num;
  118. if (render->info.h264.slice_count < 1)
  119. return;
  120. for (int i = 0; i < 2; ++i) {
  121. int foc = s->current_picture_ptr->field_poc[i];
  122. if (foc == INT_MAX)
  123. foc = 0;
  124. render->info.h264.field_order_cnt[i] = foc;
  125. }
  126. render->info.h264.is_reference = s->current_picture_ptr->reference ? VDP_TRUE : VDP_FALSE;
  127. render->info.h264.frame_num = h->frame_num;
  128. render->info.h264.field_pic_flag = s->picture_structure != PICT_FRAME;
  129. render->info.h264.bottom_field_flag = s->picture_structure == PICT_BOTTOM_FIELD;
  130. render->info.h264.num_ref_frames = h->sps.ref_frame_count;
  131. render->info.h264.mb_adaptive_frame_field_flag = h->sps.mb_aff;
  132. render->info.h264.constrained_intra_pred_flag = h->pps.constrained_intra_pred;
  133. render->info.h264.weighted_pred_flag = h->pps.weighted_pred;
  134. render->info.h264.weighted_bipred_idc = h->pps.weighted_bipred_idc;
  135. render->info.h264.frame_mbs_only_flag = h->sps.frame_mbs_only_flag;
  136. render->info.h264.transform_8x8_mode_flag = h->pps.transform_8x8_mode;
  137. render->info.h264.chroma_qp_index_offset = h->pps.chroma_qp_index_offset[0];
  138. render->info.h264.second_chroma_qp_index_offset = h->pps.chroma_qp_index_offset[1];
  139. render->info.h264.pic_init_qp_minus26 = h->pps.init_qp - 26;
  140. render->info.h264.num_ref_idx_l0_active_minus1 = h->pps.ref_count[0] - 1;
  141. render->info.h264.num_ref_idx_l1_active_minus1 = h->pps.ref_count[1] - 1;
  142. render->info.h264.log2_max_frame_num_minus4 = h->sps.log2_max_frame_num - 4;
  143. render->info.h264.pic_order_cnt_type = h->sps.poc_type;
  144. render->info.h264.log2_max_pic_order_cnt_lsb_minus4 = h->sps.log2_max_poc_lsb - 4;
  145. render->info.h264.delta_pic_order_always_zero_flag = h->sps.delta_pic_order_always_zero_flag;
  146. render->info.h264.direct_8x8_inference_flag = h->sps.direct_8x8_inference_flag;
  147. render->info.h264.entropy_coding_mode_flag = h->pps.cabac;
  148. render->info.h264.pic_order_present_flag = h->pps.pic_order_present;
  149. render->info.h264.deblocking_filter_control_present_flag = h->pps.deblocking_filter_parameters_present;
  150. render->info.h264.redundant_pic_cnt_present_flag = h->pps.redundant_pic_cnt_present;
  151. memcpy(render->info.h264.scaling_lists_4x4, h->pps.scaling_matrix4, sizeof(render->info.h264.scaling_lists_4x4));
  152. memcpy(render->info.h264.scaling_lists_8x8, h->pps.scaling_matrix8, sizeof(render->info.h264.scaling_lists_8x8));
  153. ff_draw_horiz_band(s, 0, s->avctx->height);
  154. render->bitstream_buffers_used = 0;
  155. }
  156. /* @}*/