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.

150 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. typedef struct VP8DecoderContext {
  32. struct vpx_codec_ctx decoder;
  33. } VP8Context;
  34. static av_cold int vpx_init(AVCodecContext *avctx,
  35. const struct vpx_codec_iface *iface)
  36. {
  37. VP8Context *ctx = avctx->priv_data;
  38. struct vpx_codec_dec_cfg deccfg = {
  39. /* token partitions+1 would be a decent choice */
  40. .threads = FFMIN(avctx->thread_count, 16)
  41. };
  42. av_log(avctx, AV_LOG_INFO, "%s\n", vpx_codec_version_str());
  43. av_log(avctx, AV_LOG_VERBOSE, "%s\n", vpx_codec_build_config());
  44. if (vpx_codec_dec_init(&ctx->decoder, iface, &deccfg, 0) != VPX_CODEC_OK) {
  45. const char *error = vpx_codec_error(&ctx->decoder);
  46. av_log(avctx, AV_LOG_ERROR, "Failed to initialize decoder: %s\n",
  47. error);
  48. return AVERROR(EINVAL);
  49. }
  50. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  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. if (img->fmt != VPX_IMG_FMT_I420) {
  73. av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (%d)\n",
  74. img->fmt);
  75. return AVERROR_INVALIDDATA;
  76. }
  77. if ((int) img->d_w != avctx->width || (int) img->d_h != avctx->height) {
  78. av_log(avctx, AV_LOG_INFO, "dimension change! %dx%d -> %dx%d\n",
  79. avctx->width, avctx->height, img->d_w, img->d_h);
  80. if (av_image_check_size(img->d_w, img->d_h, 0, avctx))
  81. return AVERROR_INVALIDDATA;
  82. avcodec_set_dimensions(avctx, img->d_w, img->d_h);
  83. }
  84. if ((ret = ff_get_buffer(avctx, picture, 0)) < 0)
  85. return ret;
  86. av_image_copy(picture->data, picture->linesize, img->planes,
  87. img->stride, avctx->pix_fmt, img->d_w, img->d_h);
  88. *got_frame = 1;
  89. }
  90. return avpkt->size;
  91. }
  92. static av_cold int vp8_free(AVCodecContext *avctx)
  93. {
  94. VP8Context *ctx = avctx->priv_data;
  95. vpx_codec_destroy(&ctx->decoder);
  96. return 0;
  97. }
  98. #if CONFIG_LIBVPX_VP8_DECODER
  99. static av_cold int vp8_init(AVCodecContext *avctx)
  100. {
  101. return vpx_init(avctx, &vpx_codec_vp8_dx_algo);
  102. }
  103. AVCodec ff_libvpx_vp8_decoder = {
  104. .name = "libvpx",
  105. .long_name = NULL_IF_CONFIG_SMALL("libvpx VP8"),
  106. .type = AVMEDIA_TYPE_VIDEO,
  107. .id = AV_CODEC_ID_VP8,
  108. .priv_data_size = sizeof(VP8Context),
  109. .init = vp8_init,
  110. .close = vp8_free,
  111. .decode = vp8_decode,
  112. .capabilities = CODEC_CAP_AUTO_THREADS | CODEC_CAP_DR1,
  113. };
  114. #endif /* CONFIG_LIBVPX_VP8_DECODER */
  115. #if CONFIG_LIBVPX_VP9_DECODER
  116. static av_cold int vp9_init(AVCodecContext *avctx)
  117. {
  118. return vpx_init(avctx, &vpx_codec_vp9_dx_algo);
  119. }
  120. AVCodec ff_libvpx_vp9_decoder = {
  121. .name = "libvpx-vp9",
  122. .long_name = NULL_IF_CONFIG_SMALL("libvpx VP9"),
  123. .type = AVMEDIA_TYPE_VIDEO,
  124. .id = AV_CODEC_ID_VP9,
  125. .priv_data_size = sizeof(VP8Context),
  126. .init = vp9_init,
  127. .close = vp8_free,
  128. .decode = vp8_decode,
  129. .capabilities = CODEC_CAP_AUTO_THREADS | CODEC_CAP_EXPERIMENTAL,
  130. };
  131. #endif /* CONFIG_LIBVPX_VP9_DECODER */