2 * Copyright (c) 2010, Google, Inc.
4 * This file is part of FFmpeg.
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.
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.
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
23 * AV1 decoder support via libaom
26 #include <aom/aom_decoder.h>
27 #include <aom/aomdx.h>
29 #include "libavutil/common.h"
30 #include "libavutil/imgutils.h"
36 typedef struct AV1DecodeContext {
37 struct aom_codec_ctx decoder;
40 static av_cold int aom_init(AVCodecContext *avctx,
41 const struct aom_codec_iface *iface)
43 AV1DecodeContext *ctx = avctx->priv_data;
44 struct aom_codec_dec_cfg deccfg = {
45 /* token partitions+1 would be a decent choice */
46 .threads = FFMIN(avctx->thread_count, 16)
49 av_log(avctx, AV_LOG_INFO, "%s\n", aom_codec_version_str());
50 av_log(avctx, AV_LOG_VERBOSE, "%s\n", aom_codec_build_config());
52 if (aom_codec_dec_init(&ctx->decoder, iface, &deccfg, 0) != AOM_CODEC_OK) {
53 const char *error = aom_codec_error(&ctx->decoder);
54 av_log(avctx, AV_LOG_ERROR, "Failed to initialize decoder: %s\n",
56 return AVERROR(EINVAL);
62 static void image_copy_16_to_8(AVFrame *pic, struct aom_image *img)
66 for (i = 0; i < 3; i++) {
72 w = (w + img->x_chroma_shift) >> img->x_chroma_shift;
73 h = (h + img->y_chroma_shift) >> img->y_chroma_shift;
76 for (y = 0; y < h; y++) {
77 uint16_t *src = (uint16_t *)(img->planes[i] + y * img->stride[i]);
78 uint8_t *dst = pic->data[i] + y * pic->linesize[i];
79 for (x = 0; x < w; x++)
85 // returns 0 on success, AVERROR_INVALIDDATA otherwise
86 static int set_pix_fmt(AVCodecContext *avctx, struct aom_image *img)
88 static const enum AVColorRange color_ranges[] = {
89 AVCOL_RANGE_MPEG, AVCOL_RANGE_JPEG
91 avctx->color_range = color_ranges[img->range];
92 avctx->colorspace = img->mc;
95 case AOM_IMG_FMT_I420:
96 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
97 avctx->profile = FF_PROFILE_AV1_MAIN;
99 case AOM_IMG_FMT_I422:
100 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
101 avctx->profile = FF_PROFILE_AV1_PROFESSIONAL;
103 case AOM_IMG_FMT_I444:
104 avctx->pix_fmt = avctx->colorspace == AVCOL_SPC_RGB ?
105 AV_PIX_FMT_GBRP : AV_PIX_FMT_YUV444P;
106 avctx->profile = FF_PROFILE_AV1_HIGH;
108 case AOM_IMG_FMT_I42016:
109 if (img->bit_depth == 8) {
110 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
111 avctx->profile = FF_PROFILE_AV1_MAIN;
113 } else if (img->bit_depth == 10) {
114 avctx->pix_fmt = AV_PIX_FMT_YUV420P10;
115 avctx->profile = FF_PROFILE_AV1_MAIN;
117 } else if (img->bit_depth == 12) {
118 avctx->pix_fmt = AV_PIX_FMT_YUV420P12;
119 avctx->profile = FF_PROFILE_AV1_PROFESSIONAL;
122 return AVERROR_INVALIDDATA;
124 case AOM_IMG_FMT_I42216:
125 if (img->bit_depth == 8) {
126 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
127 avctx->profile = FF_PROFILE_AV1_PROFESSIONAL;
129 } else if (img->bit_depth == 10) {
130 avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
131 avctx->profile = FF_PROFILE_AV1_PROFESSIONAL;
133 } else if (img->bit_depth == 12) {
134 avctx->pix_fmt = AV_PIX_FMT_YUV422P12;
135 avctx->profile = FF_PROFILE_AV1_PROFESSIONAL;
138 return AVERROR_INVALIDDATA;
140 case AOM_IMG_FMT_I44416:
141 if (img->bit_depth == 8) {
142 avctx->pix_fmt = avctx->colorspace == AVCOL_SPC_RGB ?
143 AV_PIX_FMT_GBRP : AV_PIX_FMT_YUV444P;
144 avctx->profile = FF_PROFILE_AV1_HIGH;
146 } else if (img->bit_depth == 10) {
147 avctx->pix_fmt = avctx->colorspace == AVCOL_SPC_RGB ?
148 AV_PIX_FMT_GBRP10 : AV_PIX_FMT_YUV444P10;
149 avctx->profile = FF_PROFILE_AV1_HIGH;
151 } else if (img->bit_depth == 12) {
152 avctx->pix_fmt = avctx->colorspace == AVCOL_SPC_RGB ?
153 AV_PIX_FMT_GBRP12 : AV_PIX_FMT_YUV444P12;
154 avctx->profile = FF_PROFILE_AV1_PROFESSIONAL;
157 return AVERROR_INVALIDDATA;
161 return AVERROR_INVALIDDATA;
165 static int aom_decode(AVCodecContext *avctx, void *data, int *got_frame,
168 AV1DecodeContext *ctx = avctx->priv_data;
169 AVFrame *picture = data;
170 const void *iter = NULL;
171 struct aom_image *img;
174 if (aom_codec_decode(&ctx->decoder, avpkt->data, avpkt->size, NULL) !=
176 const char *error = aom_codec_error(&ctx->decoder);
177 const char *detail = aom_codec_error_detail(&ctx->decoder);
179 av_log(avctx, AV_LOG_ERROR, "Failed to decode frame: %s\n", error);
181 av_log(avctx, AV_LOG_ERROR, " Additional information: %s\n",
183 return AVERROR_INVALIDDATA;
186 if ((img = aom_codec_get_frame(&ctx->decoder, &iter))) {
187 if (img->d_w > img->w || img->d_h > img->h) {
188 av_log(avctx, AV_LOG_ERROR, "Display dimensions %dx%d exceed storage %dx%d\n",
189 img->d_w, img->d_h, img->w, img->h);
190 return AVERROR_EXTERNAL;
193 if ((ret = set_pix_fmt(avctx, img)) < 0) {
194 av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (%d) / bit_depth (%d)\n",
195 img->fmt, img->bit_depth);
199 if ((int)img->d_w != avctx->width || (int)img->d_h != avctx->height) {
200 av_log(avctx, AV_LOG_INFO, "dimension change! %dx%d -> %dx%d\n",
201 avctx->width, avctx->height, img->d_w, img->d_h);
202 ret = ff_set_dimensions(avctx, img->d_w, img->d_h);
206 if ((ret = ff_get_buffer(avctx, picture, 0)) < 0)
208 if ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) && img->bit_depth == 8)
209 image_copy_16_to_8(picture, img);
211 av_image_copy(picture->data, picture->linesize, (const uint8_t **)img->planes,
212 img->stride, avctx->pix_fmt, img->d_w, img->d_h);
218 static av_cold int aom_free(AVCodecContext *avctx)
220 AV1DecodeContext *ctx = avctx->priv_data;
221 aom_codec_destroy(&ctx->decoder);
225 static av_cold int av1_init(AVCodecContext *avctx)
227 return aom_init(avctx, &aom_codec_av1_dx_algo);
230 AVCodec ff_libaom_av1_decoder = {
231 .name = "libaom-av1",
232 .long_name = NULL_IF_CONFIG_SMALL("libaom AV1"),
233 .type = AVMEDIA_TYPE_VIDEO,
234 .id = AV_CODEC_ID_AV1,
235 .priv_data_size = sizeof(AV1DecodeContext),
238 .decode = aom_decode,
239 .capabilities = AV_CODEC_CAP_AUTO_THREADS | AV_CODEC_CAP_DR1,
240 .profiles = NULL_IF_CONFIG_SMALL(ff_av1_profiles),
241 .wrapper_name = "libaom",