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.

153 lines
5.3KB

  1. /*
  2. * MPEG-1/2 HW decode acceleration through VDPAU
  3. *
  4. * Copyright (c) 2008 NVIDIA
  5. * Copyright (c) 2013 Rémi Denis-Courmont
  6. *
  7. * This file is part of Libav.
  8. *
  9. * Libav 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. * Libav 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 Libav; if not, write to the Free Software Foundation,
  21. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include <vdpau/vdpau.h>
  24. #include "avcodec.h"
  25. #include "mpegvideo.h"
  26. #include "vdpau.h"
  27. #include "vdpau_internal.h"
  28. static int vdpau_mpeg_start_frame(AVCodecContext *avctx,
  29. const uint8_t *buffer, uint32_t size)
  30. {
  31. MpegEncContext * const s = avctx->priv_data;
  32. Picture *pic = s->current_picture_ptr;
  33. struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
  34. VdpPictureInfoMPEG1Or2 *info = &pic_ctx->info.mpeg;
  35. VdpVideoSurface ref;
  36. int i;
  37. /* fill VdpPictureInfoMPEG1Or2 struct */
  38. info->forward_reference = VDP_INVALID_HANDLE;
  39. info->backward_reference = VDP_INVALID_HANDLE;
  40. switch (s->pict_type) {
  41. case AV_PICTURE_TYPE_B:
  42. ref = ff_vdpau_get_surface_id(s->next_picture.f);
  43. assert(ref != VDP_INVALID_HANDLE);
  44. info->backward_reference = ref;
  45. /* fall through to forward prediction */
  46. case AV_PICTURE_TYPE_P:
  47. ref = ff_vdpau_get_surface_id(s->last_picture.f);
  48. info->forward_reference = ref;
  49. }
  50. info->slice_count = 0;
  51. info->picture_structure = s->picture_structure;
  52. info->picture_coding_type = s->pict_type;
  53. info->intra_dc_precision = s->intra_dc_precision;
  54. info->frame_pred_frame_dct = s->frame_pred_frame_dct;
  55. info->concealment_motion_vectors = s->concealment_motion_vectors;
  56. info->intra_vlc_format = s->intra_vlc_format;
  57. info->alternate_scan = s->alternate_scan;
  58. info->q_scale_type = s->q_scale_type;
  59. info->top_field_first = s->top_field_first;
  60. // Both for MPEG-1 only, zero for MPEG-2:
  61. info->full_pel_forward_vector = s->full_pel[0];
  62. info->full_pel_backward_vector = s->full_pel[1];
  63. // For MPEG-1 fill both horizontal & vertical:
  64. info->f_code[0][0] = s->mpeg_f_code[0][0];
  65. info->f_code[0][1] = s->mpeg_f_code[0][1];
  66. info->f_code[1][0] = s->mpeg_f_code[1][0];
  67. info->f_code[1][1] = s->mpeg_f_code[1][1];
  68. for (i = 0; i < 64; ++i) {
  69. info->intra_quantizer_matrix[i] = s->intra_matrix[i];
  70. info->non_intra_quantizer_matrix[i] = s->inter_matrix[i];
  71. }
  72. return ff_vdpau_common_start_frame(pic_ctx, buffer, size);
  73. }
  74. static int vdpau_mpeg_decode_slice(AVCodecContext *avctx,
  75. const uint8_t *buffer, uint32_t size)
  76. {
  77. MpegEncContext * const s = avctx->priv_data;
  78. Picture *pic = s->current_picture_ptr;
  79. struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
  80. int val;
  81. val = ff_vdpau_add_buffer(pic_ctx, buffer, size);
  82. if (val < 0)
  83. return val;
  84. pic_ctx->info.mpeg.slice_count++;
  85. return 0;
  86. }
  87. #if CONFIG_MPEG1_VDPAU_HWACCEL
  88. static int vdpau_mpeg1_init(AVCodecContext *avctx)
  89. {
  90. return ff_vdpau_common_init(avctx, VDP_DECODER_PROFILE_MPEG1,
  91. VDP_DECODER_LEVEL_MPEG1_NA);
  92. }
  93. AVHWAccel ff_mpeg1_vdpau_hwaccel = {
  94. .name = "mpeg1_vdpau",
  95. .type = AVMEDIA_TYPE_VIDEO,
  96. .id = AV_CODEC_ID_MPEG1VIDEO,
  97. .pix_fmt = AV_PIX_FMT_VDPAU,
  98. .start_frame = vdpau_mpeg_start_frame,
  99. .end_frame = ff_vdpau_mpeg_end_frame,
  100. .decode_slice = vdpau_mpeg_decode_slice,
  101. .frame_priv_data_size = sizeof(struct vdpau_picture_context),
  102. .init = vdpau_mpeg1_init,
  103. .uninit = ff_vdpau_common_uninit,
  104. .priv_data_size = sizeof(VDPAUContext),
  105. };
  106. #endif
  107. #if CONFIG_MPEG2_VDPAU_HWACCEL
  108. static int vdpau_mpeg2_init(AVCodecContext *avctx)
  109. {
  110. VdpDecoderProfile profile;
  111. switch (avctx->profile) {
  112. case FF_PROFILE_MPEG2_MAIN:
  113. profile = VDP_DECODER_PROFILE_MPEG2_MAIN;
  114. break;
  115. case FF_PROFILE_MPEG2_SIMPLE:
  116. profile = VDP_DECODER_PROFILE_MPEG2_SIMPLE;
  117. break;
  118. default:
  119. return AVERROR(EINVAL);
  120. }
  121. return ff_vdpau_common_init(avctx, profile, VDP_DECODER_LEVEL_MPEG2_HL);
  122. }
  123. AVHWAccel ff_mpeg2_vdpau_hwaccel = {
  124. .name = "mpeg2_vdpau",
  125. .type = AVMEDIA_TYPE_VIDEO,
  126. .id = AV_CODEC_ID_MPEG2VIDEO,
  127. .pix_fmt = AV_PIX_FMT_VDPAU,
  128. .start_frame = vdpau_mpeg_start_frame,
  129. .end_frame = ff_vdpau_mpeg_end_frame,
  130. .decode_slice = vdpau_mpeg_decode_slice,
  131. .frame_priv_data_size = sizeof(struct vdpau_picture_context),
  132. .init = vdpau_mpeg2_init,
  133. .uninit = ff_vdpau_common_uninit,
  134. .priv_data_size = sizeof(VDPAUContext),
  135. };
  136. #endif