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.

258 lines
8.7KB

  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. #include "profiles.h"
  31. typedef struct AV1DecodeContext {
  32. struct aom_codec_ctx decoder;
  33. } AV1DecodeContext;
  34. static av_cold int aom_init(AVCodecContext *avctx,
  35. const struct aom_codec_iface *iface)
  36. {
  37. AV1DecodeContext *ctx = avctx->priv_data;
  38. struct aom_codec_dec_cfg deccfg = {
  39. .threads = FFMIN(avctx->thread_count ? avctx->thread_count : av_cpu_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. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pic->format);
  54. int i;
  55. for (i = 0; i < desc->nb_components; 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. // returns 0 on success, AVERROR_INVALIDDATA otherwise
  72. static int set_pix_fmt(AVCodecContext *avctx, struct aom_image *img)
  73. {
  74. static const enum AVColorRange color_ranges[] = {
  75. AVCOL_RANGE_MPEG, AVCOL_RANGE_JPEG
  76. };
  77. avctx->color_range = color_ranges[img->range];
  78. avctx->color_primaries = img->cp;
  79. avctx->colorspace = img->mc;
  80. avctx->color_trc = img->tc;
  81. switch (img->fmt) {
  82. case AOM_IMG_FMT_I420:
  83. case AOM_IMG_FMT_I42016:
  84. if (img->bit_depth == 8) {
  85. avctx->pix_fmt = img->monochrome ?
  86. AV_PIX_FMT_GRAY8 : AV_PIX_FMT_YUV420P;
  87. avctx->profile = FF_PROFILE_AV1_MAIN;
  88. return 0;
  89. } else if (img->bit_depth == 10) {
  90. avctx->pix_fmt = img->monochrome ?
  91. AV_PIX_FMT_GRAY10 : AV_PIX_FMT_YUV420P10;
  92. avctx->profile = FF_PROFILE_AV1_MAIN;
  93. return 0;
  94. } else if (img->bit_depth == 12) {
  95. avctx->pix_fmt = img->monochrome ?
  96. AV_PIX_FMT_GRAY12 : AV_PIX_FMT_YUV420P12;
  97. avctx->profile = FF_PROFILE_AV1_PROFESSIONAL;
  98. return 0;
  99. } else {
  100. return AVERROR_INVALIDDATA;
  101. }
  102. case AOM_IMG_FMT_I422:
  103. case AOM_IMG_FMT_I42216:
  104. if (img->bit_depth == 8) {
  105. avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  106. avctx->profile = FF_PROFILE_AV1_PROFESSIONAL;
  107. return 0;
  108. } else if (img->bit_depth == 10) {
  109. avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
  110. avctx->profile = FF_PROFILE_AV1_PROFESSIONAL;
  111. return 0;
  112. } else if (img->bit_depth == 12) {
  113. avctx->pix_fmt = AV_PIX_FMT_YUV422P12;
  114. avctx->profile = FF_PROFILE_AV1_PROFESSIONAL;
  115. return 0;
  116. } else {
  117. return AVERROR_INVALIDDATA;
  118. }
  119. case AOM_IMG_FMT_I444:
  120. case AOM_IMG_FMT_I44416:
  121. if (img->bit_depth == 8) {
  122. avctx->pix_fmt = AV_PIX_FMT_YUV444P;
  123. avctx->profile = FF_PROFILE_AV1_HIGH;
  124. return 0;
  125. } else if (img->bit_depth == 10) {
  126. avctx->pix_fmt = AV_PIX_FMT_YUV444P10;
  127. avctx->profile = FF_PROFILE_AV1_HIGH;
  128. return 0;
  129. } else if (img->bit_depth == 12) {
  130. avctx->pix_fmt = AV_PIX_FMT_YUV444P12;
  131. avctx->profile = FF_PROFILE_AV1_PROFESSIONAL;
  132. return 0;
  133. } else {
  134. return AVERROR_INVALIDDATA;
  135. }
  136. default:
  137. return AVERROR_INVALIDDATA;
  138. }
  139. }
  140. static int aom_decode(AVCodecContext *avctx, void *data, int *got_frame,
  141. AVPacket *avpkt)
  142. {
  143. AV1DecodeContext *ctx = avctx->priv_data;
  144. AVFrame *picture = data;
  145. const void *iter = NULL;
  146. struct aom_image *img;
  147. aom_codec_frame_flags_t av_unused flags;
  148. int ret;
  149. if (aom_codec_decode(&ctx->decoder, avpkt->data, avpkt->size, NULL) !=
  150. AOM_CODEC_OK) {
  151. const char *error = aom_codec_error(&ctx->decoder);
  152. const char *detail = aom_codec_error_detail(&ctx->decoder);
  153. av_log(avctx, AV_LOG_ERROR, "Failed to decode frame: %s\n", error);
  154. if (detail)
  155. av_log(avctx, AV_LOG_ERROR, " Additional information: %s\n",
  156. detail);
  157. return AVERROR_INVALIDDATA;
  158. }
  159. if ((img = aom_codec_get_frame(&ctx->decoder, &iter))) {
  160. if (img->d_w > img->w || img->d_h > img->h) {
  161. av_log(avctx, AV_LOG_ERROR, "Display dimensions %dx%d exceed storage %dx%d\n",
  162. img->d_w, img->d_h, img->w, img->h);
  163. return AVERROR_EXTERNAL;
  164. }
  165. if ((ret = set_pix_fmt(avctx, img)) < 0) {
  166. av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (%d) / bit_depth (%d)\n",
  167. img->fmt, img->bit_depth);
  168. return ret;
  169. }
  170. if ((int)img->d_w != avctx->width || (int)img->d_h != avctx->height) {
  171. av_log(avctx, AV_LOG_INFO, "dimension change! %dx%d -> %dx%d\n",
  172. avctx->width, avctx->height, img->d_w, img->d_h);
  173. ret = ff_set_dimensions(avctx, img->d_w, img->d_h);
  174. if (ret < 0)
  175. return ret;
  176. }
  177. if ((ret = ff_get_buffer(avctx, picture, 0)) < 0)
  178. return ret;
  179. #ifdef AOM_CTRL_AOMD_GET_FRAME_FLAGS
  180. ret = aom_codec_control(&ctx->decoder, AOMD_GET_FRAME_FLAGS, &flags);
  181. if (ret == AOM_CODEC_OK) {
  182. picture->key_frame = !!(flags & AOM_FRAME_IS_KEY);
  183. if (flags & (AOM_FRAME_IS_KEY | AOM_FRAME_IS_INTRAONLY))
  184. picture->pict_type = AV_PICTURE_TYPE_I;
  185. else if (flags & AOM_FRAME_IS_SWITCH)
  186. picture->pict_type = AV_PICTURE_TYPE_SP;
  187. else
  188. picture->pict_type = AV_PICTURE_TYPE_P;
  189. }
  190. #endif
  191. av_reduce(&picture->sample_aspect_ratio.num,
  192. &picture->sample_aspect_ratio.den,
  193. picture->height * img->r_w,
  194. picture->width * img->r_h,
  195. INT_MAX);
  196. ff_set_sar(avctx, picture->sample_aspect_ratio);
  197. if ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) && img->bit_depth == 8)
  198. image_copy_16_to_8(picture, img);
  199. else
  200. av_image_copy(picture->data, picture->linesize, (const uint8_t **)img->planes,
  201. img->stride, avctx->pix_fmt, img->d_w, img->d_h);
  202. *got_frame = 1;
  203. }
  204. return avpkt->size;
  205. }
  206. static av_cold int aom_free(AVCodecContext *avctx)
  207. {
  208. AV1DecodeContext *ctx = avctx->priv_data;
  209. aom_codec_destroy(&ctx->decoder);
  210. return 0;
  211. }
  212. static av_cold int av1_init(AVCodecContext *avctx)
  213. {
  214. return aom_init(avctx, &aom_codec_av1_dx_algo);
  215. }
  216. AVCodec ff_libaom_av1_decoder = {
  217. .name = "libaom-av1",
  218. .long_name = NULL_IF_CONFIG_SMALL("libaom AV1"),
  219. .type = AVMEDIA_TYPE_VIDEO,
  220. .id = AV_CODEC_ID_AV1,
  221. .priv_data_size = sizeof(AV1DecodeContext),
  222. .init = av1_init,
  223. .close = aom_free,
  224. .decode = aom_decode,
  225. .capabilities = AV_CODEC_CAP_OTHER_THREADS | AV_CODEC_CAP_DR1,
  226. .caps_internal = FF_CODEC_CAP_AUTO_THREADS,
  227. .profiles = NULL_IF_CONFIG_SMALL(ff_av1_profiles),
  228. .wrapper_name = "libaom",
  229. };