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.

210 lines
6.6KB

  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. #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. // returns 0 on success, AVERROR_INVALIDDATA otherwise
  54. static int set_pix_fmt(AVCodecContext *avctx, struct vpx_image *img)
  55. {
  56. if (avctx->codec_id == AV_CODEC_ID_VP8 && img->fmt != VPX_IMG_FMT_I420)
  57. return AVERROR_INVALIDDATA;
  58. switch (img->fmt) {
  59. case VPX_IMG_FMT_I420:
  60. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  61. return 0;
  62. #if CONFIG_LIBVPX_VP9_DECODER
  63. case VPX_IMG_FMT_I422:
  64. avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  65. return 0;
  66. case VPX_IMG_FMT_I444:
  67. avctx->pix_fmt = AV_PIX_FMT_YUV444P;
  68. return 0;
  69. #ifdef VPX_IMG_FMT_HIGHBITDEPTH
  70. case VPX_IMG_FMT_I42016:
  71. if (img->bit_depth == 10) {
  72. avctx->pix_fmt = AV_PIX_FMT_YUV420P10LE;
  73. return 0;
  74. } else if (img->bit_depth == 12) {
  75. avctx->pix_fmt = AV_PIX_FMT_YUV420P12LE;
  76. return 0;
  77. } else {
  78. return AVERROR_INVALIDDATA;
  79. }
  80. case VPX_IMG_FMT_I42216:
  81. if (img->bit_depth == 10) {
  82. avctx->pix_fmt = AV_PIX_FMT_YUV422P10LE;
  83. return 0;
  84. } else if (img->bit_depth == 12) {
  85. avctx->pix_fmt = AV_PIX_FMT_YUV422P12LE;
  86. return 0;
  87. } else {
  88. return AVERROR_INVALIDDATA;
  89. }
  90. case VPX_IMG_FMT_I44416:
  91. if (img->bit_depth == 10) {
  92. avctx->pix_fmt = AV_PIX_FMT_YUV444P10LE;
  93. return 0;
  94. } else if (img->bit_depth == 12) {
  95. avctx->pix_fmt = AV_PIX_FMT_YUV444P12LE;
  96. return 0;
  97. } else {
  98. return AVERROR_INVALIDDATA;
  99. }
  100. #endif
  101. #endif
  102. default:
  103. return AVERROR_INVALIDDATA;
  104. }
  105. }
  106. static int vp8_decode(AVCodecContext *avctx,
  107. void *data, int *got_frame, AVPacket *avpkt)
  108. {
  109. VP8Context *ctx = avctx->priv_data;
  110. AVFrame *picture = data;
  111. const void *iter = NULL;
  112. struct vpx_image *img;
  113. int ret;
  114. if (vpx_codec_decode(&ctx->decoder, avpkt->data, avpkt->size, NULL, 0) !=
  115. VPX_CODEC_OK) {
  116. const char *error = vpx_codec_error(&ctx->decoder);
  117. const char *detail = vpx_codec_error_detail(&ctx->decoder);
  118. av_log(avctx, AV_LOG_ERROR, "Failed to decode frame: %s\n", error);
  119. if (detail)
  120. av_log(avctx, AV_LOG_ERROR, " Additional information: %s\n",
  121. detail);
  122. return AVERROR_INVALIDDATA;
  123. }
  124. if ((img = vpx_codec_get_frame(&ctx->decoder, &iter))) {
  125. if ((ret = set_pix_fmt(avctx, img)) < 0) {
  126. #ifdef VPX_IMG_FMT_HIGHBITDEPTH
  127. av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (%d) / bit_depth (%d)\n",
  128. img->fmt, img->bit_depth);
  129. #else
  130. av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (%d) / bit_depth (%d)\n",
  131. img->fmt, 8);
  132. #endif
  133. return ret;
  134. }
  135. if ((int) img->d_w != avctx->width || (int) img->d_h != avctx->height) {
  136. av_log(avctx, AV_LOG_INFO, "dimension change! %dx%d -> %dx%d\n",
  137. avctx->width, avctx->height, img->d_w, img->d_h);
  138. ret = ff_set_dimensions(avctx, img->d_w, img->d_h);
  139. if (ret < 0)
  140. return ret;
  141. }
  142. if ((ret = ff_get_buffer(avctx, picture, 0)) < 0)
  143. return ret;
  144. av_image_copy(picture->data, picture->linesize, (const uint8_t **)img->planes,
  145. img->stride, avctx->pix_fmt, img->d_w, img->d_h);
  146. *got_frame = 1;
  147. }
  148. return avpkt->size;
  149. }
  150. static av_cold int vp8_free(AVCodecContext *avctx)
  151. {
  152. VP8Context *ctx = avctx->priv_data;
  153. vpx_codec_destroy(&ctx->decoder);
  154. return 0;
  155. }
  156. #if CONFIG_LIBVPX_VP8_DECODER
  157. static av_cold int vp8_init(AVCodecContext *avctx)
  158. {
  159. return vpx_init(avctx, &vpx_codec_vp8_dx_algo);
  160. }
  161. AVCodec ff_libvpx_vp8_decoder = {
  162. .name = "libvpx",
  163. .long_name = NULL_IF_CONFIG_SMALL("libvpx VP8"),
  164. .type = AVMEDIA_TYPE_VIDEO,
  165. .id = AV_CODEC_ID_VP8,
  166. .priv_data_size = sizeof(VP8Context),
  167. .init = vp8_init,
  168. .close = vp8_free,
  169. .decode = vp8_decode,
  170. .capabilities = CODEC_CAP_AUTO_THREADS | CODEC_CAP_DR1,
  171. };
  172. #endif /* CONFIG_LIBVPX_VP8_DECODER */
  173. #if CONFIG_LIBVPX_VP9_DECODER
  174. static av_cold int vp9_init(AVCodecContext *avctx)
  175. {
  176. return vpx_init(avctx, &vpx_codec_vp9_dx_algo);
  177. }
  178. AVCodec ff_libvpx_vp9_decoder = {
  179. .name = "libvpx-vp9",
  180. .long_name = NULL_IF_CONFIG_SMALL("libvpx VP9"),
  181. .type = AVMEDIA_TYPE_VIDEO,
  182. .id = AV_CODEC_ID_VP9,
  183. .priv_data_size = sizeof(VP8Context),
  184. .init = vp9_init,
  185. .close = vp8_free,
  186. .decode = vp8_decode,
  187. .capabilities = CODEC_CAP_AUTO_THREADS | CODEC_CAP_DR1,
  188. .init_static_data = ff_vp9_init_static,
  189. };
  190. #endif /* CONFIG_LIBVPX_VP9_DECODER */