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.

108 lines
3.3KB

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