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.

266 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. #include "profiles.h"
  33. typedef struct VP8DecoderContext {
  34. struct vpx_codec_ctx decoder;
  35. } VP8Context;
  36. static av_cold int vpx_init(AVCodecContext *avctx,
  37. const struct vpx_codec_iface *iface)
  38. {
  39. VP8Context *ctx = avctx->priv_data;
  40. struct vpx_codec_dec_cfg deccfg = {
  41. /* token partitions+1 would be a decent choice */
  42. .threads = FFMIN(avctx->thread_count, 16)
  43. };
  44. av_log(avctx, AV_LOG_INFO, "%s\n", vpx_codec_version_str());
  45. av_log(avctx, AV_LOG_VERBOSE, "%s\n", vpx_codec_build_config());
  46. if (vpx_codec_dec_init(&ctx->decoder, iface, &deccfg, 0) != VPX_CODEC_OK) {
  47. const char *error = vpx_codec_error(&ctx->decoder);
  48. av_log(avctx, AV_LOG_ERROR, "Failed to initialize decoder: %s\n",
  49. error);
  50. return AVERROR(EINVAL);
  51. }
  52. return 0;
  53. }
  54. // returns 0 on success, AVERROR_INVALIDDATA otherwise
  55. static int set_pix_fmt(AVCodecContext *avctx, struct vpx_image *img)
  56. {
  57. #if VPX_IMAGE_ABI_VERSION >= 3
  58. static const enum AVColorSpace colorspaces[8] = {
  59. AVCOL_SPC_UNSPECIFIED, AVCOL_SPC_BT470BG, AVCOL_SPC_BT709, AVCOL_SPC_SMPTE170M,
  60. AVCOL_SPC_SMPTE240M, AVCOL_SPC_BT2020_NCL, AVCOL_SPC_RESERVED, AVCOL_SPC_RGB,
  61. };
  62. #if VPX_IMAGE_ABI_VERSION >= 4
  63. static const enum AVColorRange color_ranges[] = {
  64. AVCOL_RANGE_MPEG, AVCOL_RANGE_JPEG
  65. };
  66. avctx->color_range = color_ranges[img->range];
  67. #endif
  68. avctx->colorspace = colorspaces[img->cs];
  69. #endif
  70. if (avctx->codec_id == AV_CODEC_ID_VP8 && img->fmt != VPX_IMG_FMT_I420)
  71. return AVERROR_INVALIDDATA;
  72. switch (img->fmt) {
  73. case VPX_IMG_FMT_I420:
  74. if (avctx->codec_id == AV_CODEC_ID_VP9)
  75. avctx->profile = FF_PROFILE_VP9_0;
  76. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  77. return 0;
  78. #if CONFIG_LIBVPX_VP9_DECODER
  79. case VPX_IMG_FMT_I422:
  80. avctx->profile = FF_PROFILE_VP9_1;
  81. avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  82. return 0;
  83. #if VPX_IMAGE_ABI_VERSION >= 3
  84. case VPX_IMG_FMT_I440:
  85. avctx->profile = FF_PROFILE_VP9_1;
  86. avctx->pix_fmt = AV_PIX_FMT_YUV440P;
  87. return 0;
  88. #endif
  89. case VPX_IMG_FMT_I444:
  90. avctx->profile = FF_PROFILE_VP9_1;
  91. #if VPX_IMAGE_ABI_VERSION >= 3
  92. avctx->pix_fmt = avctx->colorspace == AVCOL_SPC_RGB ?
  93. AV_PIX_FMT_GBRP : AV_PIX_FMT_YUV444P;
  94. #else
  95. avctx->pix_fmt = AV_PIX_FMT_YUV444P;
  96. #endif
  97. return 0;
  98. #ifdef VPX_IMG_FMT_HIGHBITDEPTH
  99. case VPX_IMG_FMT_I42016:
  100. avctx->profile = FF_PROFILE_VP9_2;
  101. if (img->bit_depth == 10) {
  102. avctx->pix_fmt = AV_PIX_FMT_YUV420P10;
  103. return 0;
  104. } else if (img->bit_depth == 12) {
  105. avctx->pix_fmt = AV_PIX_FMT_YUV420P12;
  106. return 0;
  107. } else {
  108. return AVERROR_INVALIDDATA;
  109. }
  110. case VPX_IMG_FMT_I42216:
  111. avctx->profile = FF_PROFILE_VP9_3;
  112. if (img->bit_depth == 10) {
  113. avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
  114. return 0;
  115. } else if (img->bit_depth == 12) {
  116. avctx->pix_fmt = AV_PIX_FMT_YUV422P12;
  117. return 0;
  118. } else {
  119. return AVERROR_INVALIDDATA;
  120. }
  121. #if VPX_IMAGE_ABI_VERSION >= 3
  122. case VPX_IMG_FMT_I44016:
  123. avctx->profile = FF_PROFILE_VP9_3;
  124. if (img->bit_depth == 10) {
  125. avctx->pix_fmt = AV_PIX_FMT_YUV440P10;
  126. return 0;
  127. } else if (img->bit_depth == 12) {
  128. avctx->pix_fmt = AV_PIX_FMT_YUV440P12;
  129. return 0;
  130. } else {
  131. return AVERROR_INVALIDDATA;
  132. }
  133. #endif
  134. case VPX_IMG_FMT_I44416:
  135. avctx->profile = FF_PROFILE_VP9_3;
  136. if (img->bit_depth == 10) {
  137. #if VPX_IMAGE_ABI_VERSION >= 3
  138. avctx->pix_fmt = avctx->colorspace == AVCOL_SPC_RGB ?
  139. AV_PIX_FMT_GBRP10 : AV_PIX_FMT_YUV444P10;
  140. #else
  141. avctx->pix_fmt = AV_PIX_FMT_YUV444P10;
  142. #endif
  143. return 0;
  144. } else if (img->bit_depth == 12) {
  145. #if VPX_IMAGE_ABI_VERSION >= 3
  146. avctx->pix_fmt = avctx->colorspace == AVCOL_SPC_RGB ?
  147. AV_PIX_FMT_GBRP12 : AV_PIX_FMT_YUV444P12;
  148. #else
  149. avctx->pix_fmt = AV_PIX_FMT_YUV444P12;
  150. #endif
  151. return 0;
  152. } else {
  153. return AVERROR_INVALIDDATA;
  154. }
  155. #endif
  156. #endif
  157. default:
  158. return AVERROR_INVALIDDATA;
  159. }
  160. }
  161. static int vp8_decode(AVCodecContext *avctx,
  162. void *data, int *got_frame, AVPacket *avpkt)
  163. {
  164. VP8Context *ctx = avctx->priv_data;
  165. AVFrame *picture = data;
  166. const void *iter = NULL;
  167. struct vpx_image *img;
  168. int ret;
  169. if (vpx_codec_decode(&ctx->decoder, avpkt->data, avpkt->size, NULL, 0) !=
  170. VPX_CODEC_OK) {
  171. const char *error = vpx_codec_error(&ctx->decoder);
  172. const char *detail = vpx_codec_error_detail(&ctx->decoder);
  173. av_log(avctx, AV_LOG_ERROR, "Failed to decode frame: %s\n", error);
  174. if (detail)
  175. av_log(avctx, AV_LOG_ERROR, " Additional information: %s\n",
  176. detail);
  177. return AVERROR_INVALIDDATA;
  178. }
  179. if ((img = vpx_codec_get_frame(&ctx->decoder, &iter))) {
  180. if ((ret = set_pix_fmt(avctx, img)) < 0) {
  181. #ifdef VPX_IMG_FMT_HIGHBITDEPTH
  182. av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (%d) / bit_depth (%d)\n",
  183. img->fmt, img->bit_depth);
  184. #else
  185. av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (%d) / bit_depth (%d)\n",
  186. img->fmt, 8);
  187. #endif
  188. return ret;
  189. }
  190. if ((int) img->d_w != avctx->width || (int) img->d_h != avctx->height) {
  191. av_log(avctx, AV_LOG_INFO, "dimension change! %dx%d -> %dx%d\n",
  192. avctx->width, avctx->height, img->d_w, img->d_h);
  193. ret = ff_set_dimensions(avctx, img->d_w, img->d_h);
  194. if (ret < 0)
  195. return ret;
  196. }
  197. if ((ret = ff_get_buffer(avctx, picture, 0)) < 0)
  198. return ret;
  199. av_image_copy(picture->data, picture->linesize, (const uint8_t **)img->planes,
  200. img->stride, avctx->pix_fmt, img->d_w, img->d_h);
  201. *got_frame = 1;
  202. }
  203. return avpkt->size;
  204. }
  205. static av_cold int vp8_free(AVCodecContext *avctx)
  206. {
  207. VP8Context *ctx = avctx->priv_data;
  208. vpx_codec_destroy(&ctx->decoder);
  209. return 0;
  210. }
  211. #if CONFIG_LIBVPX_VP8_DECODER
  212. static av_cold int vp8_init(AVCodecContext *avctx)
  213. {
  214. return vpx_init(avctx, &vpx_codec_vp8_dx_algo);
  215. }
  216. AVCodec ff_libvpx_vp8_decoder = {
  217. .name = "libvpx",
  218. .long_name = NULL_IF_CONFIG_SMALL("libvpx VP8"),
  219. .type = AVMEDIA_TYPE_VIDEO,
  220. .id = AV_CODEC_ID_VP8,
  221. .priv_data_size = sizeof(VP8Context),
  222. .init = vp8_init,
  223. .close = vp8_free,
  224. .decode = vp8_decode,
  225. .capabilities = AV_CODEC_CAP_AUTO_THREADS | AV_CODEC_CAP_DR1,
  226. };
  227. #endif /* CONFIG_LIBVPX_VP8_DECODER */
  228. #if CONFIG_LIBVPX_VP9_DECODER
  229. static av_cold int vp9_init(AVCodecContext *avctx)
  230. {
  231. return vpx_init(avctx, &vpx_codec_vp9_dx_algo);
  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(ff_vp9_profiles),
  245. };
  246. #endif /* CONFIG_LIBVPX_VP9_DECODER */