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.

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