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.

151 lines
4.8KB

  1. /*
  2. * Copyright (c) 2010, Google, Inc.
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * Libav is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * VP8 decoder support via libvpx
  23. */
  24. #define VPX_CODEC_DISABLE_COMPAT 1
  25. #include <vpx/vpx_decoder.h>
  26. #include <vpx/vp8dx.h>
  27. #include "libavutil/common.h"
  28. #include "libavutil/imgutils.h"
  29. #include "avcodec.h"
  30. #include "internal.h"
  31. #include "libvpx.h"
  32. typedef struct VP8DecoderContext {
  33. struct vpx_codec_ctx decoder;
  34. } VP8Context;
  35. static av_cold int vpx_init(AVCodecContext *avctx,
  36. const struct vpx_codec_iface *iface)
  37. {
  38. VP8Context *ctx = avctx->priv_data;
  39. struct vpx_codec_dec_cfg deccfg = {
  40. /* token partitions+1 would be a decent choice */
  41. .threads = FFMIN(avctx->thread_count, 16)
  42. };
  43. av_log(avctx, AV_LOG_INFO, "%s\n", vpx_codec_version_str());
  44. av_log(avctx, AV_LOG_VERBOSE, "%s\n", vpx_codec_build_config());
  45. if (vpx_codec_dec_init(&ctx->decoder, iface, &deccfg, 0) != VPX_CODEC_OK) {
  46. const char *error = vpx_codec_error(&ctx->decoder);
  47. av_log(avctx, AV_LOG_ERROR, "Failed to initialize decoder: %s\n",
  48. error);
  49. return AVERROR(EINVAL);
  50. }
  51. return 0;
  52. }
  53. static int vp8_decode(AVCodecContext *avctx,
  54. void *data, int *got_frame, AVPacket *avpkt)
  55. {
  56. VP8Context *ctx = avctx->priv_data;
  57. AVFrame *picture = data;
  58. const void *iter = NULL;
  59. struct vpx_image *img;
  60. int ret;
  61. if (vpx_codec_decode(&ctx->decoder, avpkt->data, avpkt->size, NULL, 0) !=
  62. VPX_CODEC_OK) {
  63. const char *error = vpx_codec_error(&ctx->decoder);
  64. const char *detail = vpx_codec_error_detail(&ctx->decoder);
  65. av_log(avctx, AV_LOG_ERROR, "Failed to decode frame: %s\n", error);
  66. if (detail)
  67. av_log(avctx, AV_LOG_ERROR, " Additional information: %s\n",
  68. detail);
  69. return AVERROR_INVALIDDATA;
  70. }
  71. if ((img = vpx_codec_get_frame(&ctx->decoder, &iter))) {
  72. avctx->pix_fmt = ff_vpx_imgfmt_to_pixfmt(img->fmt);
  73. if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
  74. av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (%d)\n",
  75. img->fmt);
  76. return AVERROR_INVALIDDATA;
  77. }
  78. if ((int) img->d_w != avctx->width || (int) img->d_h != avctx->height) {
  79. av_log(avctx, AV_LOG_INFO, "dimension change! %dx%d -> %dx%d\n",
  80. avctx->width, avctx->height, img->d_w, img->d_h);
  81. ret = ff_set_dimensions(avctx, img->d_w, img->d_h);
  82. if (ret < 0)
  83. return ret;
  84. }
  85. if ((ret = ff_get_buffer(avctx, picture, 0)) < 0)
  86. return ret;
  87. av_image_copy(picture->data, picture->linesize, img->planes,
  88. img->stride, avctx->pix_fmt, img->d_w, img->d_h);
  89. *got_frame = 1;
  90. }
  91. return avpkt->size;
  92. }
  93. static av_cold int vp8_free(AVCodecContext *avctx)
  94. {
  95. VP8Context *ctx = avctx->priv_data;
  96. vpx_codec_destroy(&ctx->decoder);
  97. return 0;
  98. }
  99. #if CONFIG_LIBVPX_VP8_DECODER
  100. static av_cold int vp8_init(AVCodecContext *avctx)
  101. {
  102. return vpx_init(avctx, &vpx_codec_vp8_dx_algo);
  103. }
  104. AVCodec ff_libvpx_vp8_decoder = {
  105. .name = "libvpx",
  106. .long_name = NULL_IF_CONFIG_SMALL("libvpx VP8"),
  107. .type = AVMEDIA_TYPE_VIDEO,
  108. .id = AV_CODEC_ID_VP8,
  109. .priv_data_size = sizeof(VP8Context),
  110. .init = vp8_init,
  111. .close = vp8_free,
  112. .decode = vp8_decode,
  113. .capabilities = AV_CODEC_CAP_AUTO_THREADS | AV_CODEC_CAP_DR1,
  114. };
  115. #endif /* CONFIG_LIBVPX_VP8_DECODER */
  116. #if CONFIG_LIBVPX_VP9_DECODER
  117. static av_cold int vp9_init(AVCodecContext *avctx)
  118. {
  119. return vpx_init(avctx, &vpx_codec_vp9_dx_algo);
  120. }
  121. AVCodec ff_libvpx_vp9_decoder = {
  122. .name = "libvpx-vp9",
  123. .long_name = NULL_IF_CONFIG_SMALL("libvpx VP9"),
  124. .type = AVMEDIA_TYPE_VIDEO,
  125. .id = AV_CODEC_ID_VP9,
  126. .priv_data_size = sizeof(VP8Context),
  127. .init = vp9_init,
  128. .close = vp8_free,
  129. .decode = vp8_decode,
  130. .capabilities = AV_CODEC_CAP_AUTO_THREADS,
  131. };
  132. #endif /* CONFIG_LIBVPX_VP9_DECODER */