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.

110 lines
3.4KB

  1. /*
  2. * VDA H.264 hardware acceleration
  3. *
  4. * copyright (c) 2011 Sebastien Zwickert
  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. #include "h264.h"
  23. #include "h264data.h"
  24. #include "vda_internal.h"
  25. /* This structure is used to store the bitstream of the current frame. */
  26. struct vda_picture_context {
  27. uint8_t *bitstream;
  28. int bitstream_size;
  29. };
  30. static int start_frame(AVCodecContext *avctx,
  31. av_unused const uint8_t *buffer,
  32. av_unused uint32_t size)
  33. {
  34. const H264Context *h = avctx->priv_data;
  35. struct vda_context *vda_ctx = avctx->hwaccel_context;
  36. struct vda_picture_context *pic_ctx = h->s.current_picture_ptr->f.hwaccel_picture_private;
  37. if (!vda_ctx->decoder)
  38. return -1;
  39. pic_ctx->bitstream = NULL;
  40. pic_ctx->bitstream_size = 0;
  41. return 0;
  42. }
  43. static int decode_slice(AVCodecContext *avctx,
  44. const uint8_t *buffer,
  45. uint32_t size)
  46. {
  47. H264Context *h = avctx->priv_data;
  48. struct vda_context *vda_ctx = avctx->hwaccel_context;
  49. struct vda_picture_context *pic_ctx = h->s.current_picture_ptr->f.hwaccel_picture_private;
  50. void *tmp;
  51. if (!vda_ctx->decoder)
  52. return -1;
  53. tmp = av_realloc(pic_ctx->bitstream, pic_ctx->bitstream_size+size+4);
  54. if (!tmp)
  55. return AVERROR(ENOMEM);
  56. pic_ctx->bitstream = tmp;
  57. AV_WB32(pic_ctx->bitstream + pic_ctx->bitstream_size, size);
  58. memcpy(pic_ctx->bitstream + pic_ctx->bitstream_size + 4, buffer, size);
  59. pic_ctx->bitstream_size += size + 4;
  60. return 0;
  61. }
  62. static int end_frame(AVCodecContext *avctx)
  63. {
  64. H264Context *h = avctx->priv_data;
  65. struct vda_context *vda_ctx = avctx->hwaccel_context;
  66. struct vda_picture_context *pic_ctx = h->s.current_picture_ptr->f.hwaccel_picture_private;
  67. AVFrame *frame = &h->s.current_picture_ptr->f;
  68. int status;
  69. if (!vda_ctx->decoder || !pic_ctx->bitstream)
  70. return -1;
  71. status = ff_vda_decoder_decode(vda_ctx, pic_ctx->bitstream,
  72. pic_ctx->bitstream_size,
  73. frame->reordered_opaque);
  74. if (status)
  75. av_log(avctx, AV_LOG_ERROR, "Failed to decode frame (%d)\n", status);
  76. av_freep(&pic_ctx->bitstream);
  77. return status;
  78. }
  79. AVHWAccel ff_h264_vda_hwaccel = {
  80. .name = "h264_vda",
  81. .type = AVMEDIA_TYPE_VIDEO,
  82. .id = CODEC_ID_H264,
  83. .pix_fmt = PIX_FMT_VDA_VLD,
  84. .start_frame = start_frame,
  85. .decode_slice = decode_slice,
  86. .end_frame = end_frame,
  87. .priv_data_size = sizeof(struct vda_picture_context),
  88. };