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.

245 lines
7.7KB

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