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.

250 lines
8.2KB

  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. * AV1 decoder support via libaom
  23. */
  24. #include <aom/aom_decoder.h>
  25. #include <aom/aomdx.h>
  26. #include "libavutil/common.h"
  27. #include "libavutil/imgutils.h"
  28. #include "avcodec.h"
  29. #include "internal.h"
  30. typedef struct AV1DecodeContext {
  31. struct aom_codec_ctx decoder;
  32. } AV1DecodeContext;
  33. static av_cold int aom_init(AVCodecContext *avctx,
  34. const struct aom_codec_iface *iface)
  35. {
  36. AV1DecodeContext *ctx = avctx->priv_data;
  37. struct aom_codec_dec_cfg deccfg = {
  38. /* token partitions+1 would be a decent choice */
  39. .threads = FFMIN(avctx->thread_count, 16)
  40. };
  41. av_log(avctx, AV_LOG_INFO, "%s\n", aom_codec_version_str());
  42. av_log(avctx, AV_LOG_VERBOSE, "%s\n", aom_codec_build_config());
  43. if (aom_codec_dec_init(&ctx->decoder, iface, &deccfg, 0) != AOM_CODEC_OK) {
  44. const char *error = aom_codec_error(&ctx->decoder);
  45. av_log(avctx, AV_LOG_ERROR, "Failed to initialize decoder: %s\n",
  46. error);
  47. return AVERROR(EINVAL);
  48. }
  49. return 0;
  50. }
  51. static void image_copy_16_to_8(AVFrame *pic, struct aom_image *img)
  52. {
  53. int i;
  54. for (i = 0; i < 3; i++) {
  55. int w = img->d_w;
  56. int h = img->d_h;
  57. int x, y;
  58. if (i) {
  59. w = (w + img->x_chroma_shift) >> img->x_chroma_shift;
  60. h = (h + img->y_chroma_shift) >> img->y_chroma_shift;
  61. }
  62. for (y = 0; y < h; y++) {
  63. uint16_t *src = (uint16_t *)(img->planes[i] + y * img->stride[i]);
  64. uint8_t *dst = pic->data[i] + y * pic->linesize[i];
  65. for (x = 0; x < w; x++)
  66. *dst++ = *src++;
  67. }
  68. }
  69. }
  70. // returns 0 on success, AVERROR_INVALIDDATA otherwise
  71. static int set_pix_fmt(AVCodecContext *avctx, struct aom_image *img)
  72. {
  73. static const enum AVColorSpace colorspaces[10] = {
  74. AVCOL_SPC_UNSPECIFIED, AVCOL_SPC_BT470BG, AVCOL_SPC_BT709, AVCOL_SPC_SMPTE170M,
  75. AVCOL_SPC_SMPTE240M, AVCOL_SPC_BT2020_NCL, AVCOL_SPC_BT2020_CL, AVCOL_SPC_RGB,
  76. AVCOL_SPC_ICTCP, AVCOL_SPC_RESERVED
  77. };
  78. static const enum AVColorRange color_ranges[] = {
  79. AVCOL_RANGE_MPEG, AVCOL_RANGE_JPEG
  80. };
  81. avctx->color_range = color_ranges[img->range];
  82. avctx->colorspace = colorspaces[img->cs];
  83. switch (img->fmt) {
  84. case AOM_IMG_FMT_I420:
  85. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  86. return 0;
  87. case AOM_IMG_FMT_I422:
  88. avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  89. return 0;
  90. case AOM_IMG_FMT_I440:
  91. avctx->pix_fmt = AV_PIX_FMT_YUV440P;
  92. return 0;
  93. case AOM_IMG_FMT_I444:
  94. avctx->pix_fmt = avctx->colorspace == AVCOL_SPC_RGB ?
  95. AV_PIX_FMT_GBRP : AV_PIX_FMT_YUV444P;
  96. return 0;
  97. case AOM_IMG_FMT_I42016:
  98. if (img->bit_depth == 8) {
  99. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  100. return 0;
  101. } else 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 AOM_IMG_FMT_I42216:
  111. if (img->bit_depth == 8) {
  112. avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  113. return 0;
  114. } else if (img->bit_depth == 10) {
  115. avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
  116. return 0;
  117. } else if (img->bit_depth == 12) {
  118. avctx->pix_fmt = AV_PIX_FMT_YUV422P12;
  119. return 0;
  120. } else {
  121. return AVERROR_INVALIDDATA;
  122. }
  123. case AOM_IMG_FMT_I44016:
  124. if (img->bit_depth == 8) {
  125. avctx->pix_fmt = AV_PIX_FMT_YUV440P;
  126. return 0;
  127. } else if (img->bit_depth == 10) {
  128. avctx->pix_fmt = AV_PIX_FMT_YUV440P10;
  129. return 0;
  130. } else if (img->bit_depth == 12) {
  131. avctx->pix_fmt = AV_PIX_FMT_YUV440P12;
  132. return 0;
  133. } else {
  134. return AVERROR_INVALIDDATA;
  135. }
  136. case AOM_IMG_FMT_I44416:
  137. if (img->bit_depth == 8) {
  138. avctx->pix_fmt = avctx->colorspace == AVCOL_SPC_RGB ?
  139. AV_PIX_FMT_GBRP : AV_PIX_FMT_YUV444P;
  140. return 0;
  141. } else if (img->bit_depth == 10) {
  142. avctx->pix_fmt = avctx->colorspace == AVCOL_SPC_RGB ?
  143. AV_PIX_FMT_GBRP10 : AV_PIX_FMT_YUV444P10;
  144. return 0;
  145. } else if (img->bit_depth == 12) {
  146. avctx->pix_fmt = avctx->colorspace == AVCOL_SPC_RGB ?
  147. AV_PIX_FMT_GBRP12 : AV_PIX_FMT_YUV444P12;
  148. return 0;
  149. } else {
  150. return AVERROR_INVALIDDATA;
  151. }
  152. default:
  153. return AVERROR_INVALIDDATA;
  154. }
  155. }
  156. static int aom_decode(AVCodecContext *avctx, void *data, int *got_frame,
  157. AVPacket *avpkt)
  158. {
  159. AV1DecodeContext *ctx = avctx->priv_data;
  160. AVFrame *picture = data;
  161. const void *iter = NULL;
  162. struct aom_image *img;
  163. int ret;
  164. if (aom_codec_decode(&ctx->decoder, avpkt->data, avpkt->size, NULL) !=
  165. AOM_CODEC_OK) {
  166. const char *error = aom_codec_error(&ctx->decoder);
  167. const char *detail = aom_codec_error_detail(&ctx->decoder);
  168. av_log(avctx, AV_LOG_ERROR, "Failed to decode frame: %s\n", error);
  169. if (detail)
  170. av_log(avctx, AV_LOG_ERROR, " Additional information: %s\n",
  171. detail);
  172. return AVERROR_INVALIDDATA;
  173. }
  174. if ((img = aom_codec_get_frame(&ctx->decoder, &iter))) {
  175. if (img->d_w > img->w || img->d_h > img->h) {
  176. av_log(avctx, AV_LOG_ERROR, "Display dimensions %dx%d exceed storage %dx%d\n",
  177. img->d_w, img->d_h, img->w, img->h);
  178. return AVERROR_EXTERNAL;
  179. }
  180. if ((ret = set_pix_fmt(avctx, img)) < 0) {
  181. av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (%d) / bit_depth (%d)\n",
  182. img->fmt, img->bit_depth);
  183. return ret;
  184. }
  185. if ((int)img->d_w != avctx->width || (int)img->d_h != avctx->height) {
  186. av_log(avctx, AV_LOG_INFO, "dimension change! %dx%d -> %dx%d\n",
  187. avctx->width, avctx->height, img->d_w, img->d_h);
  188. ret = ff_set_dimensions(avctx, img->d_w, img->d_h);
  189. if (ret < 0)
  190. return ret;
  191. }
  192. if ((ret = ff_get_buffer(avctx, picture, 0)) < 0)
  193. return ret;
  194. if ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) && img->bit_depth == 8)
  195. image_copy_16_to_8(picture, img);
  196. else
  197. av_image_copy(picture->data, picture->linesize, (const uint8_t **)img->planes,
  198. img->stride, avctx->pix_fmt, img->d_w, img->d_h);
  199. *got_frame = 1;
  200. }
  201. return avpkt->size;
  202. }
  203. static av_cold int aom_free(AVCodecContext *avctx)
  204. {
  205. AV1DecodeContext *ctx = avctx->priv_data;
  206. aom_codec_destroy(&ctx->decoder);
  207. return 0;
  208. }
  209. static av_cold int av1_init(AVCodecContext *avctx)
  210. {
  211. return aom_init(avctx, &aom_codec_av1_dx_algo);
  212. }
  213. AVCodec ff_libaom_av1_decoder = {
  214. .name = "libaom-av1",
  215. .long_name = NULL_IF_CONFIG_SMALL("libaom AV1"),
  216. .type = AVMEDIA_TYPE_VIDEO,
  217. .id = AV_CODEC_ID_AV1,
  218. .priv_data_size = sizeof(AV1DecodeContext),
  219. .init = av1_init,
  220. .close = aom_free,
  221. .decode = aom_decode,
  222. .capabilities = AV_CODEC_CAP_AUTO_THREADS | AV_CODEC_CAP_DR1,
  223. .wrapper_name = "libaom",
  224. };