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.

252 lines
8.1KB

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