FFmpeg  4.0
libaomdec.c
Go to the documentation of this file.
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;
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 {
65  int i;
66 
67  for (i = 0; i < desc->nb_components; i++) {
68  int w = img->d_w;
69  int h = img->d_h;
70  int x, y;
71 
72  if (i) {
73  w = (w + img->x_chroma_shift) >> img->x_chroma_shift;
74  h = (h + img->y_chroma_shift) >> img->y_chroma_shift;
75  }
76 
77  for (y = 0; y < h; y++) {
78  uint16_t *src = (uint16_t *)(img->planes[i] + y * img->stride[i]);
79  uint8_t *dst = pic->data[i] + y * pic->linesize[i];
80  for (x = 0; x < w; x++)
81  *dst++ = *src++;
82  }
83  }
84 }
85 
86 // returns 0 on success, AVERROR_INVALIDDATA otherwise
87 static int set_pix_fmt(AVCodecContext *avctx, struct aom_image *img)
88 {
89  static const enum AVColorRange color_ranges[] = {
91  };
92  avctx->color_range = color_ranges[img->range];
93  avctx->color_primaries = img->cp;
94  avctx->colorspace = img->mc;
95  avctx->color_trc = img->tc;
96 
97  switch (img->fmt) {
98  case AOM_IMG_FMT_I420:
99  case AOM_IMG_FMT_I42016:
100  if (img->bit_depth == 8) {
101  avctx->pix_fmt = img->monochrome ?
103  avctx->profile = FF_PROFILE_AV1_MAIN;
104  return 0;
105  } else if (img->bit_depth == 10) {
106  avctx->pix_fmt = img->monochrome ?
108  avctx->profile = FF_PROFILE_AV1_MAIN;
109  return 0;
110  } else if (img->bit_depth == 12) {
111  avctx->pix_fmt = img->monochrome ?
114  return 0;
115  } else {
116  return AVERROR_INVALIDDATA;
117  }
118  case AOM_IMG_FMT_I422:
119  case AOM_IMG_FMT_I42216:
120  if (img->bit_depth == 8) {
121  avctx->pix_fmt = AV_PIX_FMT_YUV422P;
123  return 0;
124  } else if (img->bit_depth == 10) {
125  avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
127  return 0;
128  } else if (img->bit_depth == 12) {
129  avctx->pix_fmt = AV_PIX_FMT_YUV422P12;
131  return 0;
132  } else {
133  return AVERROR_INVALIDDATA;
134  }
135  case AOM_IMG_FMT_I444:
136  case AOM_IMG_FMT_I44416:
137  if (img->bit_depth == 8) {
138  avctx->pix_fmt = AV_PIX_FMT_YUV444P;
139  avctx->profile = FF_PROFILE_AV1_HIGH;
140  return 0;
141  } else if (img->bit_depth == 10) {
142  avctx->pix_fmt = AV_PIX_FMT_YUV444P10;
143  avctx->profile = FF_PROFILE_AV1_HIGH;
144  return 0;
145  } else if (img->bit_depth == 12) {
146  avctx->pix_fmt = AV_PIX_FMT_YUV444P12;
148  return 0;
149  } else {
150  return AVERROR_INVALIDDATA;
151  }
152 
153  default:
154  return AVERROR_INVALIDDATA;
155  }
156 }
157 
158 static int aom_decode(AVCodecContext *avctx, void *data, int *got_frame,
159  AVPacket *avpkt)
160 {
161  AV1DecodeContext *ctx = avctx->priv_data;
162  AVFrame *picture = data;
163  const void *iter = NULL;
164  struct aom_image *img;
165  int ret;
166 
167  if (aom_codec_decode(&ctx->decoder, avpkt->data, avpkt->size, NULL) !=
168  AOM_CODEC_OK) {
169  const char *error = aom_codec_error(&ctx->decoder);
170  const char *detail = aom_codec_error_detail(&ctx->decoder);
171 
172  av_log(avctx, AV_LOG_ERROR, "Failed to decode frame: %s\n", error);
173  if (detail)
174  av_log(avctx, AV_LOG_ERROR, " Additional information: %s\n",
175  detail);
176  return AVERROR_INVALIDDATA;
177  }
178 
179  if ((img = aom_codec_get_frame(&ctx->decoder, &iter))) {
180  if (img->d_w > img->w || img->d_h > img->h) {
181  av_log(avctx, AV_LOG_ERROR, "Display dimensions %dx%d exceed storage %dx%d\n",
182  img->d_w, img->d_h, img->w, img->h);
183  return AVERROR_EXTERNAL;
184  }
185 
186  if ((ret = set_pix_fmt(avctx, img)) < 0) {
187  av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (%d) / bit_depth (%d)\n",
188  img->fmt, img->bit_depth);
189  return ret;
190  }
191 
192  if ((int)img->d_w != avctx->width || (int)img->d_h != avctx->height) {
193  av_log(avctx, AV_LOG_INFO, "dimension change! %dx%d -> %dx%d\n",
194  avctx->width, avctx->height, img->d_w, img->d_h);
195  ret = ff_set_dimensions(avctx, img->d_w, img->d_h);
196  if (ret < 0)
197  return ret;
198  }
199  if ((ret = ff_get_buffer(avctx, picture, 0)) < 0)
200  return ret;
201  if ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) && img->bit_depth == 8)
202  image_copy_16_to_8(picture, img);
203  else
204  av_image_copy(picture->data, picture->linesize, (const uint8_t **)img->planes,
205  img->stride, avctx->pix_fmt, img->d_w, img->d_h);
206  *got_frame = 1;
207  }
208  return avpkt->size;
209 }
210 
211 static av_cold int aom_free(AVCodecContext *avctx)
212 {
213  AV1DecodeContext *ctx = avctx->priv_data;
214  aom_codec_destroy(&ctx->decoder);
215  return 0;
216 }
217 
218 static av_cold int av1_init(AVCodecContext *avctx)
219 {
220  return aom_init(avctx, &aom_codec_av1_dx_algo);
221 }
222 
224  .name = "libaom-av1",
225  .long_name = NULL_IF_CONFIG_SMALL("libaom AV1"),
226  .type = AVMEDIA_TYPE_VIDEO,
227  .id = AV_CODEC_ID_AV1,
228  .priv_data_size = sizeof(AV1DecodeContext),
229  .init = av1_init,
230  .close = aom_free,
231  .decode = aom_decode,
234  .wrapper_name = "libaom",
235 };
#define NULL
Definition: coverity.c:32
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2363
This structure describes decoded (raw) audio or video data.
Definition: frame.h:218
static av_cold int aom_free(AVCodecContext *avctx)
Definition: libaomdec.c:211
static av_cold int aom_init(AVCodecContext *avctx, const struct aom_codec_iface *iface)
Definition: libaomdec.c:40
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:67
misc image utilities
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:104
const char * desc
Definition: nvenc.c:65
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
static int aom_decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: libaomdec.c:158
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:2148
int size
Definition: avcodec.h:1431
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1727
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:372
#define AV_CODEC_CAP_AUTO_THREADS
Codec supports avctx->thread_count == 0 (auto).
Definition: avcodec.h:1027
#define src
Definition: vp8dsp.c:254
struct aom_codec_ctx decoder
Definition: libaomdec.c:37
int profile
profile
Definition: avcodec.h:2843
AVCodec.
Definition: avcodec.h:3408
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
#define AV_PIX_FMT_GRAY10
Definition: pixfmt.h:350
#define img
#define AV_PIX_FMT_GRAY12
Definition: pixfmt.h:351
uint8_t
#define av_cold
Definition: attributes.h:82
const char data[16]
Definition: mxf.c:90
uint8_t * data
Definition: avcodec.h:1430
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
AVColorRange
MPEG vs JPEG YUV range.
Definition: pixfmt.h:496
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:373
#define av_log(a,...)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
const char * name
Name of the codec implementation.
Definition: avcodec.h:3415
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:371
void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4], const uint8_t *src_data[4], const int src_linesizes[4], enum AVPixelFormat pix_fmt, int width, int height)
Copy image in src_data to dst_data.
Definition: imgutils.c:387
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:66
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition: pixdesc.h:83
#define FFMIN(a, b)
Definition: common.h:96
int width
picture width / height.
Definition: avcodec.h:1690
uint8_t w
Definition: llviddspenc.c:38
AVCodec ff_libaom_av1_decoder
Definition: libaomdec.c:223
AVFormatContext * ctx
Definition: movenc.c:48
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:2127
static void error(const char *err)
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:2769
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:499
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:291
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
static void image_copy_16_to_8(AVFrame *pic, struct aom_image *img)
Definition: libaomdec.c:62
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:249
const AVProfile ff_av1_profiles[]
Definition: profiles.c:143
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
main external API structure.
Definition: avcodec.h:1518
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1891
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:368
static const AVProfile profiles[]
#define FF_PROFILE_AV1_PROFESSIONAL
Definition: avcodec.h:2938
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:2141
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:2134
static int set_pix_fmt(AVCodecContext *avctx, struct aom_image *img)
Definition: libaomdec.c:87
#define FF_PROFILE_AV1_MAIN
Definition: avcodec.h:2936
static av_cold int av1_init(AVCodecContext *avctx)
Definition: libaomdec.c:218
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:369
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:375
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:232
the normal 219*2^(n-8) "MPEG" YUV ranges
Definition: pixfmt.h:498
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:62
Y , 8bpp.
Definition: pixfmt.h:70
common internal api header.
common internal and external API header
void * priv_data
Definition: avcodec.h:1545
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:57
This structure stores compressed data.
Definition: avcodec.h:1407
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:959
#define FF_PROFILE_AV1_HIGH
Definition: avcodec.h:2937