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.

152 lines
4.9KB

  1. /*
  2. * Copyright (c) 2010, Google, Inc.
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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. typedef struct VP8DecoderContext {
  31. struct vpx_codec_ctx decoder;
  32. } VP8Context;
  33. static av_cold int vpx_init(AVCodecContext *avctx,
  34. const struct vpx_codec_iface *iface)
  35. {
  36. VP8Context *ctx = avctx->priv_data;
  37. struct vpx_codec_dec_cfg deccfg = {
  38. /* token partitions+1 would be a decent choice */
  39. .threads = FFMIN(avctx->thread_count, 16)
  40. };
  41. av_log(avctx, AV_LOG_INFO, "%s\n", vpx_codec_version_str());
  42. av_log(avctx, AV_LOG_VERBOSE, "%s\n", vpx_codec_build_config());
  43. if (vpx_codec_dec_init(&ctx->decoder, iface, &deccfg, 0) != VPX_CODEC_OK) {
  44. const char *error = vpx_codec_error(&ctx->decoder);
  45. av_log(avctx, AV_LOG_ERROR, "Failed to initialize decoder: %s\n",
  46. error);
  47. return AVERROR(EINVAL);
  48. }
  49. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  50. return 0;
  51. }
  52. static int vp8_decode(AVCodecContext *avctx,
  53. void *data, int *got_frame, AVPacket *avpkt)
  54. {
  55. VP8Context *ctx = avctx->priv_data;
  56. AVFrame *picture = data;
  57. const void *iter = NULL;
  58. struct vpx_image *img;
  59. if (vpx_codec_decode(&ctx->decoder, avpkt->data, avpkt->size, NULL, 0) !=
  60. VPX_CODEC_OK) {
  61. const char *error = vpx_codec_error(&ctx->decoder);
  62. const char *detail = vpx_codec_error_detail(&ctx->decoder);
  63. av_log(avctx, AV_LOG_ERROR, "Failed to decode frame: %s\n", error);
  64. if (detail)
  65. av_log(avctx, AV_LOG_ERROR, " Additional information: %s\n",
  66. detail);
  67. return AVERROR_INVALIDDATA;
  68. }
  69. if ((img = vpx_codec_get_frame(&ctx->decoder, &iter))) {
  70. if (img->fmt != VPX_IMG_FMT_I420) {
  71. av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (%d)\n",
  72. img->fmt);
  73. return AVERROR_INVALIDDATA;
  74. }
  75. if ((int) img->d_w != avctx->width || (int) img->d_h != avctx->height) {
  76. av_log(avctx, AV_LOG_INFO, "dimension change! %dx%d -> %dx%d\n",
  77. avctx->width, avctx->height, img->d_w, img->d_h);
  78. if (av_image_check_size(img->d_w, img->d_h, 0, avctx))
  79. return AVERROR_INVALIDDATA;
  80. avcodec_set_dimensions(avctx, img->d_w, img->d_h);
  81. }
  82. picture->data[0] = img->planes[0];
  83. picture->data[1] = img->planes[1];
  84. picture->data[2] = img->planes[2];
  85. picture->data[3] = NULL;
  86. picture->linesize[0] = img->stride[0];
  87. picture->linesize[1] = img->stride[1];
  88. picture->linesize[2] = img->stride[2];
  89. picture->linesize[3] = 0;
  90. *got_frame = 1;
  91. }
  92. return avpkt->size;
  93. }
  94. static av_cold int vp8_free(AVCodecContext *avctx)
  95. {
  96. VP8Context *ctx = avctx->priv_data;
  97. vpx_codec_destroy(&ctx->decoder);
  98. return 0;
  99. }
  100. #if CONFIG_LIBVPX_VP8_DECODER
  101. static av_cold int vp8_init(AVCodecContext *avctx)
  102. {
  103. return vpx_init(avctx, &vpx_codec_vp8_dx_algo);
  104. }
  105. AVCodec ff_libvpx_vp8_decoder = {
  106. .name = "libvpx",
  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 = CODEC_CAP_AUTO_THREADS,
  114. .long_name = NULL_IF_CONFIG_SMALL("libvpx VP8"),
  115. };
  116. #endif /* CONFIG_LIBVPX_VP8_DECODER */
  117. #if CONFIG_LIBVPX_VP9_DECODER
  118. static av_cold int vp9_init(AVCodecContext *avctx)
  119. {
  120. return vpx_init(avctx, &vpx_codec_vp9_dx_algo);
  121. }
  122. AVCodec ff_libvpx_vp9_decoder = {
  123. .name = "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 = CODEC_CAP_AUTO_THREADS | CODEC_CAP_EXPERIMENTAL,
  131. .long_name = NULL_IF_CONFIG_SMALL("libvpx VP9"),
  132. };
  133. #endif /* CONFIG_LIBVPX_VP9_DECODER */