Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2010, Google, Inc. |
| 3 | * |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 4 | * This file is part of FFmpeg. |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 5 | * |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 6 | * FFmpeg is free software; you can redistribute it and/or |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 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 | * |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 11 | * FFmpeg is distributed in the hope that it will be useful, |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 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 |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 17 | * License along with FFmpeg; if not, write to the Free Software |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 21 | /** |
| 22 | * @file |
| 23 | * AV1 encoder support via libaom |
| 24 | */ |
| 25 | |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 26 | #define AOM_DISABLE_CTRL_TYPECHECKS 1 |
| 27 | #include <aom/aom_encoder.h> |
| 28 | #include <aom/aomcx.h> |
| 29 | |
James Almer | c42c99c | 2018-03-30 01:35:33 | [diff] [blame] | 30 | #include "libavutil/avassert.h" |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 31 | #include "libavutil/base64.h" |
| 32 | #include "libavutil/common.h" |
| 33 | #include "libavutil/mathematics.h" |
| 34 | #include "libavutil/opt.h" |
| 35 | #include "libavutil/pixdesc.h" |
| 36 | |
Mark Thompson | 7070955 | 2018-10-22 22:40:07 | [diff] [blame^] | 37 | #include "av1.h" |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 38 | #include "avcodec.h" |
| 39 | #include "internal.h" |
James Almer | c0f0c9f | 2018-03-29 02:28:34 | [diff] [blame] | 40 | #include "profiles.h" |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 41 | |
| 42 | /* |
| 43 | * Portion of struct aom_codec_cx_pkt from aom_encoder.h. |
| 44 | * One encoded frame returned from the library. |
| 45 | */ |
| 46 | struct FrameListData { |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 47 | void *buf; /**< compressed data buffer */ |
| 48 | size_t sz; /**< length of compressed data */ |
| 49 | int64_t pts; /**< time stamp to show frame |
| 50 | (in timebase units) */ |
| 51 | unsigned long duration; /**< duration to show frame |
| 52 | (in timebase units) */ |
| 53 | uint32_t flags; /**< flags for this frame */ |
Sam John | e265832 | 2018-09-28 22:09:05 | [diff] [blame] | 54 | uint64_t sse[4]; |
| 55 | int have_sse; /**< true if we have pending sse[] */ |
| 56 | uint64_t frame_number; |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 57 | struct FrameListData *next; |
| 58 | }; |
| 59 | |
| 60 | typedef struct AOMEncoderContext { |
| 61 | AVClass *class; |
James Almer | a754af9 | 2018-07-07 19:33:16 | [diff] [blame] | 62 | AVBSFContext *bsf; |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 63 | struct aom_codec_ctx encoder; |
| 64 | struct aom_image rawimg; |
| 65 | struct aom_fixed_buf twopass_stats; |
| 66 | struct FrameListData *coded_frame_list; |
| 67 | int cpu_used; |
| 68 | int auto_alt_ref; |
| 69 | int lag_in_frames; |
| 70 | int error_resilient; |
| 71 | int crf; |
| 72 | int static_thresh; |
| 73 | int drop_threshold; |
| 74 | int noise_sensitivity; |
Sam John | e265832 | 2018-09-28 22:09:05 | [diff] [blame] | 75 | uint64_t sse[4]; |
| 76 | int have_sse; /**< true if we have pending sse[] */ |
| 77 | uint64_t frame_number; |
Mark Thompson | 7070955 | 2018-10-22 22:40:07 | [diff] [blame^] | 78 | int tile_cols, tile_rows; |
| 79 | int tile_cols_log2, tile_rows_log2; |
| 80 | aom_superblock_size_t superblock_size; |
| 81 | int uniform_tiles; |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 82 | } AOMContext; |
| 83 | |
| 84 | static const char *const ctlidstr[] = { |
| 85 | [AOME_SET_CPUUSED] = "AOME_SET_CPUUSED", |
| 86 | [AOME_SET_CQ_LEVEL] = "AOME_SET_CQ_LEVEL", |
| 87 | [AOME_SET_ENABLEAUTOALTREF] = "AOME_SET_ENABLEAUTOALTREF", |
| 88 | [AOME_SET_STATIC_THRESHOLD] = "AOME_SET_STATIC_THRESHOLD", |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 89 | [AV1E_SET_COLOR_RANGE] = "AV1E_SET_COLOR_RANGE", |
James Almer | e5819fa | 2018-03-29 04:03:24 | [diff] [blame] | 90 | [AV1E_SET_COLOR_PRIMARIES] = "AV1E_SET_COLOR_PRIMARIES", |
| 91 | [AV1E_SET_MATRIX_COEFFICIENTS] = "AV1E_SET_MATRIX_COEFFICIENTS", |
| 92 | [AV1E_SET_TRANSFER_CHARACTERISTICS] = "AV1E_SET_TRANSFER_CHARACTERISTICS", |
Mark Thompson | 7070955 | 2018-10-22 22:40:07 | [diff] [blame^] | 93 | [AV1E_SET_SUPERBLOCK_SIZE] = "AV1E_SET_SUPERBLOCK_SIZE", |
| 94 | [AV1E_SET_TILE_COLUMNS] = "AV1E_SET_TILE_COLUMNS", |
| 95 | [AV1E_SET_TILE_ROWS] = "AV1E_SET_TILE_ROWS", |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | static av_cold void log_encoder_error(AVCodecContext *avctx, const char *desc) |
| 99 | { |
| 100 | AOMContext *ctx = avctx->priv_data; |
| 101 | const char *error = aom_codec_error(&ctx->encoder); |
| 102 | const char *detail = aom_codec_error_detail(&ctx->encoder); |
| 103 | |
| 104 | av_log(avctx, AV_LOG_ERROR, "%s: %s\n", desc, error); |
| 105 | if (detail) |
| 106 | av_log(avctx, AV_LOG_ERROR, " Additional information: %s\n", detail); |
| 107 | } |
| 108 | |
| 109 | static av_cold void dump_enc_cfg(AVCodecContext *avctx, |
| 110 | const struct aom_codec_enc_cfg *cfg) |
| 111 | { |
| 112 | int width = -30; |
| 113 | int level = AV_LOG_DEBUG; |
| 114 | |
| 115 | av_log(avctx, level, "aom_codec_enc_cfg\n"); |
| 116 | av_log(avctx, level, "generic settings\n" |
| 117 | " %*s%u\n %*s%u\n %*s%u\n %*s%u\n %*s%u\n" |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 118 | " %*s%u\n %*s%u\n" |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 119 | " %*s{%u/%u}\n %*s%u\n %*s%d\n %*s%u\n", |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 120 | width, "g_usage:", cfg->g_usage, |
| 121 | width, "g_threads:", cfg->g_threads, |
| 122 | width, "g_profile:", cfg->g_profile, |
| 123 | width, "g_w:", cfg->g_w, |
| 124 | width, "g_h:", cfg->g_h, |
| 125 | width, "g_bit_depth:", cfg->g_bit_depth, |
| 126 | width, "g_input_bit_depth:", cfg->g_input_bit_depth, |
| 127 | width, "g_timebase:", cfg->g_timebase.num, cfg->g_timebase.den, |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 128 | width, "g_error_resilient:", cfg->g_error_resilient, |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 129 | width, "g_pass:", cfg->g_pass, |
| 130 | width, "g_lag_in_frames:", cfg->g_lag_in_frames); |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 131 | av_log(avctx, level, "rate control settings\n" |
James Almer | 416d354 | 2018-03-29 03:51:48 | [diff] [blame] | 132 | " %*s%u\n %*s%d\n %*s%p(%"SIZE_SPECIFIER")\n %*s%u\n", |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 133 | width, "rc_dropframe_thresh:", cfg->rc_dropframe_thresh, |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 134 | width, "rc_end_usage:", cfg->rc_end_usage, |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 135 | width, "rc_twopass_stats_in:", cfg->rc_twopass_stats_in.buf, cfg->rc_twopass_stats_in.sz, |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 136 | width, "rc_target_bitrate:", cfg->rc_target_bitrate); |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 137 | av_log(avctx, level, "quantizer settings\n" |
| 138 | " %*s%u\n %*s%u\n", |
| 139 | width, "rc_min_quantizer:", cfg->rc_min_quantizer, |
| 140 | width, "rc_max_quantizer:", cfg->rc_max_quantizer); |
| 141 | av_log(avctx, level, "bitrate tolerance\n" |
| 142 | " %*s%u\n %*s%u\n", |
| 143 | width, "rc_undershoot_pct:", cfg->rc_undershoot_pct, |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 144 | width, "rc_overshoot_pct:", cfg->rc_overshoot_pct); |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 145 | av_log(avctx, level, "decoder buffer model\n" |
| 146 | " %*s%u\n %*s%u\n %*s%u\n", |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 147 | width, "rc_buf_sz:", cfg->rc_buf_sz, |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 148 | width, "rc_buf_initial_sz:", cfg->rc_buf_initial_sz, |
| 149 | width, "rc_buf_optimal_sz:", cfg->rc_buf_optimal_sz); |
| 150 | av_log(avctx, level, "2 pass rate control settings\n" |
| 151 | " %*s%u\n %*s%u\n %*s%u\n", |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 152 | width, "rc_2pass_vbr_bias_pct:", cfg->rc_2pass_vbr_bias_pct, |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 153 | width, "rc_2pass_vbr_minsection_pct:", cfg->rc_2pass_vbr_minsection_pct, |
| 154 | width, "rc_2pass_vbr_maxsection_pct:", cfg->rc_2pass_vbr_maxsection_pct); |
| 155 | av_log(avctx, level, "keyframing settings\n" |
| 156 | " %*s%d\n %*s%u\n %*s%u\n", |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 157 | width, "kf_mode:", cfg->kf_mode, |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 158 | width, "kf_min_dist:", cfg->kf_min_dist, |
| 159 | width, "kf_max_dist:", cfg->kf_max_dist); |
Mark Thompson | 7070955 | 2018-10-22 22:40:07 | [diff] [blame^] | 160 | av_log(avctx, level, "tile settings\n" |
| 161 | " %*s%d\n %*s%d\n", |
| 162 | width, "tile_width_count:", cfg->tile_width_count, |
| 163 | width, "tile_height_count:", cfg->tile_height_count); |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 164 | av_log(avctx, level, "\n"); |
| 165 | } |
| 166 | |
| 167 | static void coded_frame_add(void *list, struct FrameListData *cx_frame) |
| 168 | { |
| 169 | struct FrameListData **p = list; |
| 170 | |
| 171 | while (*p) |
| 172 | p = &(*p)->next; |
| 173 | *p = cx_frame; |
| 174 | cx_frame->next = NULL; |
| 175 | } |
| 176 | |
| 177 | static av_cold void free_coded_frame(struct FrameListData *cx_frame) |
| 178 | { |
| 179 | av_freep(&cx_frame->buf); |
| 180 | av_freep(&cx_frame); |
| 181 | } |
| 182 | |
| 183 | static av_cold void free_frame_list(struct FrameListData *list) |
| 184 | { |
| 185 | struct FrameListData *p = list; |
| 186 | |
| 187 | while (p) { |
| 188 | list = list->next; |
| 189 | free_coded_frame(p); |
| 190 | p = list; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | static av_cold int codecctl_int(AVCodecContext *avctx, |
| 195 | enum aome_enc_control_id id, int val) |
| 196 | { |
| 197 | AOMContext *ctx = avctx->priv_data; |
| 198 | char buf[80]; |
| 199 | int width = -30; |
| 200 | int res; |
| 201 | |
| 202 | snprintf(buf, sizeof(buf), "%s:", ctlidstr[id]); |
| 203 | av_log(avctx, AV_LOG_DEBUG, " %*s%d\n", width, buf, val); |
| 204 | |
| 205 | res = aom_codec_control(&ctx->encoder, id, val); |
| 206 | if (res != AOM_CODEC_OK) { |
| 207 | snprintf(buf, sizeof(buf), "Failed to set %s codec control", |
| 208 | ctlidstr[id]); |
| 209 | log_encoder_error(avctx, buf); |
| 210 | return AVERROR(EINVAL); |
| 211 | } |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | static av_cold int aom_free(AVCodecContext *avctx) |
| 217 | { |
| 218 | AOMContext *ctx = avctx->priv_data; |
| 219 | |
| 220 | aom_codec_destroy(&ctx->encoder); |
| 221 | av_freep(&ctx->twopass_stats.buf); |
| 222 | av_freep(&avctx->stats_out); |
| 223 | free_frame_list(ctx->coded_frame_list); |
James Almer | a754af9 | 2018-07-07 19:33:16 | [diff] [blame] | 224 | av_bsf_free(&ctx->bsf); |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 225 | return 0; |
| 226 | } |
| 227 | |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 228 | static int set_pix_fmt(AVCodecContext *avctx, aom_codec_caps_t codec_caps, |
| 229 | struct aom_codec_enc_cfg *enccfg, aom_codec_flags_t *flags, |
| 230 | aom_img_fmt_t *img_fmt) |
| 231 | { |
| 232 | AOMContext av_unused *ctx = avctx->priv_data; |
| 233 | enccfg->g_bit_depth = enccfg->g_input_bit_depth = 8; |
| 234 | switch (avctx->pix_fmt) { |
| 235 | case AV_PIX_FMT_YUV420P: |
| 236 | enccfg->g_profile = FF_PROFILE_AV1_MAIN; |
| 237 | *img_fmt = AOM_IMG_FMT_I420; |
| 238 | return 0; |
| 239 | case AV_PIX_FMT_YUV422P: |
| 240 | enccfg->g_profile = FF_PROFILE_AV1_PROFESSIONAL; |
| 241 | *img_fmt = AOM_IMG_FMT_I422; |
| 242 | return 0; |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 243 | case AV_PIX_FMT_YUV444P: |
| 244 | enccfg->g_profile = FF_PROFILE_AV1_HIGH; |
| 245 | *img_fmt = AOM_IMG_FMT_I444; |
| 246 | return 0; |
| 247 | case AV_PIX_FMT_YUV420P10: |
| 248 | case AV_PIX_FMT_YUV420P12: |
| 249 | if (codec_caps & AOM_CODEC_CAP_HIGHBITDEPTH) { |
| 250 | enccfg->g_bit_depth = enccfg->g_input_bit_depth = |
| 251 | avctx->pix_fmt == AV_PIX_FMT_YUV420P10 ? 10 : 12; |
| 252 | enccfg->g_profile = |
| 253 | enccfg->g_bit_depth == 10 ? FF_PROFILE_AV1_MAIN : FF_PROFILE_AV1_PROFESSIONAL; |
| 254 | *img_fmt = AOM_IMG_FMT_I42016; |
| 255 | *flags |= AOM_CODEC_USE_HIGHBITDEPTH; |
| 256 | return 0; |
| 257 | } |
| 258 | break; |
| 259 | case AV_PIX_FMT_YUV422P10: |
| 260 | case AV_PIX_FMT_YUV422P12: |
| 261 | if (codec_caps & AOM_CODEC_CAP_HIGHBITDEPTH) { |
| 262 | enccfg->g_bit_depth = enccfg->g_input_bit_depth = |
| 263 | avctx->pix_fmt == AV_PIX_FMT_YUV422P10 ? 10 : 12; |
| 264 | enccfg->g_profile = FF_PROFILE_AV1_PROFESSIONAL; |
| 265 | *img_fmt = AOM_IMG_FMT_I42216; |
| 266 | *flags |= AOM_CODEC_USE_HIGHBITDEPTH; |
| 267 | return 0; |
| 268 | } |
| 269 | break; |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 270 | case AV_PIX_FMT_YUV444P10: |
| 271 | case AV_PIX_FMT_YUV444P12: |
| 272 | if (codec_caps & AOM_CODEC_CAP_HIGHBITDEPTH) { |
| 273 | enccfg->g_bit_depth = enccfg->g_input_bit_depth = |
James Almer | 877aff2 | 2018-04-02 19:44:05 | [diff] [blame] | 274 | avctx->pix_fmt == AV_PIX_FMT_YUV444P10 ? 10 : 12; |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 275 | enccfg->g_profile = |
| 276 | enccfg->g_bit_depth == 10 ? FF_PROFILE_AV1_HIGH : FF_PROFILE_AV1_PROFESSIONAL; |
| 277 | *img_fmt = AOM_IMG_FMT_I44416; |
| 278 | *flags |= AOM_CODEC_USE_HIGHBITDEPTH; |
| 279 | return 0; |
| 280 | } |
| 281 | break; |
| 282 | default: |
| 283 | break; |
| 284 | } |
| 285 | av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format.\n"); |
| 286 | return AVERROR_INVALIDDATA; |
| 287 | } |
| 288 | |
| 289 | static void set_color_range(AVCodecContext *avctx) |
| 290 | { |
| 291 | enum aom_color_range aom_cr; |
| 292 | switch (avctx->color_range) { |
| 293 | case AVCOL_RANGE_UNSPECIFIED: |
| 294 | case AVCOL_RANGE_MPEG: aom_cr = AOM_CR_STUDIO_RANGE; break; |
| 295 | case AVCOL_RANGE_JPEG: aom_cr = AOM_CR_FULL_RANGE; break; |
| 296 | default: |
| 297 | av_log(avctx, AV_LOG_WARNING, "Unsupported color range (%d)\n", |
| 298 | avctx->color_range); |
| 299 | return; |
| 300 | } |
| 301 | |
| 302 | codecctl_int(avctx, AV1E_SET_COLOR_RANGE, aom_cr); |
| 303 | } |
| 304 | |
Mark Thompson | 7070955 | 2018-10-22 22:40:07 | [diff] [blame^] | 305 | static int count_uniform_tiling(int dim, int sb_size, int tiles_log2) |
| 306 | { |
| 307 | int sb_dim = (dim + sb_size - 1) / sb_size; |
| 308 | int tile_dim = (sb_dim + (1 << tiles_log2) - 1) >> tiles_log2; |
| 309 | av_assert0(tile_dim > 0); |
| 310 | return (sb_dim + tile_dim - 1) / tile_dim; |
| 311 | } |
| 312 | |
| 313 | static int choose_tiling(AVCodecContext *avctx, |
| 314 | struct aom_codec_enc_cfg *enccfg) |
| 315 | { |
| 316 | AOMContext *ctx = avctx->priv_data; |
| 317 | int sb_128x128_possible, sb_size, sb_width, sb_height; |
| 318 | int uniform_rows, uniform_cols; |
| 319 | int uniform_64x64_possible, uniform_128x128_possible; |
| 320 | int tile_size, rounding, i; |
| 321 | |
| 322 | if (ctx->tile_cols_log2 >= 0) |
| 323 | ctx->tile_cols = 1 << ctx->tile_cols_log2; |
| 324 | if (ctx->tile_rows_log2 >= 0) |
| 325 | ctx->tile_rows = 1 << ctx->tile_rows_log2; |
| 326 | |
| 327 | if (ctx->tile_cols == 0) { |
| 328 | ctx->tile_cols = (avctx->width + AV1_MAX_TILE_WIDTH - 1) / |
| 329 | AV1_MAX_TILE_WIDTH; |
| 330 | if (ctx->tile_cols > 1) { |
| 331 | av_log(avctx, AV_LOG_DEBUG, "Automatically using %d tile " |
| 332 | "columns to fill width.\n", ctx->tile_cols); |
| 333 | } |
| 334 | } |
| 335 | av_assert0(ctx->tile_cols > 0); |
| 336 | if (ctx->tile_rows == 0) { |
| 337 | int max_tile_width = |
| 338 | FFALIGN((FFALIGN(avctx->width, 128) + |
| 339 | ctx->tile_cols - 1) / ctx->tile_cols, 128); |
| 340 | ctx->tile_rows = |
| 341 | (max_tile_width * FFALIGN(avctx->height, 128) + |
| 342 | AV1_MAX_TILE_AREA - 1) / AV1_MAX_TILE_AREA; |
| 343 | if (ctx->tile_rows > 1) { |
| 344 | av_log(avctx, AV_LOG_DEBUG, "Automatically using %d tile " |
| 345 | "rows to fill area.\n", ctx->tile_rows); |
| 346 | } |
| 347 | } |
| 348 | av_assert0(ctx->tile_rows > 0); |
| 349 | |
| 350 | if ((avctx->width + 63) / 64 < ctx->tile_cols || |
| 351 | (avctx->height + 63) / 64 < ctx->tile_rows) { |
| 352 | av_log(avctx, AV_LOG_ERROR, "Invalid tile sizing: frame not " |
| 353 | "large enough to fit specified tile arrangement.\n"); |
| 354 | return AVERROR(EINVAL); |
| 355 | } |
| 356 | if (ctx->tile_cols > AV1_MAX_TILE_COLS || |
| 357 | ctx->tile_rows > AV1_MAX_TILE_ROWS) { |
| 358 | av_log(avctx, AV_LOG_ERROR, "Invalid tile sizing: AV1 does " |
| 359 | "not allow more than %dx%d tiles.\n", |
| 360 | AV1_MAX_TILE_COLS, AV1_MAX_TILE_ROWS); |
| 361 | return AVERROR(EINVAL); |
| 362 | } |
| 363 | if (avctx->width / ctx->tile_cols > AV1_MAX_TILE_WIDTH) { |
| 364 | av_log(avctx, AV_LOG_ERROR, "Invalid tile sizing: AV1 does " |
| 365 | "not allow tiles of width greater than %d.\n", |
| 366 | AV1_MAX_TILE_WIDTH); |
| 367 | return AVERROR(EINVAL); |
| 368 | } |
| 369 | |
| 370 | ctx->superblock_size = AOM_SUPERBLOCK_SIZE_DYNAMIC; |
| 371 | |
| 372 | if (ctx->tile_cols == 1 && ctx->tile_rows == 1) { |
| 373 | av_log(avctx, AV_LOG_DEBUG, "Using a single tile.\n"); |
| 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | sb_128x128_possible = |
| 378 | (avctx->width + 127) / 128 >= ctx->tile_cols && |
| 379 | (avctx->height + 127) / 128 >= ctx->tile_rows; |
| 380 | |
| 381 | ctx->tile_cols_log2 = ctx->tile_cols == 1 ? 0 : |
| 382 | av_log2(ctx->tile_cols - 1) + 1; |
| 383 | ctx->tile_rows_log2 = ctx->tile_rows == 1 ? 0 : |
| 384 | av_log2(ctx->tile_rows - 1) + 1; |
| 385 | |
| 386 | uniform_cols = count_uniform_tiling(avctx->width, |
| 387 | 64, ctx->tile_cols_log2); |
| 388 | uniform_rows = count_uniform_tiling(avctx->height, |
| 389 | 64, ctx->tile_rows_log2); |
| 390 | av_log(avctx, AV_LOG_DEBUG, "Uniform with 64x64 superblocks " |
| 391 | "-> %dx%d tiles.\n", uniform_cols, uniform_rows); |
| 392 | uniform_64x64_possible = uniform_cols == ctx->tile_cols && |
| 393 | uniform_rows == ctx->tile_rows; |
| 394 | |
| 395 | if (sb_128x128_possible) { |
| 396 | uniform_cols = count_uniform_tiling(avctx->width, |
| 397 | 128, ctx->tile_cols_log2); |
| 398 | uniform_rows = count_uniform_tiling(avctx->height, |
| 399 | 128, ctx->tile_rows_log2); |
| 400 | av_log(avctx, AV_LOG_DEBUG, "Uniform with 128x128 superblocks " |
| 401 | "-> %dx%d tiles.\n", uniform_cols, uniform_rows); |
| 402 | uniform_128x128_possible = uniform_cols == ctx->tile_cols && |
| 403 | uniform_rows == ctx->tile_rows; |
| 404 | } else { |
| 405 | av_log(avctx, AV_LOG_DEBUG, "128x128 superblocks not possible.\n"); |
| 406 | uniform_128x128_possible = 0; |
| 407 | } |
| 408 | |
| 409 | ctx->uniform_tiles = 1; |
| 410 | if (uniform_64x64_possible && uniform_128x128_possible) { |
| 411 | av_log(avctx, AV_LOG_DEBUG, "Using uniform tiling with dynamic " |
| 412 | "superblocks (tile_cols_log2 = %d, tile_rows_log2 = %d).\n", |
| 413 | ctx->tile_cols_log2, ctx->tile_rows_log2); |
| 414 | return 0; |
| 415 | } |
| 416 | if (uniform_64x64_possible && !sb_128x128_possible) { |
| 417 | av_log(avctx, AV_LOG_DEBUG, "Using uniform tiling with 64x64 " |
| 418 | "superblocks (tile_cols_log2 = %d, tile_rows_log2 = %d).\n", |
| 419 | ctx->tile_cols_log2, ctx->tile_rows_log2); |
| 420 | ctx->superblock_size = AOM_SUPERBLOCK_SIZE_64X64; |
| 421 | return 0; |
| 422 | } |
| 423 | if (uniform_128x128_possible) { |
| 424 | av_log(avctx, AV_LOG_DEBUG, "Using uniform tiling with 128x128 " |
| 425 | "superblocks (tile_cols_log2 = %d, tile_rows_log2 = %d).\n", |
| 426 | ctx->tile_cols_log2, ctx->tile_rows_log2); |
| 427 | ctx->superblock_size = AOM_SUPERBLOCK_SIZE_128X128; |
| 428 | return 0; |
| 429 | } |
| 430 | ctx->uniform_tiles = 0; |
| 431 | |
| 432 | if (sb_128x128_possible) { |
| 433 | sb_size = 128; |
| 434 | ctx->superblock_size = AOM_SUPERBLOCK_SIZE_128X128; |
| 435 | } else { |
| 436 | sb_size = 64; |
| 437 | ctx->superblock_size = AOM_SUPERBLOCK_SIZE_64X64; |
| 438 | } |
| 439 | av_log(avctx, AV_LOG_DEBUG, "Using fixed tiling with %dx%d " |
| 440 | "superblocks (tile_cols = %d, tile_rows = %d).\n", |
| 441 | sb_size, sb_size, ctx->tile_cols, ctx->tile_rows); |
| 442 | |
| 443 | enccfg->tile_width_count = ctx->tile_cols; |
| 444 | enccfg->tile_height_count = ctx->tile_rows; |
| 445 | |
| 446 | sb_width = (avctx->width + sb_size - 1) / sb_size; |
| 447 | sb_height = (avctx->height + sb_size - 1) / sb_size; |
| 448 | |
| 449 | tile_size = sb_width / ctx->tile_cols; |
| 450 | rounding = sb_width % ctx->tile_cols; |
| 451 | for (i = 0; i < ctx->tile_cols; i++) { |
| 452 | enccfg->tile_widths[i] = tile_size + |
| 453 | (i < rounding / 2 || |
| 454 | i > ctx->tile_cols - 1 - (rounding + 1) / 2); |
| 455 | } |
| 456 | |
| 457 | tile_size = sb_height / ctx->tile_rows; |
| 458 | rounding = sb_height % ctx->tile_rows; |
| 459 | for (i = 0; i < ctx->tile_rows; i++) { |
| 460 | enccfg->tile_heights[i] = tile_size + |
| 461 | (i < rounding / 2 || |
| 462 | i > ctx->tile_rows - 1 - (rounding + 1) / 2); |
| 463 | } |
| 464 | |
| 465 | return 0; |
| 466 | } |
| 467 | |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 468 | static av_cold int aom_init(AVCodecContext *avctx, |
| 469 | const struct aom_codec_iface *iface) |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 470 | { |
| 471 | AOMContext *ctx = avctx->priv_data; |
| 472 | struct aom_codec_enc_cfg enccfg = { 0 }; |
Sam John | e265832 | 2018-09-28 22:09:05 | [diff] [blame] | 473 | #ifdef AOM_FRAME_IS_INTRAONLY |
| 474 | aom_codec_flags_t flags = |
| 475 | (avctx->flags & AV_CODEC_FLAG_PSNR) ? AOM_CODEC_USE_PSNR : 0; |
| 476 | #else |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 477 | aom_codec_flags_t flags = 0; |
Sam John | e265832 | 2018-09-28 22:09:05 | [diff] [blame] | 478 | #endif |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 479 | AVCPBProperties *cpb_props; |
| 480 | int res; |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 481 | aom_img_fmt_t img_fmt; |
| 482 | aom_codec_caps_t codec_caps = aom_codec_get_caps(iface); |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 483 | |
| 484 | av_log(avctx, AV_LOG_INFO, "%s\n", aom_codec_version_str()); |
| 485 | av_log(avctx, AV_LOG_VERBOSE, "%s\n", aom_codec_build_config()); |
| 486 | |
| 487 | if ((res = aom_codec_enc_config_default(iface, &enccfg, 0)) != AOM_CODEC_OK) { |
| 488 | av_log(avctx, AV_LOG_ERROR, "Failed to get config: %s\n", |
| 489 | aom_codec_err_to_string(res)); |
| 490 | return AVERROR(EINVAL); |
| 491 | } |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 492 | |
| 493 | if (set_pix_fmt(avctx, codec_caps, &enccfg, &flags, &img_fmt)) |
| 494 | return AVERROR(EINVAL); |
| 495 | |
| 496 | if(!avctx->bit_rate) |
| 497 | if(avctx->rc_max_rate || avctx->rc_buffer_size || avctx->rc_initial_buffer_occupancy) { |
| 498 | av_log( avctx, AV_LOG_ERROR, "Rate control parameters set without a bitrate\n"); |
| 499 | return AVERROR(EINVAL); |
| 500 | } |
| 501 | |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 502 | dump_enc_cfg(avctx, &enccfg); |
| 503 | |
| 504 | enccfg.g_w = avctx->width; |
| 505 | enccfg.g_h = avctx->height; |
| 506 | enccfg.g_timebase.num = avctx->time_base.num; |
| 507 | enccfg.g_timebase.den = avctx->time_base.den; |
James Almer | 309c3a0 | 2018-09-13 16:26:00 | [diff] [blame] | 508 | enccfg.g_threads = avctx->thread_count ? avctx->thread_count : av_cpu_count(); |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 509 | |
| 510 | if (ctx->lag_in_frames >= 0) |
| 511 | enccfg.g_lag_in_frames = ctx->lag_in_frames; |
| 512 | |
| 513 | if (avctx->flags & AV_CODEC_FLAG_PASS1) |
| 514 | enccfg.g_pass = AOM_RC_FIRST_PASS; |
| 515 | else if (avctx->flags & AV_CODEC_FLAG_PASS2) |
| 516 | enccfg.g_pass = AOM_RC_LAST_PASS; |
| 517 | else |
| 518 | enccfg.g_pass = AOM_RC_ONE_PASS; |
| 519 | |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 520 | if (avctx->rc_min_rate == avctx->rc_max_rate && |
| 521 | avctx->rc_min_rate == avctx->bit_rate && avctx->bit_rate) { |
| 522 | enccfg.rc_end_usage = AOM_CBR; |
| 523 | } else if (ctx->crf >= 0) { |
| 524 | enccfg.rc_end_usage = AOM_CQ; |
| 525 | if (!avctx->bit_rate) |
| 526 | enccfg.rc_end_usage = AOM_Q; |
| 527 | } |
| 528 | |
| 529 | if (avctx->bit_rate) { |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 530 | enccfg.rc_target_bitrate = av_rescale_rnd(avctx->bit_rate, 1, 1000, |
| 531 | AV_ROUND_NEAR_INF); |
James Almer | 25abaf3 | 2018-03-29 19:18:27 | [diff] [blame] | 532 | } else if (enccfg.rc_end_usage != AOM_Q) { |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 533 | if (enccfg.rc_end_usage == AOM_CQ) { |
| 534 | enccfg.rc_target_bitrate = 1000000; |
| 535 | } else { |
| 536 | avctx->bit_rate = enccfg.rc_target_bitrate * 1000; |
| 537 | av_log(avctx, AV_LOG_WARNING, |
| 538 | "Neither bitrate nor constrained quality specified, using default bitrate of %dkbit/sec\n", |
| 539 | enccfg.rc_target_bitrate); |
| 540 | } |
| 541 | } |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 542 | |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 543 | if (avctx->qmin >= 0) |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 544 | enccfg.rc_min_quantizer = avctx->qmin; |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 545 | if (avctx->qmax >= 0) |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 546 | enccfg.rc_max_quantizer = avctx->qmax; |
| 547 | |
James Almer | 25abaf3 | 2018-03-29 19:18:27 | [diff] [blame] | 548 | if (enccfg.rc_end_usage == AOM_CQ || enccfg.rc_end_usage == AOM_Q) { |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 549 | if (ctx->crf < enccfg.rc_min_quantizer || ctx->crf > enccfg.rc_max_quantizer) { |
| 550 | av_log(avctx, AV_LOG_ERROR, |
| 551 | "CQ level %d must be between minimum and maximum quantizer value (%d-%d)\n", |
| 552 | ctx->crf, enccfg.rc_min_quantizer, enccfg.rc_max_quantizer); |
| 553 | return AVERROR(EINVAL); |
| 554 | } |
| 555 | } |
| 556 | |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 557 | enccfg.rc_dropframe_thresh = ctx->drop_threshold; |
| 558 | |
| 559 | // 0-100 (0 => CBR, 100 => VBR) |
| 560 | enccfg.rc_2pass_vbr_bias_pct = round(avctx->qcompress * 100); |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 561 | if (avctx->bit_rate) |
| 562 | enccfg.rc_2pass_vbr_minsection_pct = |
| 563 | avctx->rc_min_rate * 100LL / avctx->bit_rate; |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 564 | if (avctx->rc_max_rate) |
| 565 | enccfg.rc_2pass_vbr_maxsection_pct = |
| 566 | avctx->rc_max_rate * 100LL / avctx->bit_rate; |
| 567 | |
| 568 | if (avctx->rc_buffer_size) |
| 569 | enccfg.rc_buf_sz = |
| 570 | avctx->rc_buffer_size * 1000LL / avctx->bit_rate; |
| 571 | if (avctx->rc_initial_buffer_occupancy) |
| 572 | enccfg.rc_buf_initial_sz = |
| 573 | avctx->rc_initial_buffer_occupancy * 1000LL / avctx->bit_rate; |
| 574 | enccfg.rc_buf_optimal_sz = enccfg.rc_buf_sz * 5 / 6; |
| 575 | |
| 576 | // _enc_init() will balk if kf_min_dist differs from max w/AOM_KF_AUTO |
| 577 | if (avctx->keyint_min >= 0 && avctx->keyint_min == avctx->gop_size) |
| 578 | enccfg.kf_min_dist = avctx->keyint_min; |
| 579 | if (avctx->gop_size >= 0) |
| 580 | enccfg.kf_max_dist = avctx->gop_size; |
| 581 | |
| 582 | if (enccfg.g_pass == AOM_RC_FIRST_PASS) |
| 583 | enccfg.g_lag_in_frames = 0; |
| 584 | else if (enccfg.g_pass == AOM_RC_LAST_PASS) { |
| 585 | int decode_size, ret; |
| 586 | |
| 587 | if (!avctx->stats_in) { |
| 588 | av_log(avctx, AV_LOG_ERROR, "No stats file for second pass\n"); |
| 589 | return AVERROR_INVALIDDATA; |
| 590 | } |
| 591 | |
| 592 | ctx->twopass_stats.sz = strlen(avctx->stats_in) * 3 / 4; |
| 593 | ret = av_reallocp(&ctx->twopass_stats.buf, ctx->twopass_stats.sz); |
| 594 | if (ret < 0) { |
| 595 | av_log(avctx, AV_LOG_ERROR, |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 596 | "Stat buffer alloc (%"SIZE_SPECIFIER" bytes) failed\n", |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 597 | ctx->twopass_stats.sz); |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 598 | ctx->twopass_stats.sz = 0; |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 599 | return ret; |
| 600 | } |
| 601 | decode_size = av_base64_decode(ctx->twopass_stats.buf, avctx->stats_in, |
| 602 | ctx->twopass_stats.sz); |
| 603 | if (decode_size < 0) { |
| 604 | av_log(avctx, AV_LOG_ERROR, "Stat buffer decode failed\n"); |
| 605 | return AVERROR_INVALIDDATA; |
| 606 | } |
| 607 | |
| 608 | ctx->twopass_stats.sz = decode_size; |
| 609 | enccfg.rc_twopass_stats_in = ctx->twopass_stats; |
| 610 | } |
| 611 | |
| 612 | /* 0-3: For non-zero values the encoder increasingly optimizes for reduced |
| 613 | * complexity playback on low powered devices at the expense of encode |
| 614 | * quality. */ |
| 615 | if (avctx->profile != FF_PROFILE_UNKNOWN) |
| 616 | enccfg.g_profile = avctx->profile; |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 617 | |
| 618 | enccfg.g_error_resilient = ctx->error_resilient; |
| 619 | |
Mark Thompson | 7070955 | 2018-10-22 22:40:07 | [diff] [blame^] | 620 | res = choose_tiling(avctx, &enccfg); |
| 621 | if (res < 0) |
| 622 | return res; |
| 623 | |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 624 | dump_enc_cfg(avctx, &enccfg); |
| 625 | /* Construct Encoder Context */ |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 626 | res = aom_codec_enc_init(&ctx->encoder, iface, &enccfg, flags); |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 627 | if (res != AOM_CODEC_OK) { |
| 628 | log_encoder_error(avctx, "Failed to initialize encoder"); |
| 629 | return AVERROR(EINVAL); |
| 630 | } |
| 631 | |
| 632 | // codec control failures are currently treated only as warnings |
| 633 | av_log(avctx, AV_LOG_DEBUG, "aom_codec_control\n"); |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 634 | codecctl_int(avctx, AOME_SET_CPUUSED, ctx->cpu_used); |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 635 | if (ctx->auto_alt_ref >= 0) |
| 636 | codecctl_int(avctx, AOME_SET_ENABLEAUTOALTREF, ctx->auto_alt_ref); |
| 637 | |
| 638 | codecctl_int(avctx, AOME_SET_STATIC_THRESHOLD, ctx->static_thresh); |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 639 | if (ctx->crf >= 0) |
| 640 | codecctl_int(avctx, AOME_SET_CQ_LEVEL, ctx->crf); |
| 641 | |
James Almer | e5819fa | 2018-03-29 04:03:24 | [diff] [blame] | 642 | codecctl_int(avctx, AV1E_SET_COLOR_PRIMARIES, avctx->color_primaries); |
| 643 | codecctl_int(avctx, AV1E_SET_MATRIX_COEFFICIENTS, avctx->colorspace); |
| 644 | codecctl_int(avctx, AV1E_SET_TRANSFER_CHARACTERISTICS, avctx->color_trc); |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 645 | set_color_range(avctx); |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 646 | |
Mark Thompson | 7070955 | 2018-10-22 22:40:07 | [diff] [blame^] | 647 | codecctl_int(avctx, AV1E_SET_SUPERBLOCK_SIZE, ctx->superblock_size); |
| 648 | if (ctx->uniform_tiles) { |
| 649 | codecctl_int(avctx, AV1E_SET_TILE_COLUMNS, ctx->tile_cols_log2); |
| 650 | codecctl_int(avctx, AV1E_SET_TILE_ROWS, ctx->tile_rows_log2); |
| 651 | } |
| 652 | |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 653 | // provide dummy value to initialize wrapper, values will be updated each _encode() |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 654 | aom_img_wrap(&ctx->rawimg, img_fmt, avctx->width, avctx->height, 1, |
| 655 | (unsigned char*)1); |
| 656 | |
| 657 | if (codec_caps & AOM_CODEC_CAP_HIGHBITDEPTH) |
| 658 | ctx->rawimg.bit_depth = enccfg.g_bit_depth; |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 659 | |
| 660 | cpb_props = ff_add_cpb_side_data(avctx); |
| 661 | if (!cpb_props) |
| 662 | return AVERROR(ENOMEM); |
| 663 | |
James Almer | a754af9 | 2018-07-07 19:33:16 | [diff] [blame] | 664 | if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) { |
| 665 | const AVBitStreamFilter *filter = av_bsf_get_by_name("extract_extradata"); |
| 666 | int ret; |
| 667 | |
| 668 | if (!filter) { |
| 669 | av_log(avctx, AV_LOG_ERROR, "extract_extradata bitstream filter " |
| 670 | "not found. This is a bug, please report it.\n"); |
| 671 | return AVERROR_BUG; |
| 672 | } |
| 673 | ret = av_bsf_alloc(filter, &ctx->bsf); |
| 674 | if (ret < 0) |
| 675 | return ret; |
| 676 | |
| 677 | ret = avcodec_parameters_from_context(ctx->bsf->par_in, avctx); |
| 678 | if (ret < 0) |
| 679 | return ret; |
| 680 | |
| 681 | ret = av_bsf_init(ctx->bsf); |
| 682 | if (ret < 0) |
| 683 | return ret; |
| 684 | } |
| 685 | |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 686 | if (enccfg.rc_end_usage == AOM_CBR || |
| 687 | enccfg.g_pass != AOM_RC_ONE_PASS) { |
| 688 | cpb_props->max_bitrate = avctx->rc_max_rate; |
| 689 | cpb_props->min_bitrate = avctx->rc_min_rate; |
| 690 | cpb_props->avg_bitrate = avctx->bit_rate; |
| 691 | } |
| 692 | cpb_props->buffer_size = avctx->rc_buffer_size; |
| 693 | |
| 694 | return 0; |
| 695 | } |
| 696 | |
Sam John | e265832 | 2018-09-28 22:09:05 | [diff] [blame] | 697 | static inline void cx_pktcpy(AOMContext *ctx, |
| 698 | struct FrameListData *dst, |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 699 | const struct aom_codec_cx_pkt *src) |
| 700 | { |
| 701 | dst->pts = src->data.frame.pts; |
| 702 | dst->duration = src->data.frame.duration; |
| 703 | dst->flags = src->data.frame.flags; |
| 704 | dst->sz = src->data.frame.sz; |
| 705 | dst->buf = src->data.frame.buf; |
Sam John | e265832 | 2018-09-28 22:09:05 | [diff] [blame] | 706 | #ifdef AOM_FRAME_IS_INTRAONLY |
| 707 | dst->have_sse = 0; |
| 708 | dst->frame_number = ++ctx->frame_number; |
| 709 | dst->have_sse = ctx->have_sse; |
| 710 | if (ctx->have_sse) { |
| 711 | /* associate last-seen SSE to the frame. */ |
| 712 | /* Transfers ownership from ctx to dst. */ |
| 713 | memcpy(dst->sse, ctx->sse, sizeof(dst->sse)); |
| 714 | ctx->have_sse = 0; |
| 715 | } |
| 716 | #endif |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | /** |
| 720 | * Store coded frame information in format suitable for return from encode2(). |
| 721 | * |
| 722 | * Write information from @a cx_frame to @a pkt |
| 723 | * @return packet data size on success |
| 724 | * @return a negative AVERROR on error |
| 725 | */ |
| 726 | static int storeframe(AVCodecContext *avctx, struct FrameListData *cx_frame, |
| 727 | AVPacket *pkt) |
| 728 | { |
James Almer | a754af9 | 2018-07-07 19:33:16 | [diff] [blame] | 729 | AOMContext *ctx = avctx->priv_data; |
Sam John | e265832 | 2018-09-28 22:09:05 | [diff] [blame] | 730 | int pict_type; |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 731 | int ret = ff_alloc_packet2(avctx, pkt, cx_frame->sz, 0); |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 732 | if (ret < 0) { |
| 733 | av_log(avctx, AV_LOG_ERROR, |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 734 | "Error getting output packet of size %"SIZE_SPECIFIER".\n", cx_frame->sz); |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 735 | return ret; |
| 736 | } |
| 737 | memcpy(pkt->data, cx_frame->buf, pkt->size); |
| 738 | pkt->pts = pkt->dts = cx_frame->pts; |
| 739 | |
Sam John | e265832 | 2018-09-28 22:09:05 | [diff] [blame] | 740 | if (!!(cx_frame->flags & AOM_FRAME_IS_KEY)) { |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 741 | pkt->flags |= AV_PKT_FLAG_KEY; |
Sam John | e265832 | 2018-09-28 22:09:05 | [diff] [blame] | 742 | #ifdef AOM_FRAME_IS_INTRAONLY |
| 743 | pict_type = AV_PICTURE_TYPE_I; |
| 744 | } else if (cx_frame->flags & AOM_FRAME_IS_INTRAONLY) { |
| 745 | pict_type = AV_PICTURE_TYPE_I; |
| 746 | } else { |
| 747 | pict_type = AV_PICTURE_TYPE_P; |
| 748 | } |
| 749 | |
| 750 | ff_side_data_set_encoder_stats(pkt, 0, cx_frame->sse + 1, |
| 751 | cx_frame->have_sse ? 3 : 0, pict_type); |
| 752 | |
| 753 | if (cx_frame->have_sse) { |
| 754 | int i; |
| 755 | for (i = 0; i < 3; ++i) { |
| 756 | avctx->error[i] += cx_frame->sse[i + 1]; |
| 757 | } |
| 758 | cx_frame->have_sse = 0; |
| 759 | #endif |
| 760 | } |
James Almer | a754af9 | 2018-07-07 19:33:16 | [diff] [blame] | 761 | |
| 762 | if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) { |
| 763 | ret = av_bsf_send_packet(ctx->bsf, pkt); |
| 764 | if (ret < 0) { |
| 765 | av_log(avctx, AV_LOG_ERROR, "extract_extradata filter " |
| 766 | "failed to send input packet\n"); |
| 767 | return ret; |
| 768 | } |
| 769 | ret = av_bsf_receive_packet(ctx->bsf, pkt); |
| 770 | |
| 771 | if (ret < 0) { |
| 772 | av_log(avctx, AV_LOG_ERROR, "extract_extradata filter " |
| 773 | "failed to receive output packet\n"); |
| 774 | return ret; |
| 775 | } |
| 776 | } |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 777 | return pkt->size; |
| 778 | } |
| 779 | |
| 780 | /** |
| 781 | * Queue multiple output frames from the encoder, returning the front-most. |
| 782 | * In cases where aom_codec_get_cx_data() returns more than 1 frame append |
| 783 | * the frame queue. Return the head frame if available. |
| 784 | * @return Stored frame size |
| 785 | * @return AVERROR(EINVAL) on output size error |
| 786 | * @return AVERROR(ENOMEM) on coded frame queue data allocation error |
| 787 | */ |
| 788 | static int queue_frames(AVCodecContext *avctx, AVPacket *pkt_out) |
| 789 | { |
| 790 | AOMContext *ctx = avctx->priv_data; |
| 791 | const struct aom_codec_cx_pkt *pkt; |
| 792 | const void *iter = NULL; |
| 793 | int size = 0; |
| 794 | |
| 795 | if (ctx->coded_frame_list) { |
| 796 | struct FrameListData *cx_frame = ctx->coded_frame_list; |
| 797 | /* return the leading frame if we've already begun queueing */ |
| 798 | size = storeframe(avctx, cx_frame, pkt_out); |
| 799 | if (size < 0) |
| 800 | return size; |
| 801 | ctx->coded_frame_list = cx_frame->next; |
| 802 | free_coded_frame(cx_frame); |
| 803 | } |
| 804 | |
| 805 | /* consume all available output from the encoder before returning. buffers |
| 806 | * are only good through the next aom_codec call */ |
| 807 | while ((pkt = aom_codec_get_cx_data(&ctx->encoder, &iter))) { |
| 808 | switch (pkt->kind) { |
| 809 | case AOM_CODEC_CX_FRAME_PKT: |
| 810 | if (!size) { |
| 811 | struct FrameListData cx_frame; |
| 812 | |
| 813 | /* avoid storing the frame when the list is empty and we haven't yet |
| 814 | * provided a frame for output */ |
James Almer | c42c99c | 2018-03-30 01:35:33 | [diff] [blame] | 815 | av_assert0(!ctx->coded_frame_list); |
Sam John | e265832 | 2018-09-28 22:09:05 | [diff] [blame] | 816 | cx_pktcpy(ctx, &cx_frame, pkt); |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 817 | size = storeframe(avctx, &cx_frame, pkt_out); |
| 818 | if (size < 0) |
| 819 | return size; |
| 820 | } else { |
| 821 | struct FrameListData *cx_frame = |
| 822 | av_malloc(sizeof(struct FrameListData)); |
| 823 | |
| 824 | if (!cx_frame) { |
| 825 | av_log(avctx, AV_LOG_ERROR, |
| 826 | "Frame queue element alloc failed\n"); |
| 827 | return AVERROR(ENOMEM); |
| 828 | } |
Sam John | e265832 | 2018-09-28 22:09:05 | [diff] [blame] | 829 | cx_pktcpy(ctx, cx_frame, pkt); |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 830 | cx_frame->buf = av_malloc(cx_frame->sz); |
| 831 | |
| 832 | if (!cx_frame->buf) { |
| 833 | av_log(avctx, AV_LOG_ERROR, |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 834 | "Data buffer alloc (%"SIZE_SPECIFIER" bytes) failed\n", |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 835 | cx_frame->sz); |
| 836 | av_freep(&cx_frame); |
| 837 | return AVERROR(ENOMEM); |
| 838 | } |
| 839 | memcpy(cx_frame->buf, pkt->data.frame.buf, pkt->data.frame.sz); |
| 840 | coded_frame_add(&ctx->coded_frame_list, cx_frame); |
| 841 | } |
| 842 | break; |
| 843 | case AOM_CODEC_STATS_PKT: |
| 844 | { |
| 845 | struct aom_fixed_buf *stats = &ctx->twopass_stats; |
| 846 | int err; |
| 847 | if ((err = av_reallocp(&stats->buf, |
| 848 | stats->sz + |
| 849 | pkt->data.twopass_stats.sz)) < 0) { |
| 850 | stats->sz = 0; |
| 851 | av_log(avctx, AV_LOG_ERROR, "Stat buffer realloc failed\n"); |
| 852 | return err; |
| 853 | } |
| 854 | memcpy((uint8_t *)stats->buf + stats->sz, |
| 855 | pkt->data.twopass_stats.buf, pkt->data.twopass_stats.sz); |
| 856 | stats->sz += pkt->data.twopass_stats.sz; |
| 857 | break; |
| 858 | } |
Sam John | e265832 | 2018-09-28 22:09:05 | [diff] [blame] | 859 | #ifdef AOM_FRAME_IS_INTRAONLY |
| 860 | case AOM_CODEC_PSNR_PKT: |
| 861 | { |
| 862 | av_assert0(!ctx->have_sse); |
| 863 | ctx->sse[0] = pkt->data.psnr.sse[0]; |
| 864 | ctx->sse[1] = pkt->data.psnr.sse[1]; |
| 865 | ctx->sse[2] = pkt->data.psnr.sse[2]; |
| 866 | ctx->sse[3] = pkt->data.psnr.sse[3]; |
| 867 | ctx->have_sse = 1; |
| 868 | break; |
| 869 | } |
| 870 | #endif |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 871 | case AOM_CODEC_CUSTOM_PKT: |
| 872 | // ignore unsupported/unrecognized packet types |
| 873 | break; |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | return size; |
| 878 | } |
| 879 | |
| 880 | static int aom_encode(AVCodecContext *avctx, AVPacket *pkt, |
| 881 | const AVFrame *frame, int *got_packet) |
| 882 | { |
| 883 | AOMContext *ctx = avctx->priv_data; |
| 884 | struct aom_image *rawimg = NULL; |
| 885 | int64_t timestamp = 0; |
| 886 | int res, coded_size; |
| 887 | aom_enc_frame_flags_t flags = 0; |
| 888 | |
| 889 | if (frame) { |
| 890 | rawimg = &ctx->rawimg; |
| 891 | rawimg->planes[AOM_PLANE_Y] = frame->data[0]; |
| 892 | rawimg->planes[AOM_PLANE_U] = frame->data[1]; |
| 893 | rawimg->planes[AOM_PLANE_V] = frame->data[2]; |
| 894 | rawimg->stride[AOM_PLANE_Y] = frame->linesize[0]; |
| 895 | rawimg->stride[AOM_PLANE_U] = frame->linesize[1]; |
| 896 | rawimg->stride[AOM_PLANE_V] = frame->linesize[2]; |
| 897 | timestamp = frame->pts; |
| 898 | switch (frame->color_range) { |
| 899 | case AVCOL_RANGE_MPEG: |
| 900 | rawimg->range = AOM_CR_STUDIO_RANGE; |
| 901 | break; |
| 902 | case AVCOL_RANGE_JPEG: |
| 903 | rawimg->range = AOM_CR_FULL_RANGE; |
| 904 | break; |
| 905 | } |
| 906 | |
| 907 | if (frame->pict_type == AV_PICTURE_TYPE_I) |
| 908 | flags |= AOM_EFLAG_FORCE_KF; |
| 909 | } |
| 910 | |
| 911 | res = aom_codec_encode(&ctx->encoder, rawimg, timestamp, |
| 912 | avctx->ticks_per_frame, flags); |
| 913 | if (res != AOM_CODEC_OK) { |
| 914 | log_encoder_error(avctx, "Error encoding frame"); |
| 915 | return AVERROR_INVALIDDATA; |
| 916 | } |
| 917 | coded_size = queue_frames(avctx, pkt); |
| 918 | |
| 919 | if (!frame && avctx->flags & AV_CODEC_FLAG_PASS1) { |
| 920 | size_t b64_size = AV_BASE64_SIZE(ctx->twopass_stats.sz); |
| 921 | |
| 922 | avctx->stats_out = av_malloc(b64_size); |
| 923 | if (!avctx->stats_out) { |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 924 | av_log(avctx, AV_LOG_ERROR, "Stat buffer alloc (%"SIZE_SPECIFIER" bytes) failed\n", |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 925 | b64_size); |
| 926 | return AVERROR(ENOMEM); |
| 927 | } |
| 928 | av_base64_encode(avctx->stats_out, b64_size, ctx->twopass_stats.buf, |
| 929 | ctx->twopass_stats.sz); |
| 930 | } |
| 931 | |
| 932 | *got_packet = !!coded_size; |
| 933 | return 0; |
| 934 | } |
| 935 | |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 936 | static const enum AVPixelFormat av1_pix_fmts[] = { |
| 937 | AV_PIX_FMT_YUV420P, |
| 938 | AV_PIX_FMT_YUV422P, |
| 939 | AV_PIX_FMT_YUV444P, |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 940 | AV_PIX_FMT_NONE |
| 941 | }; |
| 942 | |
| 943 | static const enum AVPixelFormat av1_pix_fmts_highbd[] = { |
| 944 | AV_PIX_FMT_YUV420P, |
| 945 | AV_PIX_FMT_YUV422P, |
| 946 | AV_PIX_FMT_YUV444P, |
| 947 | AV_PIX_FMT_YUV420P10, |
| 948 | AV_PIX_FMT_YUV422P10, |
| 949 | AV_PIX_FMT_YUV444P10, |
| 950 | AV_PIX_FMT_YUV420P12, |
| 951 | AV_PIX_FMT_YUV422P12, |
| 952 | AV_PIX_FMT_YUV444P12, |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 953 | AV_PIX_FMT_NONE |
| 954 | }; |
| 955 | |
| 956 | static av_cold void av1_init_static(AVCodec *codec) |
| 957 | { |
| 958 | aom_codec_caps_t codec_caps = aom_codec_get_caps(aom_codec_av1_cx()); |
| 959 | if (codec_caps & AOM_CODEC_CAP_HIGHBITDEPTH) |
| 960 | codec->pix_fmts = av1_pix_fmts_highbd; |
| 961 | else |
| 962 | codec->pix_fmts = av1_pix_fmts; |
| 963 | } |
| 964 | |
| 965 | static av_cold int av1_init(AVCodecContext *avctx) |
| 966 | { |
| 967 | return aom_init(avctx, aom_codec_av1_cx()); |
| 968 | } |
| 969 | |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 970 | #define OFFSET(x) offsetof(AOMContext, x) |
| 971 | #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM |
| 972 | static const AVOption options[] = { |
Gyan Doshi | 076b196 | 2018-08-05 06:03:45 | [diff] [blame] | 973 | { "cpu-used", "Quality/Speed ratio modifier", OFFSET(cpu_used), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 8, VE}, |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 974 | { "auto-alt-ref", "Enable use of alternate reference " |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 975 | "frames (2-pass only)", OFFSET(auto_alt_ref), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 2, VE}, |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 976 | { "lag-in-frames", "Number of frames to look ahead at for " |
| 977 | "alternate reference frame selection", OFFSET(lag_in_frames), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE}, |
| 978 | { "error-resilience", "Error resilience configuration", OFFSET(error_resilient), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, VE, "er"}, |
| 979 | { "default", "Improve resiliency against losses of whole frames", 0, AV_OPT_TYPE_CONST, {.i64 = AOM_ERROR_RESILIENT_DEFAULT}, 0, 0, VE, "er"}, |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 980 | { "crf", "Select the quality for constant quality mode", offsetof(AOMContext, crf), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 63, VE }, |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 981 | { "static-thresh", "A change threshold on blocks below which they will be skipped by the encoder", OFFSET(static_thresh), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE }, |
| 982 | { "drop-threshold", "Frame drop threshold", offsetof(AOMContext, drop_threshold), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, VE }, |
| 983 | { "noise-sensitivity", "Noise sensitivity", OFFSET(noise_sensitivity), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 4, VE}, |
Mark Thompson | 7070955 | 2018-10-22 22:40:07 | [diff] [blame^] | 984 | { "tiles", "Tile columns x rows", OFFSET(tile_cols), AV_OPT_TYPE_IMAGE_SIZE, { .str = NULL }, 0, 0, VE }, |
| 985 | { "tile-columns", "Log2 of number of tile columns to use", OFFSET(tile_cols_log2), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 6, VE}, |
| 986 | { "tile-rows", "Log2 of number of tile rows to use", OFFSET(tile_rows_log2), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 6, VE}, |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 987 | { NULL } |
| 988 | }; |
| 989 | |
| 990 | static const AVCodecDefault defaults[] = { |
| 991 | { "qmin", "-1" }, |
| 992 | { "qmax", "-1" }, |
| 993 | { "g", "-1" }, |
| 994 | { "keyint_min", "-1" }, |
| 995 | { NULL }, |
| 996 | }; |
| 997 | |
| 998 | static const AVClass class_aom = { |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 999 | .class_name = "libaom-av1 encoder", |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 1000 | .item_name = av_default_item_name, |
| 1001 | .option = options, |
| 1002 | .version = LIBAVUTIL_VERSION_INT, |
| 1003 | }; |
| 1004 | |
| 1005 | AVCodec ff_libaom_av1_encoder = { |
| 1006 | .name = "libaom-av1", |
| 1007 | .long_name = NULL_IF_CONFIG_SMALL("libaom AV1"), |
| 1008 | .type = AVMEDIA_TYPE_VIDEO, |
| 1009 | .id = AV_CODEC_ID_AV1, |
| 1010 | .priv_data_size = sizeof(AOMContext), |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 1011 | .init = av1_init, |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 1012 | .encode2 = aom_encode, |
| 1013 | .close = aom_free, |
| 1014 | .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AUTO_THREADS | AV_CODEC_CAP_EXPERIMENTAL, |
James Almer | c0f0c9f | 2018-03-29 02:28:34 | [diff] [blame] | 1015 | .profiles = NULL_IF_CONFIG_SMALL(ff_av1_profiles), |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 1016 | .priv_class = &class_aom, |
| 1017 | .defaults = defaults, |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 1018 | .init_static_data = av1_init_static, |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 1019 | .wrapper_name = "libaom", |
| 1020 | }; |