FFmpeg
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/cpu.h"
31 #include "libavutil/imgutils.h"
32 
33 #include "avcodec.h"
34 #include "codec_internal.h"
35 #include "decode.h"
36 #include "libaom.h"
37 #include "profiles.h"
38 
39 typedef struct AV1DecodeContext {
40  struct aom_codec_ctx decoder;
42 
43 static av_cold int aom_init(AVCodecContext *avctx,
44  const struct aom_codec_iface *iface)
45 {
46  AV1DecodeContext *ctx = avctx->priv_data;
47  struct aom_codec_dec_cfg deccfg = {
48  .threads = FFMIN(avctx->thread_count ? avctx->thread_count : av_cpu_count(), 16)
49  };
50 
51  av_log(avctx, AV_LOG_VERBOSE, "%s\n", aom_codec_version_str());
52  av_log(avctx, AV_LOG_VERBOSE, "%s\n", aom_codec_build_config());
53 
54  if (aom_codec_dec_init(&ctx->decoder, iface, &deccfg, 0) != AOM_CODEC_OK) {
55  const char *error = aom_codec_error(&ctx->decoder);
56  av_log(avctx, AV_LOG_ERROR, "Failed to initialize decoder: %s\n",
57  error);
58  return AVERROR(EINVAL);
59  }
60 
61  return 0;
62 }
63 
64 // returns 0 on success, AVERROR_INVALIDDATA otherwise
65 static int set_pix_fmt(AVCodecContext *avctx, struct aom_image *img)
66 {
67  static const enum AVColorRange color_ranges[] = {
69  };
70  avctx->color_range = color_ranges[img->range];
71  avctx->color_primaries = img->cp;
72  avctx->colorspace = img->mc;
73  avctx->color_trc = img->tc;
74 
75  switch (img->fmt) {
76  case AOM_IMG_FMT_I420:
77  case AOM_IMG_FMT_I42016:
78  if (img->bit_depth == 8) {
79  avctx->pix_fmt = img->monochrome ?
82  return 0;
83  } else if (img->bit_depth == 10) {
84  avctx->pix_fmt = img->monochrome ?
87  return 0;
88  } else if (img->bit_depth == 12) {
89  avctx->pix_fmt = img->monochrome ?
92  return 0;
93  } else {
94  return AVERROR_INVALIDDATA;
95  }
96  case AOM_IMG_FMT_I422:
97  case AOM_IMG_FMT_I42216:
98  if (img->bit_depth == 8) {
99  avctx->pix_fmt = AV_PIX_FMT_YUV422P;
101  return 0;
102  } else if (img->bit_depth == 10) {
103  avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
105  return 0;
106  } else if (img->bit_depth == 12) {
107  avctx->pix_fmt = AV_PIX_FMT_YUV422P12;
109  return 0;
110  } else {
111  return AVERROR_INVALIDDATA;
112  }
113  case AOM_IMG_FMT_I444:
114  case AOM_IMG_FMT_I44416:
115  if (img->bit_depth == 8) {
116  avctx->pix_fmt = avctx->colorspace == AVCOL_SPC_RGB ?
118  avctx->profile = AV_PROFILE_AV1_HIGH;
119  return 0;
120  } else if (img->bit_depth == 10) {
121  avctx->pix_fmt = AV_PIX_FMT_YUV444P10;
122  avctx->pix_fmt = avctx->colorspace == AVCOL_SPC_RGB ?
124  avctx->profile = AV_PROFILE_AV1_HIGH;
125  return 0;
126  } else if (img->bit_depth == 12) {
127  avctx->pix_fmt = avctx->colorspace == AVCOL_SPC_RGB ?
130  return 0;
131  } else {
132  return AVERROR_INVALIDDATA;
133  }
134 
135  default:
136  return AVERROR_INVALIDDATA;
137  }
138 }
139 
140 static int aom_decode(AVCodecContext *avctx, AVFrame *picture,
141  int *got_frame, AVPacket *avpkt)
142 {
143  AV1DecodeContext *ctx = avctx->priv_data;
144  const void *iter = NULL;
145  struct aom_image *img;
146  int ret;
147 
148  if (aom_codec_decode(&ctx->decoder, avpkt->data, avpkt->size, NULL) !=
149  AOM_CODEC_OK) {
150  const char *error = aom_codec_error(&ctx->decoder);
151  const char *detail = aom_codec_error_detail(&ctx->decoder);
152 
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 
160  if ((img = aom_codec_get_frame(&ctx->decoder, &iter))) {
161  if (img->d_w > img->w || img->d_h > img->h) {
162  av_log(avctx, AV_LOG_ERROR, "Display dimensions %dx%d exceed storage %dx%d\n",
163  img->d_w, img->d_h, img->w, img->h);
164  return AVERROR_EXTERNAL;
165  }
166 
167  if ((ret = set_pix_fmt(avctx, img)) < 0) {
168  av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (%d) / bit_depth (%d)\n",
169  img->fmt, img->bit_depth);
170  return ret;
171  }
172 
173  if ((int)img->d_w != avctx->width || (int)img->d_h != avctx->height) {
174  av_log(avctx, AV_LOG_INFO, "dimension change! %dx%d -> %dx%d\n",
175  avctx->width, avctx->height, img->d_w, img->d_h);
176  ret = ff_set_dimensions(avctx, img->d_w, img->d_h);
177  if (ret < 0)
178  return ret;
179  }
180  if ((ret = ff_get_buffer(avctx, picture, 0)) < 0)
181  return ret;
182 
183 #ifdef AOM_CTRL_AOMD_GET_FRAME_FLAGS
184  {
185  aom_codec_frame_flags_t flags;
186  ret = aom_codec_control(&ctx->decoder, AOMD_GET_FRAME_FLAGS, &flags);
187  if (ret == AOM_CODEC_OK) {
188  if (flags & AOM_FRAME_IS_KEY)
189  picture->flags |= AV_FRAME_FLAG_KEY;
190  else
191  picture->flags &= ~AV_FRAME_FLAG_KEY;
192  if (flags & (AOM_FRAME_IS_KEY | AOM_FRAME_IS_INTRAONLY))
193  picture->pict_type = AV_PICTURE_TYPE_I;
194  else if (flags & AOM_FRAME_IS_SWITCH)
195  picture->pict_type = AV_PICTURE_TYPE_SP;
196  else
197  picture->pict_type = AV_PICTURE_TYPE_P;
198  }
199  }
200 #endif
201 
203  &picture->sample_aspect_ratio.den,
204  picture->height * img->r_w,
205  picture->width * img->r_h,
206  INT_MAX);
207  ff_set_sar(avctx, picture->sample_aspect_ratio);
208 
209  if ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) && img->bit_depth == 8)
210  ff_aom_image_copy_16_to_8(picture, img);
211  else {
212  const uint8_t *planes[4] = { img->planes[0], img->planes[1], img->planes[2] };
213  const int stride[4] = { img->stride[0], img->stride[1], img->stride[2] };
214 
215  av_image_copy(picture->data, picture->linesize, planes,
216  stride, avctx->pix_fmt, img->d_w, img->d_h);
217  }
218  *got_frame = 1;
219  }
220  return avpkt->size;
221 }
222 
223 static av_cold int aom_free(AVCodecContext *avctx)
224 {
225  AV1DecodeContext *ctx = avctx->priv_data;
226  aom_codec_destroy(&ctx->decoder);
227  return 0;
228 }
229 
230 static av_cold int av1_init(AVCodecContext *avctx)
231 {
232  return aom_init(avctx, aom_codec_av1_dx());
233 }
234 
236  .p.name = "libaom-av1",
237  CODEC_LONG_NAME("libaom AV1"),
238  .p.type = AVMEDIA_TYPE_VIDEO,
239  .p.id = AV_CODEC_ID_AV1,
240  .priv_data_size = sizeof(AV1DecodeContext),
241  .init = av1_init,
242  .close = aom_free,
244  .p.capabilities = AV_CODEC_CAP_OTHER_THREADS | AV_CODEC_CAP_DR1,
245  .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE |
247  .p.profiles = NULL_IF_CONFIG_SMALL(ff_av1_profiles),
248  .p.wrapper_name = "libaom",
249 };
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:32
aom_free
static av_cold int aom_free(AVCodecContext *avctx)
Definition: libaomdec.c:223
aom_init
static av_cold int aom_init(AVCodecContext *avctx, const struct aom_codec_iface *iface)
Definition: libaomdec.c:43
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVCodecContext::colorspace
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:691
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
AVCodecContext::color_trc
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:684
AVFrame::width
int width
Definition: frame.h:461
AVCOL_RANGE_JPEG
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:717
AVPacket::data
uint8_t * data
Definition: packet.h:539
set_pix_fmt
static int set_pix_fmt(AVCodecContext *avctx, struct aom_image *img)
Definition: libaomdec.c:65
AV_PIX_FMT_YUV420P10
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:502
FF_CODEC_CAP_NOT_INIT_THREADSAFE
#define FF_CODEC_CAP_NOT_INIT_THREADSAFE
The codec is not known to be init-threadsafe (i.e.
Definition: codec_internal.h:35
FFCodec
Definition: codec_internal.h:127
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:225
AVCOL_SPC_RGB
@ AVCOL_SPC_RGB
order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB), YZX and ST 428-1
Definition: pixfmt.h:641
AVFrame::flags
int flags
Frame flags, a combination of AV_FRAME_FLAGS.
Definition: frame.h:661
AV_PROFILE_AV1_PROFESSIONAL
#define AV_PROFILE_AV1_PROFESSIONAL
Definition: defs.h:171
ff_set_dimensions
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:94
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:410
planes
static const struct @466 planes[]
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
AVCodecContext::thread_count
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
Definition: avcodec.h:1593
AV_PIX_FMT_GBRP10
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:518
av_reduce
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
AVRational::num
int num
Numerator.
Definition: rational.h:59
AV_PIX_FMT_YUV444P10
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:505
AV1DecodeContext::decoder
struct aom_codec_ctx decoder
Definition: libaomdec.c:40
AVCodecContext::color_primaries
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:677
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
av_cold
#define av_cold
Definition: attributes.h:90
AV_FRAME_FLAG_KEY
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition: frame.h:640
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:311
AV_CODEC_CAP_OTHER_THREADS
#define AV_CODEC_CAP_OTHER_THREADS
Codec supports multithreading through a method other than slice- or frame-level multithreading.
Definition: codec.h:124
ctx
AVFormatContext * ctx
Definition: movenc.c:49
decode.h
ff_av1_profiles
const AVProfile ff_av1_profiles[]
Definition: profiles.c:163
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:73
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:296
AV_PIX_FMT_GRAY10
#define AV_PIX_FMT_GRAY10
Definition: pixfmt.h:483
NULL
#define NULL
Definition: coverity.c:32
AVCodecContext::color_range
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:701
AV_CODEC_ID_AV1
@ AV_CODEC_ID_AV1
Definition: codec_id.h:284
ff_libaom_av1_decoder
const FFCodec ff_libaom_av1_decoder
Definition: libaomdec.c:235
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:279
profiles.h
ff_set_sar
int ff_set_sar(AVCodecContext *avctx, AVRational sar)
Check that the provided sample aspect ratio is valid and set it on the codec context.
Definition: utils.c:109
AV_PIX_FMT_YUV422P10
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:503
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:81
AV_PICTURE_TYPE_SP
@ AV_PICTURE_TYPE_SP
Switching Predicted.
Definition: avutil.h:284
av_cpu_count
int av_cpu_count(void)
Definition: cpu.c:217
AVFrame::pict_type
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:491
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1697
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:540
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
av1_init
static av_cold int av1_init(AVCodecContext *avctx)
Definition: libaomdec.c:230
codec_internal.h
cpu.h
AV_PIX_FMT_YUV422P12
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:507
AV_PIX_FMT_YUV444P12
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:509
aom_decode
static int aom_decode(AVCodecContext *avctx, AVFrame *picture, int *got_frame, AVPacket *avpkt)
Definition: libaomdec.c:140
img
#define img
Definition: vf_colormatrix.c:114
AVERROR_EXTERNAL
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:59
AV_PROFILE_AV1_HIGH
#define AV_PROFILE_AV1_HIGH
Definition: defs.h:170
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:220
AV_PIX_FMT_GBRP12
#define AV_PIX_FMT_GBRP12
Definition: pixfmt.h:519
common.h
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
AVCodecContext::height
int height
Definition: avcodec.h:624
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:663
AVCOL_RANGE_MPEG
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition: pixfmt.h:700
avcodec.h
stride
#define stride
Definition: h264pred_template.c:537
ret
ret
Definition: filter_design.txt:187
AVFrame::sample_aspect_ratio
AVRational sample_aspect_ratio
Sample aspect ratio for the video frame, 0/1 if unknown/unspecified.
Definition: frame.h:496
AV_PIX_FMT_YUV420P12
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:506
AVCodecContext
main external API structure.
Definition: avcodec.h:451
AVFrame::height
int height
Definition: frame.h:461
AVRational::den
int den
Denominator.
Definition: rational.h:60
AVCodecContext::profile
int profile
profile
Definition: avcodec.h:1650
AV1DecodeContext
Definition: libaomdec.c:39
AV_PIX_FMT_YUV444P
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:78
AV_PIX_FMT_GBRP
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:165
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:280
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AV_PIX_FMT_YUV422P
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:77
FF_CODEC_CAP_AUTO_THREADS
#define FF_CODEC_CAP_AUTO_THREADS
Codec handles avctx->thread_count == 0 (auto) internally.
Definition: codec_internal.h:73
ff_aom_image_copy_16_to_8
void ff_aom_image_copy_16_to_8(AVFrame *pic, struct aom_image *img)
Definition: libaom.c:27
AVPacket
This structure stores compressed data.
Definition: packet.h:516
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:478
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:624
imgutils.h
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:482
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:434
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
AV_PIX_FMT_GRAY12
#define AV_PIX_FMT_GRAY12
Definition: pixfmt.h:484
av_image_copy
void av_image_copy(uint8_t *const dst_data[4], const int dst_linesizes[4], const uint8_t *const 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:422
libaom.h
AV_PROFILE_AV1_MAIN
#define AV_PROFILE_AV1_MAIN
Definition: defs.h:169
AVColorRange
AVColorRange
Visual content value range.
Definition: pixfmt.h:682