]> git.sesse.net Git - ffmpeg/blob - libavcodec/libaomdec.c
lavc: prefer the mp3float decoder to the mp3 decoder
[ffmpeg] / libavcodec / libaomdec.c
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 /**
22  * @file
23  * AV1 decoder support via libaom
24  */
25
26 #include <aom/aom_decoder.h>
27 #include <aom/aomdx.h>
28
29 #include "libavutil/common.h"
30 #include "libavutil/imgutils.h"
31
32 #include "avcodec.h"
33 #include "internal.h"
34 #include "profiles.h"
35
36 typedef struct AV1DecodeContext {
37     struct aom_codec_ctx decoder;
38 } AV1DecodeContext;
39
40 static av_cold int aom_init(AVCodecContext *avctx,
41                             const struct aom_codec_iface *iface)
42 {
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)
47     };
48
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());
51
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",
55                error);
56         return AVERROR(EINVAL);
57     }
58
59     return 0;
60 }
61
62 static void image_copy_16_to_8(AVFrame *pic, struct aom_image *img)
63 {
64     int i;
65
66     for (i = 0; i < 3; i++) {
67         int w = img->d_w;
68         int h = img->d_h;
69         int x, y;
70
71         if (i) {
72             w = (w + img->x_chroma_shift) >> img->x_chroma_shift;
73             h = (h + img->y_chroma_shift) >> img->y_chroma_shift;
74         }
75
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++)
80                 *dst++ = *src++;
81         }
82     }
83 }
84
85 // returns 0 on success, AVERROR_INVALIDDATA otherwise
86 static int set_pix_fmt(AVCodecContext *avctx, struct aom_image *img)
87 {
88     static const enum AVColorRange color_ranges[] = {
89         AVCOL_RANGE_MPEG, AVCOL_RANGE_JPEG
90     };
91     avctx->color_range = color_ranges[img->range];
92     avctx->color_primaries = img->cp;
93     avctx->colorspace  = img->mc;
94     avctx->color_trc   = img->tc;
95
96     switch (img->fmt) {
97     case AOM_IMG_FMT_I420:
98         avctx->pix_fmt = AV_PIX_FMT_YUV420P;
99         avctx->profile = FF_PROFILE_AV1_MAIN;
100         return 0;
101     case AOM_IMG_FMT_I422:
102         avctx->pix_fmt = AV_PIX_FMT_YUV422P;
103         avctx->profile = FF_PROFILE_AV1_PROFESSIONAL;
104         return 0;
105     case AOM_IMG_FMT_I444:
106         avctx->pix_fmt = avctx->colorspace == AVCOL_SPC_RGB ?
107                          AV_PIX_FMT_GBRP : AV_PIX_FMT_YUV444P;
108         avctx->profile = FF_PROFILE_AV1_HIGH;
109         return 0;
110     case AOM_IMG_FMT_I42016:
111         if (img->bit_depth == 8) {
112             avctx->pix_fmt = AV_PIX_FMT_YUV420P;
113             avctx->profile = FF_PROFILE_AV1_MAIN;
114             return 0;
115         } else if (img->bit_depth == 10) {
116             avctx->pix_fmt = AV_PIX_FMT_YUV420P10;
117             avctx->profile = FF_PROFILE_AV1_MAIN;
118             return 0;
119         } else if (img->bit_depth == 12) {
120             avctx->pix_fmt = AV_PIX_FMT_YUV420P12;
121             avctx->profile = FF_PROFILE_AV1_PROFESSIONAL;
122             return 0;
123         } else {
124             return AVERROR_INVALIDDATA;
125         }
126     case AOM_IMG_FMT_I42216:
127         if (img->bit_depth == 8) {
128             avctx->pix_fmt = AV_PIX_FMT_YUV422P;
129             avctx->profile = FF_PROFILE_AV1_PROFESSIONAL;
130             return 0;
131         } else if (img->bit_depth == 10) {
132             avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
133             avctx->profile = FF_PROFILE_AV1_PROFESSIONAL;
134             return 0;
135         } else if (img->bit_depth == 12) {
136             avctx->pix_fmt = AV_PIX_FMT_YUV422P12;
137             avctx->profile = FF_PROFILE_AV1_PROFESSIONAL;
138             return 0;
139         } else {
140             return AVERROR_INVALIDDATA;
141         }
142     case AOM_IMG_FMT_I44416:
143         if (img->bit_depth == 8) {
144             avctx->pix_fmt = avctx->colorspace == AVCOL_SPC_RGB ?
145                              AV_PIX_FMT_GBRP : AV_PIX_FMT_YUV444P;
146             avctx->profile = FF_PROFILE_AV1_HIGH;
147             return 0;
148         } else if (img->bit_depth == 10) {
149             avctx->pix_fmt = avctx->colorspace == AVCOL_SPC_RGB ?
150                              AV_PIX_FMT_GBRP10 : AV_PIX_FMT_YUV444P10;
151             avctx->profile = FF_PROFILE_AV1_HIGH;
152             return 0;
153         } else if (img->bit_depth == 12) {
154             avctx->pix_fmt = avctx->colorspace == AVCOL_SPC_RGB ?
155                              AV_PIX_FMT_GBRP12 : AV_PIX_FMT_YUV444P12;
156             avctx->profile = FF_PROFILE_AV1_PROFESSIONAL;
157             return 0;
158         } else {
159             return AVERROR_INVALIDDATA;
160         }
161
162     default:
163         return AVERROR_INVALIDDATA;
164     }
165 }
166
167 static int aom_decode(AVCodecContext *avctx, void *data, int *got_frame,
168                       AVPacket *avpkt)
169 {
170     AV1DecodeContext *ctx = avctx->priv_data;
171     AVFrame *picture      = data;
172     const void *iter      = NULL;
173     struct aom_image *img;
174     int ret;
175
176     if (aom_codec_decode(&ctx->decoder, avpkt->data, avpkt->size, NULL) !=
177         AOM_CODEC_OK) {
178         const char *error  = aom_codec_error(&ctx->decoder);
179         const char *detail = aom_codec_error_detail(&ctx->decoder);
180
181         av_log(avctx, AV_LOG_ERROR, "Failed to decode frame: %s\n", error);
182         if (detail)
183             av_log(avctx, AV_LOG_ERROR, "  Additional information: %s\n",
184                    detail);
185         return AVERROR_INVALIDDATA;
186     }
187
188     if ((img = aom_codec_get_frame(&ctx->decoder, &iter))) {
189         if (img->d_w > img->w || img->d_h > img->h) {
190             av_log(avctx, AV_LOG_ERROR, "Display dimensions %dx%d exceed storage %dx%d\n",
191                    img->d_w, img->d_h, img->w, img->h);
192             return AVERROR_EXTERNAL;
193         }
194
195         if ((ret = set_pix_fmt(avctx, img)) < 0) {
196             av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (%d) / bit_depth (%d)\n",
197                    img->fmt, img->bit_depth);
198             return ret;
199         }
200
201         if ((int)img->d_w != avctx->width || (int)img->d_h != avctx->height) {
202             av_log(avctx, AV_LOG_INFO, "dimension change! %dx%d -> %dx%d\n",
203                    avctx->width, avctx->height, img->d_w, img->d_h);
204             ret = ff_set_dimensions(avctx, img->d_w, img->d_h);
205             if (ret < 0)
206                 return ret;
207         }
208         if ((ret = ff_get_buffer(avctx, picture, 0)) < 0)
209             return ret;
210         if ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) && img->bit_depth == 8)
211             image_copy_16_to_8(picture, img);
212         else
213             av_image_copy(picture->data, picture->linesize, (const uint8_t **)img->planes,
214                           img->stride, avctx->pix_fmt, img->d_w, img->d_h);
215         *got_frame = 1;
216     }
217     return avpkt->size;
218 }
219
220 static av_cold int aom_free(AVCodecContext *avctx)
221 {
222     AV1DecodeContext *ctx = avctx->priv_data;
223     aom_codec_destroy(&ctx->decoder);
224     return 0;
225 }
226
227 static av_cold int av1_init(AVCodecContext *avctx)
228 {
229     return aom_init(avctx, &aom_codec_av1_dx_algo);
230 }
231
232 AVCodec ff_libaom_av1_decoder = {
233     .name           = "libaom-av1",
234     .long_name      = NULL_IF_CONFIG_SMALL("libaom AV1"),
235     .type           = AVMEDIA_TYPE_VIDEO,
236     .id             = AV_CODEC_ID_AV1,
237     .priv_data_size = sizeof(AV1DecodeContext),
238     .init           = av1_init,
239     .close          = aom_free,
240     .decode         = aom_decode,
241     .capabilities   = AV_CODEC_CAP_AUTO_THREADS | AV_CODEC_CAP_DR1,
242     .profiles       = NULL_IF_CONFIG_SMALL(ff_av1_profiles),
243     .wrapper_name   = "libaom",
244 };