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.

161 lines
5.1KB

  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, (const uint8_t **) img->planes,
  88. img->stride, avctx->pix_fmt, img->d_w, img->d_h);
  89. #if VPX_IMAGE_ABI_VERSION >= 4
  90. switch (img->range) {
  91. case VPX_CR_STUDIO_RANGE:
  92. picture->color_range = AVCOL_RANGE_MPEG;
  93. break;
  94. case VPX_CR_FULL_RANGE:
  95. picture->color_range = AVCOL_RANGE_JPEG;
  96. break;
  97. }
  98. #endif
  99. *got_frame = 1;
  100. }
  101. return avpkt->size;
  102. }
  103. static av_cold int vp8_free(AVCodecContext *avctx)
  104. {
  105. VP8Context *ctx = avctx->priv_data;
  106. vpx_codec_destroy(&ctx->decoder);
  107. return 0;
  108. }
  109. #if CONFIG_LIBVPX_VP8_DECODER
  110. static av_cold int vp8_init(AVCodecContext *avctx)
  111. {
  112. return vpx_init(avctx, &vpx_codec_vp8_dx_algo);
  113. }
  114. AVCodec ff_libvpx_vp8_decoder = {
  115. .name = "libvpx",
  116. .long_name = NULL_IF_CONFIG_SMALL("libvpx VP8"),
  117. .type = AVMEDIA_TYPE_VIDEO,
  118. .id = AV_CODEC_ID_VP8,
  119. .priv_data_size = sizeof(VP8Context),
  120. .init = vp8_init,
  121. .close = vp8_free,
  122. .decode = vp8_decode,
  123. .capabilities = AV_CODEC_CAP_AUTO_THREADS | AV_CODEC_CAP_DR1,
  124. };
  125. #endif /* CONFIG_LIBVPX_VP8_DECODER */
  126. #if CONFIG_LIBVPX_VP9_DECODER
  127. static av_cold int vp9_init(AVCodecContext *avctx)
  128. {
  129. return vpx_init(avctx, &vpx_codec_vp9_dx_algo);
  130. }
  131. AVCodec ff_libvpx_vp9_decoder = {
  132. .name = "libvpx-vp9",
  133. .long_name = NULL_IF_CONFIG_SMALL("libvpx VP9"),
  134. .type = AVMEDIA_TYPE_VIDEO,
  135. .id = AV_CODEC_ID_VP9,
  136. .priv_data_size = sizeof(VP8Context),
  137. .init = vp9_init,
  138. .close = vp8_free,
  139. .decode = vp8_decode,
  140. .capabilities = AV_CODEC_CAP_AUTO_THREADS,
  141. };
  142. #endif /* CONFIG_LIBVPX_VP9_DECODER */