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.

161 lines
5.1KB

  1. /*
  2. * Copyright (c) 2010, Google, Inc.
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; 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. #include "libaom.h"
  31. typedef struct AV1DecodeContext {
  32. struct aom_codec_ctx decoder;
  33. } AV1DecodeContext;
  34. static av_cold int aom_init(AVCodecContext *avctx)
  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. const struct aom_codec_iface *iface = &aom_codec_av1_dx_algo;
  42. av_log(avctx, AV_LOG_INFO, "%s\n", aom_codec_version_str());
  43. av_log(avctx, AV_LOG_VERBOSE, "%s\n", aom_codec_build_config());
  44. if (aom_codec_dec_init(&ctx->decoder, iface, &deccfg, 0) != AOM_CODEC_OK) {
  45. const char *error = aom_codec_error(&ctx->decoder);
  46. av_log(avctx, AV_LOG_ERROR, "Failed to initialize decoder: %s\n",
  47. error);
  48. return AVERROR(EINVAL);
  49. }
  50. return 0;
  51. }
  52. static void image_copy_16_to_8(AVFrame *pic, struct aom_image *img)
  53. {
  54. int i;
  55. for (i = 0; i < 3; i++) {
  56. int w = img->d_w;
  57. int h = img->d_h;
  58. int x, y;
  59. if (i) {
  60. w = (w + img->x_chroma_shift) >> img->x_chroma_shift;
  61. h = (h + img->y_chroma_shift) >> img->y_chroma_shift;
  62. }
  63. for (y = 0; y < h; y++) {
  64. uint16_t *src = (uint16_t *)(img->planes[i] + y * img->stride[i]);
  65. uint8_t *dst = pic->data[i] + y * pic->linesize[i];
  66. for (x = 0; x < w; x++)
  67. *dst++ = *src++;
  68. }
  69. }
  70. }
  71. static int aom_decode(AVCodecContext *avctx, void *data, int *got_frame,
  72. AVPacket *avpkt)
  73. {
  74. AV1DecodeContext *ctx = avctx->priv_data;
  75. AVFrame *picture = data;
  76. const void *iter = NULL;
  77. struct aom_image *img;
  78. int ret;
  79. if (aom_codec_decode(&ctx->decoder, avpkt->data, avpkt->size, NULL) !=
  80. AOM_CODEC_OK) {
  81. const char *error = aom_codec_error(&ctx->decoder);
  82. const char *detail = aom_codec_error_detail(&ctx->decoder);
  83. av_log(avctx, AV_LOG_ERROR, "Failed to decode frame: %s\n", error);
  84. if (detail)
  85. av_log(avctx, AV_LOG_ERROR, " Additional information: %s\n",
  86. detail);
  87. return AVERROR_INVALIDDATA;
  88. }
  89. if ((img = aom_codec_get_frame(&ctx->decoder, &iter))) {
  90. avctx->pix_fmt = ff_aom_imgfmt_to_pixfmt(img->fmt, img->bit_depth);
  91. if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
  92. av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (0x%02x %dbits)\n",
  93. img->fmt, img->bit_depth);
  94. return AVERROR_INVALIDDATA;
  95. }
  96. if ((int)img->d_w != avctx->width || (int)img->d_h != avctx->height) {
  97. av_log(avctx, AV_LOG_INFO, "dimension change! %dx%d -> %dx%d\n",
  98. avctx->width, avctx->height, img->d_w, img->d_h);
  99. ret = ff_set_dimensions(avctx, img->d_w, img->d_h);
  100. if (ret < 0)
  101. return ret;
  102. }
  103. if ((ret = ff_get_buffer(avctx, picture, 0)) < 0)
  104. return ret;
  105. if ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) && img->bit_depth == 8)
  106. image_copy_16_to_8(picture, img);
  107. else
  108. av_image_copy(picture->data, picture->linesize, (const uint8_t **)img->planes,
  109. img->stride, avctx->pix_fmt, img->d_w, img->d_h);
  110. switch (img->range) {
  111. case AOM_CR_STUDIO_RANGE:
  112. picture->color_range = AVCOL_RANGE_MPEG;
  113. break;
  114. case AOM_CR_FULL_RANGE:
  115. picture->color_range = AVCOL_RANGE_JPEG;
  116. break;
  117. }
  118. *got_frame = 1;
  119. }
  120. return avpkt->size;
  121. }
  122. static av_cold int aom_free(AVCodecContext *avctx)
  123. {
  124. AV1DecodeContext *ctx = avctx->priv_data;
  125. aom_codec_destroy(&ctx->decoder);
  126. return 0;
  127. }
  128. AVCodec ff_libaom_av1_decoder = {
  129. .name = "libaom-av1",
  130. .long_name = NULL_IF_CONFIG_SMALL("libaom AV1"),
  131. .type = AVMEDIA_TYPE_VIDEO,
  132. .id = AV_CODEC_ID_AV1,
  133. .priv_data_size = sizeof(AV1DecodeContext),
  134. .init = aom_init,
  135. .close = aom_free,
  136. .decode = aom_decode,
  137. .capabilities = AV_CODEC_CAP_AUTO_THREADS | AV_CODEC_CAP_DR1,
  138. .wrapper_name = "libaom",
  139. };