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" |
Andreas Rheinhardt | 69f120e | 2021-06-12 20:10:11 | [diff] [blame] | 33 | #include "libavutil/cpu.h" |
James Almer | b0cd979 | 2022-07-18 18:36:14 | [diff] [blame] | 34 | #include "libavutil/imgutils.h" |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 35 | #include "libavutil/mathematics.h" |
| 36 | #include "libavutil/opt.h" |
| 37 | #include "libavutil/pixdesc.h" |
| 38 | |
Mark Thompson | 7070955 | 2018-10-22 22:40:07 | [diff] [blame] | 39 | #include "av1.h" |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 40 | #include "avcodec.h" |
Andreas Rheinhardt | 57b5ec6 | 2021-06-11 21:35:33 | [diff] [blame] | 41 | #include "bsf.h" |
Andreas Rheinhardt | a688f3c | 2022-03-16 17:18:28 | [diff] [blame] | 42 | #include "codec_internal.h" |
Andreas Rheinhardt | 044daa7 | 2021-04-24 23:43:26 | [diff] [blame] | 43 | #include "encode.h" |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 44 | #include "internal.h" |
James Almer | b0cd979 | 2022-07-18 18:36:14 | [diff] [blame] | 45 | #include "libaom.h" |
James Almer | 6e19039 | 2020-06-02 21:38:33 | [diff] [blame] | 46 | #include "packet_internal.h" |
James Almer | c0f0c9f | 2018-03-29 02:28:34 | [diff] [blame] | 47 | #include "profiles.h" |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 48 | |
| 49 | /* |
| 50 | * Portion of struct aom_codec_cx_pkt from aom_encoder.h. |
| 51 | * One encoded frame returned from the library. |
| 52 | */ |
| 53 | struct FrameListData { |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 54 | void *buf; /**< compressed data buffer */ |
| 55 | size_t sz; /**< length of compressed data */ |
| 56 | int64_t pts; /**< time stamp to show frame |
| 57 | (in timebase units) */ |
| 58 | unsigned long duration; /**< duration to show frame |
| 59 | (in timebase units) */ |
| 60 | uint32_t flags; /**< flags for this frame */ |
Sam John | e265832 | 2018-09-28 22:09:05 | [diff] [blame] | 61 | uint64_t sse[4]; |
| 62 | int have_sse; /**< true if we have pending sse[] */ |
| 63 | uint64_t frame_number; |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 64 | struct FrameListData *next; |
| 65 | }; |
| 66 | |
| 67 | typedef struct AOMEncoderContext { |
| 68 | AVClass *class; |
James Almer | a754af9 | 2018-07-07 19:33:16 | [diff] [blame] | 69 | AVBSFContext *bsf; |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 70 | struct aom_codec_ctx encoder; |
| 71 | struct aom_image rawimg; |
| 72 | struct aom_fixed_buf twopass_stats; |
James Almer | 2251452 | 2022-08-23 23:32:33 | [diff] [blame] | 73 | unsigned twopass_stats_size; |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 74 | struct FrameListData *coded_frame_list; |
| 75 | int cpu_used; |
| 76 | int auto_alt_ref; |
Sam John via ffmpeg-devel | 995889a | 2019-03-29 16:58:38 | [diff] [blame] | 77 | int arnr_max_frames; |
| 78 | int arnr_strength; |
| 79 | int aq_mode; |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 80 | int lag_in_frames; |
| 81 | int error_resilient; |
| 82 | int crf; |
| 83 | int static_thresh; |
| 84 | int drop_threshold; |
Sam John via ffmpeg-devel | 995889a | 2019-03-29 16:58:38 | [diff] [blame] | 85 | int denoise_noise_level; |
| 86 | int denoise_block_size; |
Sam John | e265832 | 2018-09-28 22:09:05 | [diff] [blame] | 87 | uint64_t sse[4]; |
| 88 | int have_sse; /**< true if we have pending sse[] */ |
| 89 | uint64_t frame_number; |
Sam John via ffmpeg-devel | 995889a | 2019-03-29 16:58:38 | [diff] [blame] | 90 | int rc_undershoot_pct; |
| 91 | int rc_overshoot_pct; |
| 92 | int minsection_pct; |
| 93 | int maxsection_pct; |
| 94 | int frame_parallel; |
Mark Thompson | 7070955 | 2018-10-22 22:40:07 | [diff] [blame] | 95 | int tile_cols, tile_rows; |
| 96 | int tile_cols_log2, tile_rows_log2; |
| 97 | aom_superblock_size_t superblock_size; |
| 98 | int uniform_tiles; |
James Almer | d12d4d4 | 2018-12-08 23:14:12 | [diff] [blame] | 99 | int row_mt; |
Sam John via ffmpeg-devel | 995889a | 2019-03-29 16:58:38 | [diff] [blame] | 100 | int enable_cdef; |
| 101 | int enable_global_motion; |
| 102 | int enable_intrabc; |
Wang Cao | 8c9d82a | 2019-12-23 20:16:24 | [diff] [blame] | 103 | int enable_restoration; |
James Almer | c461500 | 2020-01-23 00:02:17 | [diff] [blame] | 104 | int usage; |
Wang Cao | bd3389e | 2020-04-03 21:00:26 | [diff] [blame] | 105 | int tune; |
Vignesh Venkatasubramanian | ab05e9a | 2022-05-02 21:37:01 | [diff] [blame] | 106 | int still_picture; |
Wang Cao | aa5c6f3 | 2020-06-26 00:55:13 | [diff] [blame] | 107 | int enable_rect_partitions; |
| 108 | int enable_1to4_partitions; |
| 109 | int enable_ab_partitions; |
Wang Cao | 297d5a1 | 2020-06-26 00:55:14 | [diff] [blame] | 110 | int enable_angle_delta; |
| 111 | int enable_cfl_intra; |
| 112 | int enable_paeth_intra; |
| 113 | int enable_smooth_intra; |
| 114 | int enable_intra_edge_filter; |
| 115 | int enable_palette; |
| 116 | int enable_filter_intra; |
Wang Cao | 498ad7b | 2020-07-22 23:12:01 | [diff] [blame] | 117 | int enable_flip_idtx; |
| 118 | int enable_tx64; |
| 119 | int reduced_tx_type_set; |
| 120 | int use_intra_dct_only; |
| 121 | int use_inter_dct_only; |
| 122 | int use_intra_default_tx_only; |
Wang Cao | 017bf96 | 2020-07-22 23:11:12 | [diff] [blame] | 123 | int enable_ref_frame_mvs; |
| 124 | int enable_interinter_wedge; |
| 125 | int enable_interintra_wedge; |
| 126 | int enable_interintra_comp; |
| 127 | int enable_masked_comp; |
| 128 | int enable_obmc; |
| 129 | int enable_onesided_comp; |
| 130 | int enable_reduced_reference_set; |
| 131 | int enable_smooth_interintra; |
| 132 | int enable_diff_wtd_comp; |
| 133 | int enable_dist_wtd_comp; |
| 134 | int enable_dual_filter; |
Bohan Li | 82aab8a | 2021-02-09 04:04:41 | [diff] [blame] | 135 | AVDictionary *aom_params; |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 136 | } AOMContext; |
| 137 | |
| 138 | static const char *const ctlidstr[] = { |
| 139 | [AOME_SET_CPUUSED] = "AOME_SET_CPUUSED", |
| 140 | [AOME_SET_CQ_LEVEL] = "AOME_SET_CQ_LEVEL", |
| 141 | [AOME_SET_ENABLEAUTOALTREF] = "AOME_SET_ENABLEAUTOALTREF", |
Sam John via ffmpeg-devel | 995889a | 2019-03-29 16:58:38 | [diff] [blame] | 142 | [AOME_SET_ARNR_MAXFRAMES] = "AOME_SET_ARNR_MAXFRAMES", |
| 143 | [AOME_SET_ARNR_STRENGTH] = "AOME_SET_ARNR_STRENGTH", |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 144 | [AOME_SET_STATIC_THRESHOLD] = "AOME_SET_STATIC_THRESHOLD", |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 145 | [AV1E_SET_COLOR_RANGE] = "AV1E_SET_COLOR_RANGE", |
James Almer | e5819fa | 2018-03-29 04:03:24 | [diff] [blame] | 146 | [AV1E_SET_COLOR_PRIMARIES] = "AV1E_SET_COLOR_PRIMARIES", |
| 147 | [AV1E_SET_MATRIX_COEFFICIENTS] = "AV1E_SET_MATRIX_COEFFICIENTS", |
| 148 | [AV1E_SET_TRANSFER_CHARACTERISTICS] = "AV1E_SET_TRANSFER_CHARACTERISTICS", |
Sam John via ffmpeg-devel | 995889a | 2019-03-29 16:58:38 | [diff] [blame] | 149 | [AV1E_SET_AQ_MODE] = "AV1E_SET_AQ_MODE", |
| 150 | [AV1E_SET_FRAME_PARALLEL_DECODING] = "AV1E_SET_FRAME_PARALLEL_DECODING", |
Mark Thompson | 7070955 | 2018-10-22 22:40:07 | [diff] [blame] | 151 | [AV1E_SET_SUPERBLOCK_SIZE] = "AV1E_SET_SUPERBLOCK_SIZE", |
| 152 | [AV1E_SET_TILE_COLUMNS] = "AV1E_SET_TILE_COLUMNS", |
| 153 | [AV1E_SET_TILE_ROWS] = "AV1E_SET_TILE_ROWS", |
Wang Cao | 8c9d82a | 2019-12-23 20:16:24 | [diff] [blame] | 154 | [AV1E_SET_ENABLE_RESTORATION] = "AV1E_SET_ENABLE_RESTORATION", |
James Almer | d12d4d4 | 2018-12-08 23:14:12 | [diff] [blame] | 155 | #ifdef AOM_CTRL_AV1E_SET_ROW_MT |
| 156 | [AV1E_SET_ROW_MT] = "AV1E_SET_ROW_MT", |
| 157 | #endif |
Sam John via ffmpeg-devel | 995889a | 2019-03-29 16:58:38 | [diff] [blame] | 158 | #ifdef AOM_CTRL_AV1E_SET_DENOISE_NOISE_LEVEL |
| 159 | [AV1E_SET_DENOISE_NOISE_LEVEL] = "AV1E_SET_DENOISE_NOISE_LEVEL", |
| 160 | #endif |
| 161 | #ifdef AOM_CTRL_AV1E_SET_DENOISE_BLOCK_SIZE |
| 162 | [AV1E_SET_DENOISE_BLOCK_SIZE] = "AV1E_SET_DENOISE_BLOCK_SIZE", |
| 163 | #endif |
| 164 | #ifdef AOM_CTRL_AV1E_SET_MAX_REFERENCE_FRAMES |
| 165 | [AV1E_SET_MAX_REFERENCE_FRAMES] = "AV1E_SET_MAX_REFERENCE_FRAMES", |
| 166 | #endif |
| 167 | #ifdef AOM_CTRL_AV1E_SET_ENABLE_GLOBAL_MOTION |
| 168 | [AV1E_SET_ENABLE_GLOBAL_MOTION] = "AV1E_SET_ENABLE_GLOBAL_MOTION", |
| 169 | #endif |
| 170 | #ifdef AOM_CTRL_AV1E_SET_ENABLE_INTRABC |
| 171 | [AV1E_SET_ENABLE_INTRABC] = "AV1E_SET_ENABLE_INTRABC", |
| 172 | #endif |
| 173 | [AV1E_SET_ENABLE_CDEF] = "AV1E_SET_ENABLE_CDEF", |
Wang Cao | bd3389e | 2020-04-03 21:00:26 | [diff] [blame] | 174 | [AOME_SET_TUNING] = "AOME_SET_TUNING", |
James Zern | d294716 | 2020-07-02 02:53:54 | [diff] [blame] | 175 | #if AOM_ENCODER_ABI_VERSION >= 22 |
Wang Cao | aa5c6f3 | 2020-06-26 00:55:13 | [diff] [blame] | 176 | [AV1E_SET_ENABLE_1TO4_PARTITIONS] = "AV1E_SET_ENABLE_1TO4_PARTITIONS", |
| 177 | [AV1E_SET_ENABLE_AB_PARTITIONS] = "AV1E_SET_ENABLE_AB_PARTITIONS", |
| 178 | [AV1E_SET_ENABLE_RECT_PARTITIONS] = "AV1E_SET_ENABLE_RECT_PARTITIONS", |
Wang Cao | 297d5a1 | 2020-06-26 00:55:14 | [diff] [blame] | 179 | [AV1E_SET_ENABLE_ANGLE_DELTA] = "AV1E_SET_ENABLE_ANGLE_DELTA", |
| 180 | [AV1E_SET_ENABLE_CFL_INTRA] = "AV1E_SET_ENABLE_CFL_INTRA", |
| 181 | [AV1E_SET_ENABLE_FILTER_INTRA] = "AV1E_SET_ENABLE_FILTER_INTRA", |
| 182 | [AV1E_SET_ENABLE_INTRA_EDGE_FILTER] = "AV1E_SET_ENABLE_INTRA_EDGE_FILTER", |
| 183 | [AV1E_SET_ENABLE_PAETH_INTRA] = "AV1E_SET_ENABLE_PAETH_INTRA", |
| 184 | [AV1E_SET_ENABLE_SMOOTH_INTRA] = "AV1E_SET_ENABLE_SMOOTH_INTRA", |
| 185 | [AV1E_SET_ENABLE_PALETTE] = "AV1E_SET_ENABLE_PALETTE", |
Wang Cao | 498ad7b | 2020-07-22 23:12:01 | [diff] [blame] | 186 | [AV1E_SET_ENABLE_FLIP_IDTX] = "AV1E_SET_ENABLE_FLIP_IDTX", |
| 187 | [AV1E_SET_ENABLE_TX64] = "AV1E_SET_ENABLE_TX64", |
| 188 | [AV1E_SET_INTRA_DCT_ONLY] = "AV1E_SET_INTRA_DCT_ONLY", |
| 189 | [AV1E_SET_INTER_DCT_ONLY] = "AV1E_SET_INTER_DCT_ONLY", |
| 190 | [AV1E_SET_INTRA_DEFAULT_TX_ONLY] = "AV1E_SET_INTRA_DEFAULT_TX_ONLY", |
| 191 | [AV1E_SET_REDUCED_TX_TYPE_SET] = "AV1E_SET_REDUCED_TX_TYPE_SET", |
Wang Cao | 017bf96 | 2020-07-22 23:11:12 | [diff] [blame] | 192 | [AV1E_SET_ENABLE_DIFF_WTD_COMP] = "AV1E_SET_ENABLE_DIFF_WTD_COMP", |
| 193 | [AV1E_SET_ENABLE_DIST_WTD_COMP] = "AV1E_SET_ENABLE_DIST_WTD_COMP", |
| 194 | [AV1E_SET_ENABLE_DUAL_FILTER] = "AV1E_SET_ENABLE_DUAL_FILTER", |
| 195 | [AV1E_SET_ENABLE_INTERINTER_WEDGE] = "AV1E_SET_ENABLE_INTERINTER_WEDGE", |
| 196 | [AV1E_SET_ENABLE_INTERINTRA_WEDGE] = "AV1E_SET_ENABLE_INTERINTRA_WEDGE", |
| 197 | [AV1E_SET_ENABLE_MASKED_COMP] = "AV1E_SET_ENABLE_MASKED_COMP", |
| 198 | [AV1E_SET_ENABLE_INTERINTRA_COMP] = "AV1E_SET_ENABLE_INTERINTRA_COMP", |
| 199 | [AV1E_SET_ENABLE_OBMC] = "AV1E_SET_ENABLE_OBMC", |
| 200 | [AV1E_SET_ENABLE_ONESIDED_COMP] = "AV1E_SET_ENABLE_ONESIDED_COMP", |
| 201 | [AV1E_SET_REDUCED_REFERENCE_SET] = "AV1E_SET_REDUCED_REFERENCE_SET", |
| 202 | [AV1E_SET_ENABLE_SMOOTH_INTERINTRA] = "AV1E_SET_ENABLE_SMOOTH_INTERINTRA", |
| 203 | [AV1E_SET_ENABLE_REF_FRAME_MVS] = "AV1E_SET_ENABLE_REF_FRAME_MVS", |
James Zern | d294716 | 2020-07-02 02:53:54 | [diff] [blame] | 204 | #endif |
Wan-Teh Chang | 38bc24b | 2022-06-16 16:24:22 | [diff] [blame] | 205 | #ifdef AOM_CTRL_AV1E_GET_NUM_OPERATING_POINTS |
| 206 | [AV1E_GET_NUM_OPERATING_POINTS] = "AV1E_GET_NUM_OPERATING_POINTS", |
| 207 | #endif |
Bohan Li | 950123d | 2022-04-19 18:18:51 | [diff] [blame] | 208 | #ifdef AOM_CTRL_AV1E_GET_SEQ_LEVEL_IDX |
| 209 | [AV1E_GET_SEQ_LEVEL_IDX] = "AV1E_GET_SEQ_LEVEL_IDX", |
| 210 | #endif |
| 211 | #ifdef AOM_CTRL_AV1E_GET_TARGET_SEQ_LEVEL_IDX |
| 212 | [AV1E_GET_TARGET_SEQ_LEVEL_IDX] = "AV1E_GET_TARGET_SEQ_LEVEL_IDX", |
| 213 | #endif |
James Almer | b0cd979 | 2022-07-18 18:36:14 | [diff] [blame] | 214 | [AV1_GET_NEW_FRAME_IMAGE] = "AV1_GET_NEW_FRAME_IMAGE", |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | static av_cold void log_encoder_error(AVCodecContext *avctx, const char *desc) |
| 218 | { |
| 219 | AOMContext *ctx = avctx->priv_data; |
| 220 | const char *error = aom_codec_error(&ctx->encoder); |
| 221 | const char *detail = aom_codec_error_detail(&ctx->encoder); |
| 222 | |
| 223 | av_log(avctx, AV_LOG_ERROR, "%s: %s\n", desc, error); |
| 224 | if (detail) |
| 225 | av_log(avctx, AV_LOG_ERROR, " Additional information: %s\n", detail); |
| 226 | } |
| 227 | |
| 228 | static av_cold void dump_enc_cfg(AVCodecContext *avctx, |
Matthieu Patou | 268f134 | 2021-06-14 18:18:24 | [diff] [blame] | 229 | const struct aom_codec_enc_cfg *cfg, |
| 230 | int level) |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 231 | { |
| 232 | int width = -30; |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 233 | |
| 234 | av_log(avctx, level, "aom_codec_enc_cfg\n"); |
| 235 | av_log(avctx, level, "generic settings\n" |
| 236 | " %*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] | 237 | " %*s%u\n %*s%u\n" |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 238 | " %*s{%u/%u}\n %*s%u\n %*s%d\n %*s%u\n", |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 239 | width, "g_usage:", cfg->g_usage, |
| 240 | width, "g_threads:", cfg->g_threads, |
| 241 | width, "g_profile:", cfg->g_profile, |
| 242 | width, "g_w:", cfg->g_w, |
| 243 | width, "g_h:", cfg->g_h, |
| 244 | width, "g_bit_depth:", cfg->g_bit_depth, |
| 245 | width, "g_input_bit_depth:", cfg->g_input_bit_depth, |
| 246 | width, "g_timebase:", cfg->g_timebase.num, cfg->g_timebase.den, |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 247 | width, "g_error_resilient:", cfg->g_error_resilient, |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 248 | width, "g_pass:", cfg->g_pass, |
| 249 | width, "g_lag_in_frames:", cfg->g_lag_in_frames); |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 250 | av_log(avctx, level, "rate control settings\n" |
James Almer | 416d354 | 2018-03-29 03:51:48 | [diff] [blame] | 251 | " %*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] | 252 | width, "rc_dropframe_thresh:", cfg->rc_dropframe_thresh, |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 253 | width, "rc_end_usage:", cfg->rc_end_usage, |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 254 | 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] | 255 | width, "rc_target_bitrate:", cfg->rc_target_bitrate); |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 256 | av_log(avctx, level, "quantizer settings\n" |
| 257 | " %*s%u\n %*s%u\n", |
| 258 | width, "rc_min_quantizer:", cfg->rc_min_quantizer, |
| 259 | width, "rc_max_quantizer:", cfg->rc_max_quantizer); |
| 260 | av_log(avctx, level, "bitrate tolerance\n" |
| 261 | " %*s%u\n %*s%u\n", |
| 262 | width, "rc_undershoot_pct:", cfg->rc_undershoot_pct, |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 263 | width, "rc_overshoot_pct:", cfg->rc_overshoot_pct); |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 264 | av_log(avctx, level, "decoder buffer model\n" |
| 265 | " %*s%u\n %*s%u\n %*s%u\n", |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 266 | width, "rc_buf_sz:", cfg->rc_buf_sz, |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 267 | width, "rc_buf_initial_sz:", cfg->rc_buf_initial_sz, |
| 268 | width, "rc_buf_optimal_sz:", cfg->rc_buf_optimal_sz); |
| 269 | av_log(avctx, level, "2 pass rate control settings\n" |
| 270 | " %*s%u\n %*s%u\n %*s%u\n", |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 271 | width, "rc_2pass_vbr_bias_pct:", cfg->rc_2pass_vbr_bias_pct, |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 272 | width, "rc_2pass_vbr_minsection_pct:", cfg->rc_2pass_vbr_minsection_pct, |
| 273 | width, "rc_2pass_vbr_maxsection_pct:", cfg->rc_2pass_vbr_maxsection_pct); |
| 274 | av_log(avctx, level, "keyframing settings\n" |
| 275 | " %*s%d\n %*s%u\n %*s%u\n", |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 276 | width, "kf_mode:", cfg->kf_mode, |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 277 | width, "kf_min_dist:", cfg->kf_min_dist, |
| 278 | width, "kf_max_dist:", cfg->kf_max_dist); |
Mark Thompson | 7070955 | 2018-10-22 22:40:07 | [diff] [blame] | 279 | av_log(avctx, level, "tile settings\n" |
| 280 | " %*s%d\n %*s%d\n", |
| 281 | width, "tile_width_count:", cfg->tile_width_count, |
| 282 | width, "tile_height_count:", cfg->tile_height_count); |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 283 | av_log(avctx, level, "\n"); |
| 284 | } |
| 285 | |
| 286 | static void coded_frame_add(void *list, struct FrameListData *cx_frame) |
| 287 | { |
| 288 | struct FrameListData **p = list; |
| 289 | |
| 290 | while (*p) |
| 291 | p = &(*p)->next; |
| 292 | *p = cx_frame; |
| 293 | cx_frame->next = NULL; |
| 294 | } |
| 295 | |
| 296 | static av_cold void free_coded_frame(struct FrameListData *cx_frame) |
| 297 | { |
| 298 | av_freep(&cx_frame->buf); |
| 299 | av_freep(&cx_frame); |
| 300 | } |
| 301 | |
| 302 | static av_cold void free_frame_list(struct FrameListData *list) |
| 303 | { |
| 304 | struct FrameListData *p = list; |
| 305 | |
| 306 | while (p) { |
| 307 | list = list->next; |
| 308 | free_coded_frame(p); |
| 309 | p = list; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | static av_cold int codecctl_int(AVCodecContext *avctx, |
Helmut K. C. Tessarek | aaf9171 | 2018-12-19 17:13:26 | [diff] [blame] | 314 | #ifdef UENUM1BYTE |
| 315 | aome_enc_control_id id, |
| 316 | #else |
| 317 | enum aome_enc_control_id id, |
| 318 | #endif |
| 319 | int val) |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 320 | { |
| 321 | AOMContext *ctx = avctx->priv_data; |
| 322 | char buf[80]; |
| 323 | int width = -30; |
| 324 | int res; |
| 325 | |
| 326 | snprintf(buf, sizeof(buf), "%s:", ctlidstr[id]); |
| 327 | av_log(avctx, AV_LOG_DEBUG, " %*s%d\n", width, buf, val); |
| 328 | |
| 329 | res = aom_codec_control(&ctx->encoder, id, val); |
| 330 | if (res != AOM_CODEC_OK) { |
| 331 | snprintf(buf, sizeof(buf), "Failed to set %s codec control", |
| 332 | ctlidstr[id]); |
| 333 | log_encoder_error(avctx, buf); |
| 334 | return AVERROR(EINVAL); |
| 335 | } |
| 336 | |
| 337 | return 0; |
| 338 | } |
| 339 | |
Wan-Teh Chang | 38bc24b | 2022-06-16 16:24:22 | [diff] [blame] | 340 | #if defined(AOM_CTRL_AV1E_GET_NUM_OPERATING_POINTS) && \ |
| 341 | defined(AOM_CTRL_AV1E_GET_SEQ_LEVEL_IDX) && \ |
Bohan Li | 950123d | 2022-04-19 18:18:51 | [diff] [blame] | 342 | defined(AOM_CTRL_AV1E_GET_TARGET_SEQ_LEVEL_IDX) |
| 343 | static av_cold int codecctl_intp(AVCodecContext *avctx, |
| 344 | #ifdef UENUM1BYTE |
| 345 | aome_enc_control_id id, |
| 346 | #else |
| 347 | enum aome_enc_control_id id, |
| 348 | #endif |
| 349 | int* ptr) |
| 350 | { |
| 351 | AOMContext *ctx = avctx->priv_data; |
| 352 | char buf[80]; |
| 353 | int width = -30; |
| 354 | int res; |
| 355 | |
| 356 | snprintf(buf, sizeof(buf), "%s:", ctlidstr[id]); |
| 357 | av_log(avctx, AV_LOG_DEBUG, " %*s%d\n", width, buf, *ptr); |
| 358 | |
| 359 | res = aom_codec_control(&ctx->encoder, id, ptr); |
| 360 | if (res != AOM_CODEC_OK) { |
| 361 | snprintf(buf, sizeof(buf), "Failed to set %s codec control", |
| 362 | ctlidstr[id]); |
| 363 | log_encoder_error(avctx, buf); |
| 364 | return AVERROR(EINVAL); |
| 365 | } |
| 366 | |
| 367 | return 0; |
| 368 | } |
| 369 | #endif |
| 370 | |
James Almer | b0cd979 | 2022-07-18 18:36:14 | [diff] [blame] | 371 | static av_cold int codecctl_imgp(AVCodecContext *avctx, |
| 372 | #ifdef UENUM1BYTE |
| 373 | aome_enc_control_id id, |
| 374 | #else |
| 375 | enum aome_enc_control_id id, |
| 376 | #endif |
| 377 | struct aom_image *img) |
| 378 | { |
| 379 | AOMContext *ctx = avctx->priv_data; |
| 380 | char buf[80]; |
| 381 | int res; |
| 382 | |
| 383 | snprintf(buf, sizeof(buf), "%s:", ctlidstr[id]); |
| 384 | |
| 385 | res = aom_codec_control(&ctx->encoder, id, img); |
| 386 | if (res != AOM_CODEC_OK) { |
| 387 | snprintf(buf, sizeof(buf), "Failed to get %s codec control", |
| 388 | ctlidstr[id]); |
| 389 | log_encoder_error(avctx, buf); |
| 390 | return AVERROR(EINVAL); |
| 391 | } |
| 392 | |
| 393 | return 0; |
| 394 | } |
| 395 | |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 396 | static av_cold int aom_free(AVCodecContext *avctx) |
| 397 | { |
| 398 | AOMContext *ctx = avctx->priv_data; |
| 399 | |
Wan-Teh Chang | 38bc24b | 2022-06-16 16:24:22 | [diff] [blame] | 400 | #if defined(AOM_CTRL_AV1E_GET_NUM_OPERATING_POINTS) && \ |
| 401 | defined(AOM_CTRL_AV1E_GET_SEQ_LEVEL_IDX) && \ |
Bohan Li | 950123d | 2022-04-19 18:18:51 | [diff] [blame] | 402 | defined(AOM_CTRL_AV1E_GET_TARGET_SEQ_LEVEL_IDX) |
James Almer | adc3873 | 2022-08-25 19:25:55 | [diff] [blame] | 403 | if (ctx->encoder.iface && !(avctx->flags & AV_CODEC_FLAG_PASS1)) { |
Wan-Teh Chang | 38bc24b | 2022-06-16 16:24:22 | [diff] [blame] | 404 | int num_operating_points; |
| 405 | int levels[32]; |
| 406 | int target_levels[32]; |
Bohan Li | 950123d | 2022-04-19 18:18:51 | [diff] [blame] | 407 | |
Wan-Teh Chang | 38bc24b | 2022-06-16 16:24:22 | [diff] [blame] | 408 | if (!codecctl_intp(avctx, AV1E_GET_NUM_OPERATING_POINTS, |
| 409 | &num_operating_points) && |
| 410 | !codecctl_intp(avctx, AV1E_GET_SEQ_LEVEL_IDX, levels) && |
Bohan Li | 950123d | 2022-04-19 18:18:51 | [diff] [blame] | 411 | !codecctl_intp(avctx, AV1E_GET_TARGET_SEQ_LEVEL_IDX, |
| 412 | target_levels)) { |
Wan-Teh Chang | 38bc24b | 2022-06-16 16:24:22 | [diff] [blame] | 413 | for (int i = 0; i < num_operating_points; i++) { |
Bohan Li | 950123d | 2022-04-19 18:18:51 | [diff] [blame] | 414 | if (levels[i] > target_levels[i]) { |
| 415 | // Warn when the target level was not met |
| 416 | av_log(avctx, AV_LOG_WARNING, |
| 417 | "Could not encode to target level %d.%d for " |
| 418 | "operating point %d. The output level is %d.%d.\n", |
| 419 | 2 + (target_levels[i] >> 2), target_levels[i] & 3, |
| 420 | i, 2 + (levels[i] >> 2), levels[i] & 3); |
| 421 | } else if (target_levels[i] < 31) { |
| 422 | // Log the encoded level if a target level was given |
| 423 | av_log(avctx, AV_LOG_INFO, |
| 424 | "Output level for operating point %d is %d.%d.\n", |
| 425 | i, 2 + (levels[i] >> 2), levels[i] & 3); |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | #endif |
| 431 | |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 432 | aom_codec_destroy(&ctx->encoder); |
| 433 | av_freep(&ctx->twopass_stats.buf); |
| 434 | av_freep(&avctx->stats_out); |
| 435 | free_frame_list(ctx->coded_frame_list); |
James Almer | a754af9 | 2018-07-07 19:33:16 | [diff] [blame] | 436 | av_bsf_free(&ctx->bsf); |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 437 | return 0; |
| 438 | } |
| 439 | |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 440 | static int set_pix_fmt(AVCodecContext *avctx, aom_codec_caps_t codec_caps, |
| 441 | struct aom_codec_enc_cfg *enccfg, aom_codec_flags_t *flags, |
| 442 | aom_img_fmt_t *img_fmt) |
| 443 | { |
| 444 | AOMContext av_unused *ctx = avctx->priv_data; |
James Almer | 36e51c1 | 2020-07-17 20:46:32 | [diff] [blame] | 445 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt); |
| 446 | enccfg->g_bit_depth = enccfg->g_input_bit_depth = desc->comp[0].depth; |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 447 | switch (avctx->pix_fmt) { |
Philip Langdale | 4013582 | 2020-12-08 00:33:29 | [diff] [blame] | 448 | case AV_PIX_FMT_GRAY8: |
| 449 | enccfg->monochrome = 1; |
| 450 | /* Fall-through */ |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 451 | case AV_PIX_FMT_YUV420P: |
| 452 | enccfg->g_profile = FF_PROFILE_AV1_MAIN; |
| 453 | *img_fmt = AOM_IMG_FMT_I420; |
| 454 | return 0; |
| 455 | case AV_PIX_FMT_YUV422P: |
| 456 | enccfg->g_profile = FF_PROFILE_AV1_PROFESSIONAL; |
| 457 | *img_fmt = AOM_IMG_FMT_I422; |
| 458 | return 0; |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 459 | case AV_PIX_FMT_YUV444P: |
Lynne | 6a2f3f6 | 2020-07-16 10:39:05 | [diff] [blame] | 460 | case AV_PIX_FMT_GBRP: |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 461 | enccfg->g_profile = FF_PROFILE_AV1_HIGH; |
| 462 | *img_fmt = AOM_IMG_FMT_I444; |
| 463 | return 0; |
Philip Langdale | 4013582 | 2020-12-08 00:33:29 | [diff] [blame] | 464 | case AV_PIX_FMT_GRAY10: |
| 465 | case AV_PIX_FMT_GRAY12: |
| 466 | enccfg->monochrome = 1; |
| 467 | /* Fall-through */ |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 468 | case AV_PIX_FMT_YUV420P10: |
| 469 | case AV_PIX_FMT_YUV420P12: |
| 470 | if (codec_caps & AOM_CODEC_CAP_HIGHBITDEPTH) { |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 471 | enccfg->g_profile = |
| 472 | enccfg->g_bit_depth == 10 ? FF_PROFILE_AV1_MAIN : FF_PROFILE_AV1_PROFESSIONAL; |
| 473 | *img_fmt = AOM_IMG_FMT_I42016; |
| 474 | *flags |= AOM_CODEC_USE_HIGHBITDEPTH; |
| 475 | return 0; |
| 476 | } |
| 477 | break; |
| 478 | case AV_PIX_FMT_YUV422P10: |
| 479 | case AV_PIX_FMT_YUV422P12: |
| 480 | if (codec_caps & AOM_CODEC_CAP_HIGHBITDEPTH) { |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 481 | enccfg->g_profile = FF_PROFILE_AV1_PROFESSIONAL; |
| 482 | *img_fmt = AOM_IMG_FMT_I42216; |
| 483 | *flags |= AOM_CODEC_USE_HIGHBITDEPTH; |
| 484 | return 0; |
| 485 | } |
| 486 | break; |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 487 | case AV_PIX_FMT_YUV444P10: |
| 488 | case AV_PIX_FMT_YUV444P12: |
Lynne | 6a2f3f6 | 2020-07-16 10:39:05 | [diff] [blame] | 489 | case AV_PIX_FMT_GBRP10: |
| 490 | case AV_PIX_FMT_GBRP12: |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 491 | if (codec_caps & AOM_CODEC_CAP_HIGHBITDEPTH) { |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 492 | enccfg->g_profile = |
| 493 | enccfg->g_bit_depth == 10 ? FF_PROFILE_AV1_HIGH : FF_PROFILE_AV1_PROFESSIONAL; |
| 494 | *img_fmt = AOM_IMG_FMT_I44416; |
| 495 | *flags |= AOM_CODEC_USE_HIGHBITDEPTH; |
| 496 | return 0; |
| 497 | } |
| 498 | break; |
| 499 | default: |
| 500 | break; |
| 501 | } |
| 502 | av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format.\n"); |
| 503 | return AVERROR_INVALIDDATA; |
| 504 | } |
| 505 | |
| 506 | static void set_color_range(AVCodecContext *avctx) |
| 507 | { |
Helmut K. C. Tessarek | aaf9171 | 2018-12-19 17:13:26 | [diff] [blame] | 508 | aom_color_range_t aom_cr; |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 509 | switch (avctx->color_range) { |
| 510 | case AVCOL_RANGE_UNSPECIFIED: |
| 511 | case AVCOL_RANGE_MPEG: aom_cr = AOM_CR_STUDIO_RANGE; break; |
| 512 | case AVCOL_RANGE_JPEG: aom_cr = AOM_CR_FULL_RANGE; break; |
| 513 | default: |
| 514 | av_log(avctx, AV_LOG_WARNING, "Unsupported color range (%d)\n", |
| 515 | avctx->color_range); |
| 516 | return; |
| 517 | } |
| 518 | |
| 519 | codecctl_int(avctx, AV1E_SET_COLOR_RANGE, aom_cr); |
| 520 | } |
| 521 | |
Mark Thompson | 7070955 | 2018-10-22 22:40:07 | [diff] [blame] | 522 | static int count_uniform_tiling(int dim, int sb_size, int tiles_log2) |
| 523 | { |
| 524 | int sb_dim = (dim + sb_size - 1) / sb_size; |
| 525 | int tile_dim = (sb_dim + (1 << tiles_log2) - 1) >> tiles_log2; |
| 526 | av_assert0(tile_dim > 0); |
| 527 | return (sb_dim + tile_dim - 1) / tile_dim; |
| 528 | } |
| 529 | |
| 530 | static int choose_tiling(AVCodecContext *avctx, |
| 531 | struct aom_codec_enc_cfg *enccfg) |
| 532 | { |
| 533 | AOMContext *ctx = avctx->priv_data; |
| 534 | int sb_128x128_possible, sb_size, sb_width, sb_height; |
| 535 | int uniform_rows, uniform_cols; |
| 536 | int uniform_64x64_possible, uniform_128x128_possible; |
| 537 | int tile_size, rounding, i; |
| 538 | |
| 539 | if (ctx->tile_cols_log2 >= 0) |
| 540 | ctx->tile_cols = 1 << ctx->tile_cols_log2; |
| 541 | if (ctx->tile_rows_log2 >= 0) |
| 542 | ctx->tile_rows = 1 << ctx->tile_rows_log2; |
| 543 | |
| 544 | if (ctx->tile_cols == 0) { |
| 545 | ctx->tile_cols = (avctx->width + AV1_MAX_TILE_WIDTH - 1) / |
| 546 | AV1_MAX_TILE_WIDTH; |
| 547 | if (ctx->tile_cols > 1) { |
| 548 | av_log(avctx, AV_LOG_DEBUG, "Automatically using %d tile " |
| 549 | "columns to fill width.\n", ctx->tile_cols); |
| 550 | } |
| 551 | } |
| 552 | av_assert0(ctx->tile_cols > 0); |
| 553 | if (ctx->tile_rows == 0) { |
| 554 | int max_tile_width = |
| 555 | FFALIGN((FFALIGN(avctx->width, 128) + |
| 556 | ctx->tile_cols - 1) / ctx->tile_cols, 128); |
| 557 | ctx->tile_rows = |
| 558 | (max_tile_width * FFALIGN(avctx->height, 128) + |
| 559 | AV1_MAX_TILE_AREA - 1) / AV1_MAX_TILE_AREA; |
| 560 | if (ctx->tile_rows > 1) { |
| 561 | av_log(avctx, AV_LOG_DEBUG, "Automatically using %d tile " |
| 562 | "rows to fill area.\n", ctx->tile_rows); |
| 563 | } |
| 564 | } |
| 565 | av_assert0(ctx->tile_rows > 0); |
| 566 | |
| 567 | if ((avctx->width + 63) / 64 < ctx->tile_cols || |
| 568 | (avctx->height + 63) / 64 < ctx->tile_rows) { |
| 569 | av_log(avctx, AV_LOG_ERROR, "Invalid tile sizing: frame not " |
| 570 | "large enough to fit specified tile arrangement.\n"); |
| 571 | return AVERROR(EINVAL); |
| 572 | } |
| 573 | if (ctx->tile_cols > AV1_MAX_TILE_COLS || |
| 574 | ctx->tile_rows > AV1_MAX_TILE_ROWS) { |
| 575 | av_log(avctx, AV_LOG_ERROR, "Invalid tile sizing: AV1 does " |
| 576 | "not allow more than %dx%d tiles.\n", |
| 577 | AV1_MAX_TILE_COLS, AV1_MAX_TILE_ROWS); |
| 578 | return AVERROR(EINVAL); |
| 579 | } |
| 580 | if (avctx->width / ctx->tile_cols > AV1_MAX_TILE_WIDTH) { |
| 581 | av_log(avctx, AV_LOG_ERROR, "Invalid tile sizing: AV1 does " |
| 582 | "not allow tiles of width greater than %d.\n", |
| 583 | AV1_MAX_TILE_WIDTH); |
| 584 | return AVERROR(EINVAL); |
| 585 | } |
| 586 | |
| 587 | ctx->superblock_size = AOM_SUPERBLOCK_SIZE_DYNAMIC; |
| 588 | |
| 589 | if (ctx->tile_cols == 1 && ctx->tile_rows == 1) { |
| 590 | av_log(avctx, AV_LOG_DEBUG, "Using a single tile.\n"); |
| 591 | return 0; |
| 592 | } |
| 593 | |
| 594 | sb_128x128_possible = |
| 595 | (avctx->width + 127) / 128 >= ctx->tile_cols && |
| 596 | (avctx->height + 127) / 128 >= ctx->tile_rows; |
| 597 | |
| 598 | ctx->tile_cols_log2 = ctx->tile_cols == 1 ? 0 : |
| 599 | av_log2(ctx->tile_cols - 1) + 1; |
| 600 | ctx->tile_rows_log2 = ctx->tile_rows == 1 ? 0 : |
| 601 | av_log2(ctx->tile_rows - 1) + 1; |
| 602 | |
| 603 | uniform_cols = count_uniform_tiling(avctx->width, |
| 604 | 64, ctx->tile_cols_log2); |
| 605 | uniform_rows = count_uniform_tiling(avctx->height, |
| 606 | 64, ctx->tile_rows_log2); |
| 607 | av_log(avctx, AV_LOG_DEBUG, "Uniform with 64x64 superblocks " |
| 608 | "-> %dx%d tiles.\n", uniform_cols, uniform_rows); |
| 609 | uniform_64x64_possible = uniform_cols == ctx->tile_cols && |
| 610 | uniform_rows == ctx->tile_rows; |
| 611 | |
| 612 | if (sb_128x128_possible) { |
| 613 | uniform_cols = count_uniform_tiling(avctx->width, |
| 614 | 128, ctx->tile_cols_log2); |
| 615 | uniform_rows = count_uniform_tiling(avctx->height, |
| 616 | 128, ctx->tile_rows_log2); |
| 617 | av_log(avctx, AV_LOG_DEBUG, "Uniform with 128x128 superblocks " |
| 618 | "-> %dx%d tiles.\n", uniform_cols, uniform_rows); |
| 619 | uniform_128x128_possible = uniform_cols == ctx->tile_cols && |
| 620 | uniform_rows == ctx->tile_rows; |
| 621 | } else { |
| 622 | av_log(avctx, AV_LOG_DEBUG, "128x128 superblocks not possible.\n"); |
| 623 | uniform_128x128_possible = 0; |
| 624 | } |
| 625 | |
| 626 | ctx->uniform_tiles = 1; |
| 627 | if (uniform_64x64_possible && uniform_128x128_possible) { |
| 628 | av_log(avctx, AV_LOG_DEBUG, "Using uniform tiling with dynamic " |
| 629 | "superblocks (tile_cols_log2 = %d, tile_rows_log2 = %d).\n", |
| 630 | ctx->tile_cols_log2, ctx->tile_rows_log2); |
| 631 | return 0; |
| 632 | } |
| 633 | if (uniform_64x64_possible && !sb_128x128_possible) { |
| 634 | av_log(avctx, AV_LOG_DEBUG, "Using uniform tiling with 64x64 " |
| 635 | "superblocks (tile_cols_log2 = %d, tile_rows_log2 = %d).\n", |
| 636 | ctx->tile_cols_log2, ctx->tile_rows_log2); |
| 637 | ctx->superblock_size = AOM_SUPERBLOCK_SIZE_64X64; |
| 638 | return 0; |
| 639 | } |
| 640 | if (uniform_128x128_possible) { |
| 641 | av_log(avctx, AV_LOG_DEBUG, "Using uniform tiling with 128x128 " |
| 642 | "superblocks (tile_cols_log2 = %d, tile_rows_log2 = %d).\n", |
| 643 | ctx->tile_cols_log2, ctx->tile_rows_log2); |
| 644 | ctx->superblock_size = AOM_SUPERBLOCK_SIZE_128X128; |
| 645 | return 0; |
| 646 | } |
| 647 | ctx->uniform_tiles = 0; |
| 648 | |
| 649 | if (sb_128x128_possible) { |
| 650 | sb_size = 128; |
| 651 | ctx->superblock_size = AOM_SUPERBLOCK_SIZE_128X128; |
| 652 | } else { |
| 653 | sb_size = 64; |
| 654 | ctx->superblock_size = AOM_SUPERBLOCK_SIZE_64X64; |
| 655 | } |
| 656 | av_log(avctx, AV_LOG_DEBUG, "Using fixed tiling with %dx%d " |
| 657 | "superblocks (tile_cols = %d, tile_rows = %d).\n", |
| 658 | sb_size, sb_size, ctx->tile_cols, ctx->tile_rows); |
| 659 | |
| 660 | enccfg->tile_width_count = ctx->tile_cols; |
| 661 | enccfg->tile_height_count = ctx->tile_rows; |
| 662 | |
| 663 | sb_width = (avctx->width + sb_size - 1) / sb_size; |
| 664 | sb_height = (avctx->height + sb_size - 1) / sb_size; |
| 665 | |
| 666 | tile_size = sb_width / ctx->tile_cols; |
| 667 | rounding = sb_width % ctx->tile_cols; |
| 668 | for (i = 0; i < ctx->tile_cols; i++) { |
| 669 | enccfg->tile_widths[i] = tile_size + |
| 670 | (i < rounding / 2 || |
| 671 | i > ctx->tile_cols - 1 - (rounding + 1) / 2); |
| 672 | } |
| 673 | |
| 674 | tile_size = sb_height / ctx->tile_rows; |
| 675 | rounding = sb_height % ctx->tile_rows; |
| 676 | for (i = 0; i < ctx->tile_rows; i++) { |
| 677 | enccfg->tile_heights[i] = tile_size + |
| 678 | (i < rounding / 2 || |
| 679 | i > ctx->tile_rows - 1 - (rounding + 1) / 2); |
| 680 | } |
| 681 | |
| 682 | return 0; |
| 683 | } |
| 684 | |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 685 | static av_cold int aom_init(AVCodecContext *avctx, |
| 686 | const struct aom_codec_iface *iface) |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 687 | { |
| 688 | AOMContext *ctx = avctx->priv_data; |
James Almer | 36e51c1 | 2020-07-17 20:46:32 | [diff] [blame] | 689 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt); |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 690 | struct aom_codec_enc_cfg enccfg = { 0 }; |
Sam John | e265832 | 2018-09-28 22:09:05 | [diff] [blame] | 691 | #ifdef AOM_FRAME_IS_INTRAONLY |
| 692 | aom_codec_flags_t flags = |
| 693 | (avctx->flags & AV_CODEC_FLAG_PSNR) ? AOM_CODEC_USE_PSNR : 0; |
| 694 | #else |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 695 | aom_codec_flags_t flags = 0; |
Sam John | e265832 | 2018-09-28 22:09:05 | [diff] [blame] | 696 | #endif |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 697 | AVCPBProperties *cpb_props; |
| 698 | int res; |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 699 | aom_img_fmt_t img_fmt; |
| 700 | aom_codec_caps_t codec_caps = aom_codec_get_caps(iface); |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 701 | |
| 702 | av_log(avctx, AV_LOG_INFO, "%s\n", aom_codec_version_str()); |
| 703 | av_log(avctx, AV_LOG_VERBOSE, "%s\n", aom_codec_build_config()); |
| 704 | |
James Zern | cb23c1e | 2021-08-14 02:00:50 | [diff] [blame] | 705 | if ((res = aom_codec_enc_config_default(iface, &enccfg, ctx->usage)) != AOM_CODEC_OK) { |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 706 | av_log(avctx, AV_LOG_ERROR, "Failed to get config: %s\n", |
| 707 | aom_codec_err_to_string(res)); |
| 708 | return AVERROR(EINVAL); |
| 709 | } |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 710 | |
| 711 | if (set_pix_fmt(avctx, codec_caps, &enccfg, &flags, &img_fmt)) |
| 712 | return AVERROR(EINVAL); |
| 713 | |
| 714 | if(!avctx->bit_rate) |
| 715 | if(avctx->rc_max_rate || avctx->rc_buffer_size || avctx->rc_initial_buffer_occupancy) { |
| 716 | av_log( avctx, AV_LOG_ERROR, "Rate control parameters set without a bitrate\n"); |
| 717 | return AVERROR(EINVAL); |
| 718 | } |
| 719 | |
Matthieu Patou | 268f134 | 2021-06-14 18:18:24 | [diff] [blame] | 720 | dump_enc_cfg(avctx, &enccfg, AV_LOG_DEBUG); |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 721 | |
| 722 | enccfg.g_w = avctx->width; |
| 723 | enccfg.g_h = avctx->height; |
| 724 | enccfg.g_timebase.num = avctx->time_base.num; |
| 725 | enccfg.g_timebase.den = avctx->time_base.den; |
Jun Zhao | b87063c | 2018-11-27 09:18:26 | [diff] [blame] | 726 | enccfg.g_threads = |
| 727 | FFMIN(avctx->thread_count ? avctx->thread_count : av_cpu_count(), 64); |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 728 | |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 729 | if (ctx->lag_in_frames >= 0) |
| 730 | enccfg.g_lag_in_frames = ctx->lag_in_frames; |
| 731 | |
| 732 | if (avctx->flags & AV_CODEC_FLAG_PASS1) |
| 733 | enccfg.g_pass = AOM_RC_FIRST_PASS; |
| 734 | else if (avctx->flags & AV_CODEC_FLAG_PASS2) |
| 735 | enccfg.g_pass = AOM_RC_LAST_PASS; |
| 736 | else |
| 737 | enccfg.g_pass = AOM_RC_ONE_PASS; |
| 738 | |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 739 | if (avctx->rc_min_rate == avctx->rc_max_rate && |
| 740 | avctx->rc_min_rate == avctx->bit_rate && avctx->bit_rate) { |
| 741 | enccfg.rc_end_usage = AOM_CBR; |
| 742 | } else if (ctx->crf >= 0) { |
| 743 | enccfg.rc_end_usage = AOM_CQ; |
| 744 | if (!avctx->bit_rate) |
| 745 | enccfg.rc_end_usage = AOM_Q; |
| 746 | } |
| 747 | |
| 748 | if (avctx->bit_rate) { |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 749 | enccfg.rc_target_bitrate = av_rescale_rnd(avctx->bit_rate, 1, 1000, |
| 750 | AV_ROUND_NEAR_INF); |
James Almer | 25abaf3 | 2018-03-29 19:18:27 | [diff] [blame] | 751 | } else if (enccfg.rc_end_usage != AOM_Q) { |
elliottk | ea673a0 | 2019-09-15 21:02:50 | [diff] [blame] | 752 | enccfg.rc_end_usage = AOM_Q; |
| 753 | ctx->crf = 32; |
| 754 | av_log(avctx, AV_LOG_WARNING, |
| 755 | "Neither bitrate nor constrained quality specified, using default CRF of %d\n", |
| 756 | ctx->crf); |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 757 | } |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 758 | |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 759 | if (avctx->qmin >= 0) |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 760 | enccfg.rc_min_quantizer = avctx->qmin; |
Carl Eugen Hoyos | 288ca1c | 2021-03-20 18:27:48 | [diff] [blame] | 761 | if (avctx->qmax >= 0) { |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 762 | enccfg.rc_max_quantizer = avctx->qmax; |
Carl Eugen Hoyos | 288ca1c | 2021-03-20 18:27:48 | [diff] [blame] | 763 | } else if (!ctx->crf) { |
| 764 | enccfg.rc_max_quantizer = 0; |
| 765 | } |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 766 | |
James Almer | 25abaf3 | 2018-03-29 19:18:27 | [diff] [blame] | 767 | if (enccfg.rc_end_usage == AOM_CQ || enccfg.rc_end_usage == AOM_Q) { |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 768 | if (ctx->crf < enccfg.rc_min_quantizer || ctx->crf > enccfg.rc_max_quantizer) { |
| 769 | av_log(avctx, AV_LOG_ERROR, |
| 770 | "CQ level %d must be between minimum and maximum quantizer value (%d-%d)\n", |
| 771 | ctx->crf, enccfg.rc_min_quantizer, enccfg.rc_max_quantizer); |
| 772 | return AVERROR(EINVAL); |
| 773 | } |
| 774 | } |
| 775 | |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 776 | enccfg.rc_dropframe_thresh = ctx->drop_threshold; |
| 777 | |
| 778 | // 0-100 (0 => CBR, 100 => VBR) |
| 779 | enccfg.rc_2pass_vbr_bias_pct = round(avctx->qcompress * 100); |
Sam John via ffmpeg-devel | 995889a | 2019-03-29 16:58:38 | [diff] [blame] | 780 | if (ctx->minsection_pct >= 0) |
| 781 | enccfg.rc_2pass_vbr_minsection_pct = ctx->minsection_pct; |
| 782 | else if (avctx->bit_rate) |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 783 | enccfg.rc_2pass_vbr_minsection_pct = |
| 784 | avctx->rc_min_rate * 100LL / avctx->bit_rate; |
Sam John via ffmpeg-devel | 995889a | 2019-03-29 16:58:38 | [diff] [blame] | 785 | if (ctx->maxsection_pct >= 0) |
| 786 | enccfg.rc_2pass_vbr_maxsection_pct = ctx->maxsection_pct; |
| 787 | else if (avctx->rc_max_rate) |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 788 | enccfg.rc_2pass_vbr_maxsection_pct = |
| 789 | avctx->rc_max_rate * 100LL / avctx->bit_rate; |
| 790 | |
| 791 | if (avctx->rc_buffer_size) |
| 792 | enccfg.rc_buf_sz = |
| 793 | avctx->rc_buffer_size * 1000LL / avctx->bit_rate; |
| 794 | if (avctx->rc_initial_buffer_occupancy) |
| 795 | enccfg.rc_buf_initial_sz = |
| 796 | avctx->rc_initial_buffer_occupancy * 1000LL / avctx->bit_rate; |
| 797 | enccfg.rc_buf_optimal_sz = enccfg.rc_buf_sz * 5 / 6; |
| 798 | |
Sam John via ffmpeg-devel | 995889a | 2019-03-29 16:58:38 | [diff] [blame] | 799 | if (ctx->rc_undershoot_pct >= 0) |
| 800 | enccfg.rc_undershoot_pct = ctx->rc_undershoot_pct; |
| 801 | if (ctx->rc_overshoot_pct >= 0) |
| 802 | enccfg.rc_overshoot_pct = ctx->rc_overshoot_pct; |
| 803 | |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 804 | // _enc_init() will balk if kf_min_dist differs from max w/AOM_KF_AUTO |
| 805 | if (avctx->keyint_min >= 0 && avctx->keyint_min == avctx->gop_size) |
| 806 | enccfg.kf_min_dist = avctx->keyint_min; |
| 807 | if (avctx->gop_size >= 0) |
| 808 | enccfg.kf_max_dist = avctx->gop_size; |
| 809 | |
| 810 | if (enccfg.g_pass == AOM_RC_FIRST_PASS) |
| 811 | enccfg.g_lag_in_frames = 0; |
| 812 | else if (enccfg.g_pass == AOM_RC_LAST_PASS) { |
| 813 | int decode_size, ret; |
| 814 | |
| 815 | if (!avctx->stats_in) { |
| 816 | av_log(avctx, AV_LOG_ERROR, "No stats file for second pass\n"); |
| 817 | return AVERROR_INVALIDDATA; |
| 818 | } |
| 819 | |
| 820 | ctx->twopass_stats.sz = strlen(avctx->stats_in) * 3 / 4; |
| 821 | ret = av_reallocp(&ctx->twopass_stats.buf, ctx->twopass_stats.sz); |
| 822 | if (ret < 0) { |
| 823 | av_log(avctx, AV_LOG_ERROR, |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 824 | "Stat buffer alloc (%"SIZE_SPECIFIER" bytes) failed\n", |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 825 | ctx->twopass_stats.sz); |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 826 | ctx->twopass_stats.sz = 0; |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 827 | return ret; |
| 828 | } |
| 829 | decode_size = av_base64_decode(ctx->twopass_stats.buf, avctx->stats_in, |
| 830 | ctx->twopass_stats.sz); |
| 831 | if (decode_size < 0) { |
| 832 | av_log(avctx, AV_LOG_ERROR, "Stat buffer decode failed\n"); |
| 833 | return AVERROR_INVALIDDATA; |
| 834 | } |
| 835 | |
| 836 | ctx->twopass_stats.sz = decode_size; |
| 837 | enccfg.rc_twopass_stats_in = ctx->twopass_stats; |
| 838 | } |
| 839 | |
| 840 | /* 0-3: For non-zero values the encoder increasingly optimizes for reduced |
| 841 | * complexity playback on low powered devices at the expense of encode |
| 842 | * quality. */ |
| 843 | if (avctx->profile != FF_PROFILE_UNKNOWN) |
| 844 | enccfg.g_profile = avctx->profile; |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 845 | |
| 846 | enccfg.g_error_resilient = ctx->error_resilient; |
| 847 | |
Mark Thompson | 7070955 | 2018-10-22 22:40:07 | [diff] [blame] | 848 | res = choose_tiling(avctx, &enccfg); |
| 849 | if (res < 0) |
| 850 | return res; |
| 851 | |
Vignesh Venkatasubramanian | ab05e9a | 2022-05-02 21:37:01 | [diff] [blame] | 852 | if (ctx->still_picture) { |
| 853 | // Set the maximum number of frames to 1. This will let libaom set |
| 854 | // still_picture and reduced_still_picture_header to 1 in the Sequence |
| 855 | // Header as required by AVIF still images. |
| 856 | enccfg.g_limit = 1; |
| 857 | // Reduce memory usage for still images. |
| 858 | enccfg.g_lag_in_frames = 0; |
| 859 | // All frames will be key frames. |
| 860 | enccfg.kf_max_dist = 0; |
| 861 | enccfg.kf_mode = AOM_KF_DISABLED; |
| 862 | } |
| 863 | |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 864 | /* Construct Encoder Context */ |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 865 | res = aom_codec_enc_init(&ctx->encoder, iface, &enccfg, flags); |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 866 | if (res != AOM_CODEC_OK) { |
Matthieu Patou | 268f134 | 2021-06-14 18:18:24 | [diff] [blame] | 867 | dump_enc_cfg(avctx, &enccfg, AV_LOG_WARNING); |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 868 | log_encoder_error(avctx, "Failed to initialize encoder"); |
| 869 | return AVERROR(EINVAL); |
| 870 | } |
Matthieu Patou | 268f134 | 2021-06-14 18:18:24 | [diff] [blame] | 871 | dump_enc_cfg(avctx, &enccfg, AV_LOG_DEBUG); |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 872 | |
| 873 | // codec control failures are currently treated only as warnings |
| 874 | av_log(avctx, AV_LOG_DEBUG, "aom_codec_control\n"); |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 875 | codecctl_int(avctx, AOME_SET_CPUUSED, ctx->cpu_used); |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 876 | if (ctx->auto_alt_ref >= 0) |
| 877 | codecctl_int(avctx, AOME_SET_ENABLEAUTOALTREF, ctx->auto_alt_ref); |
Sam John via ffmpeg-devel | 995889a | 2019-03-29 16:58:38 | [diff] [blame] | 878 | if (ctx->arnr_max_frames >= 0) |
| 879 | codecctl_int(avctx, AOME_SET_ARNR_MAXFRAMES, ctx->arnr_max_frames); |
| 880 | if (ctx->arnr_strength >= 0) |
| 881 | codecctl_int(avctx, AOME_SET_ARNR_STRENGTH, ctx->arnr_strength); |
| 882 | if (ctx->enable_cdef >= 0) |
| 883 | codecctl_int(avctx, AV1E_SET_ENABLE_CDEF, ctx->enable_cdef); |
Wang Cao | 8c9d82a | 2019-12-23 20:16:24 | [diff] [blame] | 884 | if (ctx->enable_restoration >= 0) |
| 885 | codecctl_int(avctx, AV1E_SET_ENABLE_RESTORATION, ctx->enable_restoration); |
James Zern | d294716 | 2020-07-02 02:53:54 | [diff] [blame] | 886 | #if AOM_ENCODER_ABI_VERSION >= 22 |
Wang Cao | aa5c6f3 | 2020-06-26 00:55:13 | [diff] [blame] | 887 | if (ctx->enable_rect_partitions >= 0) |
| 888 | codecctl_int(avctx, AV1E_SET_ENABLE_RECT_PARTITIONS, ctx->enable_rect_partitions); |
| 889 | if (ctx->enable_1to4_partitions >= 0) |
| 890 | codecctl_int(avctx, AV1E_SET_ENABLE_1TO4_PARTITIONS, ctx->enable_1to4_partitions); |
| 891 | if (ctx->enable_ab_partitions >= 0) |
| 892 | codecctl_int(avctx, AV1E_SET_ENABLE_AB_PARTITIONS, ctx->enable_ab_partitions); |
Wang Cao | 297d5a1 | 2020-06-26 00:55:14 | [diff] [blame] | 893 | if (ctx->enable_angle_delta >= 0) |
| 894 | codecctl_int(avctx, AV1E_SET_ENABLE_ANGLE_DELTA, ctx->enable_angle_delta); |
| 895 | if (ctx->enable_cfl_intra >= 0) |
| 896 | codecctl_int(avctx, AV1E_SET_ENABLE_CFL_INTRA, ctx->enable_cfl_intra); |
| 897 | if (ctx->enable_filter_intra >= 0) |
| 898 | codecctl_int(avctx, AV1E_SET_ENABLE_FILTER_INTRA, ctx->enable_filter_intra); |
| 899 | if (ctx->enable_intra_edge_filter >= 0) |
| 900 | codecctl_int(avctx, AV1E_SET_ENABLE_INTRA_EDGE_FILTER, ctx->enable_intra_edge_filter); |
| 901 | if (ctx->enable_paeth_intra >= 0) |
| 902 | codecctl_int(avctx, AV1E_SET_ENABLE_PAETH_INTRA, ctx->enable_paeth_intra); |
| 903 | if (ctx->enable_smooth_intra >= 0) |
| 904 | codecctl_int(avctx, AV1E_SET_ENABLE_SMOOTH_INTRA, ctx->enable_smooth_intra); |
| 905 | if (ctx->enable_palette >= 0) |
| 906 | codecctl_int(avctx, AV1E_SET_ENABLE_PALETTE, ctx->enable_palette); |
Wang Cao | 498ad7b | 2020-07-22 23:12:01 | [diff] [blame] | 907 | if (ctx->enable_tx64 >= 0) |
| 908 | codecctl_int(avctx, AV1E_SET_ENABLE_TX64, ctx->enable_tx64); |
| 909 | if (ctx->enable_flip_idtx >= 0) |
| 910 | codecctl_int(avctx, AV1E_SET_ENABLE_FLIP_IDTX, ctx->enable_flip_idtx); |
| 911 | if (ctx->use_intra_dct_only >= 0) |
| 912 | codecctl_int(avctx, AV1E_SET_INTRA_DCT_ONLY, ctx->use_intra_dct_only); |
| 913 | if (ctx->use_inter_dct_only >= 0) |
| 914 | codecctl_int(avctx, AV1E_SET_INTER_DCT_ONLY, ctx->use_inter_dct_only); |
| 915 | if (ctx->use_intra_default_tx_only >= 0) |
| 916 | codecctl_int(avctx, AV1E_SET_INTRA_DEFAULT_TX_ONLY, ctx->use_intra_default_tx_only); |
| 917 | if (ctx->reduced_tx_type_set >= 0) |
| 918 | codecctl_int(avctx, AV1E_SET_REDUCED_TX_TYPE_SET, ctx->reduced_tx_type_set); |
Wang Cao | 017bf96 | 2020-07-22 23:11:12 | [diff] [blame] | 919 | if (ctx->enable_ref_frame_mvs >= 0) |
| 920 | codecctl_int(avctx, AV1E_SET_ENABLE_REF_FRAME_MVS, ctx->enable_ref_frame_mvs); |
| 921 | if (ctx->enable_reduced_reference_set >= 0) |
| 922 | codecctl_int(avctx, AV1E_SET_REDUCED_REFERENCE_SET, ctx->enable_reduced_reference_set); |
| 923 | if (ctx->enable_diff_wtd_comp >= 0) |
| 924 | codecctl_int(avctx, AV1E_SET_ENABLE_DIFF_WTD_COMP, ctx->enable_diff_wtd_comp); |
| 925 | if (ctx->enable_dist_wtd_comp >= 0) |
| 926 | codecctl_int(avctx, AV1E_SET_ENABLE_DIST_WTD_COMP, ctx->enable_dist_wtd_comp); |
| 927 | if (ctx->enable_dual_filter >= 0) |
| 928 | codecctl_int(avctx, AV1E_SET_ENABLE_DUAL_FILTER, ctx->enable_dual_filter); |
| 929 | if (ctx->enable_interinter_wedge >= 0) |
| 930 | codecctl_int(avctx, AV1E_SET_ENABLE_INTERINTER_WEDGE, ctx->enable_interinter_wedge); |
| 931 | if (ctx->enable_masked_comp >= 0) |
| 932 | codecctl_int(avctx, AV1E_SET_ENABLE_MASKED_COMP, ctx->enable_masked_comp); |
| 933 | if (ctx->enable_interintra_comp >= 0) |
| 934 | codecctl_int(avctx, AV1E_SET_ENABLE_INTERINTRA_COMP, ctx->enable_interintra_comp); |
| 935 | if (ctx->enable_interintra_wedge >= 0) |
| 936 | codecctl_int(avctx, AV1E_SET_ENABLE_INTERINTRA_WEDGE, ctx->enable_interintra_wedge); |
| 937 | if (ctx->enable_obmc >= 0) |
| 938 | codecctl_int(avctx, AV1E_SET_ENABLE_OBMC, ctx->enable_obmc); |
| 939 | if (ctx->enable_onesided_comp >= 0) |
| 940 | codecctl_int(avctx, AV1E_SET_ENABLE_ONESIDED_COMP, ctx->enable_onesided_comp); |
| 941 | if (ctx->enable_smooth_interintra >= 0) |
| 942 | codecctl_int(avctx, AV1E_SET_ENABLE_SMOOTH_INTERINTRA, ctx->enable_smooth_interintra); |
James Zern | d294716 | 2020-07-02 02:53:54 | [diff] [blame] | 943 | #endif |
Wang Cao | 8c9d82a | 2019-12-23 20:16:24 | [diff] [blame] | 944 | |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 945 | codecctl_int(avctx, AOME_SET_STATIC_THRESHOLD, ctx->static_thresh); |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 946 | if (ctx->crf >= 0) |
| 947 | codecctl_int(avctx, AOME_SET_CQ_LEVEL, ctx->crf); |
Wang Cao | bd3389e | 2020-04-03 21:00:26 | [diff] [blame] | 948 | if (ctx->tune >= 0) |
| 949 | codecctl_int(avctx, AOME_SET_TUNING, ctx->tune); |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 950 | |
James Almer | 36e51c1 | 2020-07-17 20:46:32 | [diff] [blame] | 951 | if (desc->flags & AV_PIX_FMT_FLAG_RGB) { |
Lynne | 6a2f3f6 | 2020-07-16 10:39:05 | [diff] [blame] | 952 | codecctl_int(avctx, AV1E_SET_COLOR_PRIMARIES, AVCOL_PRI_BT709); |
| 953 | codecctl_int(avctx, AV1E_SET_MATRIX_COEFFICIENTS, AVCOL_SPC_RGB); |
| 954 | codecctl_int(avctx, AV1E_SET_TRANSFER_CHARACTERISTICS, AVCOL_TRC_IEC61966_2_1); |
| 955 | } else { |
| 956 | codecctl_int(avctx, AV1E_SET_COLOR_PRIMARIES, avctx->color_primaries); |
| 957 | codecctl_int(avctx, AV1E_SET_MATRIX_COEFFICIENTS, avctx->colorspace); |
| 958 | codecctl_int(avctx, AV1E_SET_TRANSFER_CHARACTERISTICS, avctx->color_trc); |
| 959 | } |
Sam John via ffmpeg-devel | 995889a | 2019-03-29 16:58:38 | [diff] [blame] | 960 | if (ctx->aq_mode >= 0) |
| 961 | codecctl_int(avctx, AV1E_SET_AQ_MODE, ctx->aq_mode); |
| 962 | if (ctx->frame_parallel >= 0) |
| 963 | codecctl_int(avctx, AV1E_SET_FRAME_PARALLEL_DECODING, ctx->frame_parallel); |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 964 | set_color_range(avctx); |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 965 | |
Mark Thompson | 7070955 | 2018-10-22 22:40:07 | [diff] [blame] | 966 | codecctl_int(avctx, AV1E_SET_SUPERBLOCK_SIZE, ctx->superblock_size); |
| 967 | if (ctx->uniform_tiles) { |
| 968 | codecctl_int(avctx, AV1E_SET_TILE_COLUMNS, ctx->tile_cols_log2); |
| 969 | codecctl_int(avctx, AV1E_SET_TILE_ROWS, ctx->tile_rows_log2); |
| 970 | } |
| 971 | |
Sam John via ffmpeg-devel | 995889a | 2019-03-29 16:58:38 | [diff] [blame] | 972 | #ifdef AOM_CTRL_AV1E_SET_DENOISE_NOISE_LEVEL |
| 973 | if (ctx->denoise_noise_level >= 0) |
| 974 | codecctl_int(avctx, AV1E_SET_DENOISE_NOISE_LEVEL, ctx->denoise_noise_level); |
| 975 | #endif |
| 976 | #ifdef AOM_CTRL_AV1E_SET_DENOISE_BLOCK_SIZE |
| 977 | if (ctx->denoise_block_size >= 0) |
| 978 | codecctl_int(avctx, AV1E_SET_DENOISE_BLOCK_SIZE, ctx->denoise_block_size); |
| 979 | #endif |
| 980 | #ifdef AOM_CTRL_AV1E_SET_ENABLE_GLOBAL_MOTION |
| 981 | if (ctx->enable_global_motion >= 0) |
| 982 | codecctl_int(avctx, AV1E_SET_ENABLE_GLOBAL_MOTION, ctx->enable_global_motion); |
| 983 | #endif |
| 984 | #ifdef AOM_CTRL_AV1E_SET_MAX_REFERENCE_FRAMES |
| 985 | if (avctx->refs >= 3) { |
| 986 | codecctl_int(avctx, AV1E_SET_MAX_REFERENCE_FRAMES, avctx->refs); |
| 987 | } |
| 988 | #endif |
James Almer | d12d4d4 | 2018-12-08 23:14:12 | [diff] [blame] | 989 | #ifdef AOM_CTRL_AV1E_SET_ROW_MT |
James Almer | 0856c5d | 2019-03-29 17:55:00 | [diff] [blame] | 990 | if (ctx->row_mt >= 0) |
| 991 | codecctl_int(avctx, AV1E_SET_ROW_MT, ctx->row_mt); |
James Almer | d12d4d4 | 2018-12-08 23:14:12 | [diff] [blame] | 992 | #endif |
Sam John via ffmpeg-devel | 995889a | 2019-03-29 16:58:38 | [diff] [blame] | 993 | #ifdef AOM_CTRL_AV1E_SET_ENABLE_INTRABC |
| 994 | if (ctx->enable_intrabc >= 0) |
| 995 | codecctl_int(avctx, AV1E_SET_ENABLE_INTRABC, ctx->enable_intrabc); |
| 996 | #endif |
James Almer | d12d4d4 | 2018-12-08 23:14:12 | [diff] [blame] | 997 | |
Bohan Li | 82aab8a | 2021-02-09 04:04:41 | [diff] [blame] | 998 | #if AOM_ENCODER_ABI_VERSION >= 23 |
| 999 | { |
| 1000 | AVDictionaryEntry *en = NULL; |
| 1001 | |
| 1002 | while ((en = av_dict_get(ctx->aom_params, "", en, AV_DICT_IGNORE_SUFFIX))) { |
| 1003 | int ret = aom_codec_set_option(&ctx->encoder, en->key, en->value); |
| 1004 | if (ret != AOM_CODEC_OK) { |
| 1005 | log_encoder_error(avctx, en->key); |
| 1006 | return AVERROR_EXTERNAL; |
| 1007 | } |
| 1008 | } |
| 1009 | } |
| 1010 | #endif |
| 1011 | |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 1012 | // provide dummy value to initialize wrapper, values will be updated each _encode() |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 1013 | aom_img_wrap(&ctx->rawimg, img_fmt, avctx->width, avctx->height, 1, |
| 1014 | (unsigned char*)1); |
| 1015 | |
| 1016 | if (codec_caps & AOM_CODEC_CAP_HIGHBITDEPTH) |
| 1017 | ctx->rawimg.bit_depth = enccfg.g_bit_depth; |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 1018 | |
| 1019 | cpb_props = ff_add_cpb_side_data(avctx); |
| 1020 | if (!cpb_props) |
| 1021 | return AVERROR(ENOMEM); |
| 1022 | |
James Almer | a754af9 | 2018-07-07 19:33:16 | [diff] [blame] | 1023 | if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) { |
| 1024 | const AVBitStreamFilter *filter = av_bsf_get_by_name("extract_extradata"); |
| 1025 | int ret; |
| 1026 | |
| 1027 | if (!filter) { |
| 1028 | av_log(avctx, AV_LOG_ERROR, "extract_extradata bitstream filter " |
| 1029 | "not found. This is a bug, please report it.\n"); |
| 1030 | return AVERROR_BUG; |
| 1031 | } |
| 1032 | ret = av_bsf_alloc(filter, &ctx->bsf); |
| 1033 | if (ret < 0) |
| 1034 | return ret; |
| 1035 | |
| 1036 | ret = avcodec_parameters_from_context(ctx->bsf->par_in, avctx); |
| 1037 | if (ret < 0) |
| 1038 | return ret; |
| 1039 | |
| 1040 | ret = av_bsf_init(ctx->bsf); |
| 1041 | if (ret < 0) |
| 1042 | return ret; |
| 1043 | } |
| 1044 | |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 1045 | if (enccfg.rc_end_usage == AOM_CBR || |
| 1046 | enccfg.g_pass != AOM_RC_ONE_PASS) { |
| 1047 | cpb_props->max_bitrate = avctx->rc_max_rate; |
| 1048 | cpb_props->min_bitrate = avctx->rc_min_rate; |
| 1049 | cpb_props->avg_bitrate = avctx->bit_rate; |
| 1050 | } |
| 1051 | cpb_props->buffer_size = avctx->rc_buffer_size; |
| 1052 | |
| 1053 | return 0; |
| 1054 | } |
| 1055 | |
Sam John | e265832 | 2018-09-28 22:09:05 | [diff] [blame] | 1056 | static inline void cx_pktcpy(AOMContext *ctx, |
| 1057 | struct FrameListData *dst, |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 1058 | const struct aom_codec_cx_pkt *src) |
| 1059 | { |
| 1060 | dst->pts = src->data.frame.pts; |
| 1061 | dst->duration = src->data.frame.duration; |
| 1062 | dst->flags = src->data.frame.flags; |
| 1063 | dst->sz = src->data.frame.sz; |
| 1064 | dst->buf = src->data.frame.buf; |
Sam John | e265832 | 2018-09-28 22:09:05 | [diff] [blame] | 1065 | #ifdef AOM_FRAME_IS_INTRAONLY |
Sam John | e265832 | 2018-09-28 22:09:05 | [diff] [blame] | 1066 | dst->frame_number = ++ctx->frame_number; |
| 1067 | dst->have_sse = ctx->have_sse; |
| 1068 | if (ctx->have_sse) { |
| 1069 | /* associate last-seen SSE to the frame. */ |
| 1070 | /* Transfers ownership from ctx to dst. */ |
| 1071 | memcpy(dst->sse, ctx->sse, sizeof(dst->sse)); |
| 1072 | ctx->have_sse = 0; |
| 1073 | } |
| 1074 | #endif |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 1075 | } |
| 1076 | |
| 1077 | /** |
| 1078 | * Store coded frame information in format suitable for return from encode2(). |
| 1079 | * |
| 1080 | * Write information from @a cx_frame to @a pkt |
| 1081 | * @return packet data size on success |
| 1082 | * @return a negative AVERROR on error |
| 1083 | */ |
| 1084 | static int storeframe(AVCodecContext *avctx, struct FrameListData *cx_frame, |
| 1085 | AVPacket *pkt) |
| 1086 | { |
James Almer | a754af9 | 2018-07-07 19:33:16 | [diff] [blame] | 1087 | AOMContext *ctx = avctx->priv_data; |
Carl Eugen Hoyos | 1e8475b | 2019-04-16 12:20:43 | [diff] [blame] | 1088 | int av_unused pict_type; |
Andreas Rheinhardt | 044daa7 | 2021-04-24 23:43:26 | [diff] [blame] | 1089 | int ret = ff_get_encode_buffer(avctx, pkt, cx_frame->sz, 0); |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 1090 | if (ret < 0) { |
| 1091 | av_log(avctx, AV_LOG_ERROR, |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 1092 | "Error getting output packet of size %"SIZE_SPECIFIER".\n", cx_frame->sz); |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 1093 | return ret; |
| 1094 | } |
| 1095 | memcpy(pkt->data, cx_frame->buf, pkt->size); |
| 1096 | pkt->pts = pkt->dts = cx_frame->pts; |
| 1097 | |
Sam John | e265832 | 2018-09-28 22:09:05 | [diff] [blame] | 1098 | if (!!(cx_frame->flags & AOM_FRAME_IS_KEY)) { |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 1099 | pkt->flags |= AV_PKT_FLAG_KEY; |
Sam John | e265832 | 2018-09-28 22:09:05 | [diff] [blame] | 1100 | #ifdef AOM_FRAME_IS_INTRAONLY |
| 1101 | pict_type = AV_PICTURE_TYPE_I; |
| 1102 | } else if (cx_frame->flags & AOM_FRAME_IS_INTRAONLY) { |
| 1103 | pict_type = AV_PICTURE_TYPE_I; |
| 1104 | } else { |
| 1105 | pict_type = AV_PICTURE_TYPE_P; |
| 1106 | } |
| 1107 | |
| 1108 | ff_side_data_set_encoder_stats(pkt, 0, cx_frame->sse + 1, |
| 1109 | cx_frame->have_sse ? 3 : 0, pict_type); |
| 1110 | |
| 1111 | if (cx_frame->have_sse) { |
| 1112 | int i; |
| 1113 | for (i = 0; i < 3; ++i) { |
| 1114 | avctx->error[i] += cx_frame->sse[i + 1]; |
| 1115 | } |
| 1116 | cx_frame->have_sse = 0; |
| 1117 | #endif |
| 1118 | } |
James Almer | a754af9 | 2018-07-07 19:33:16 | [diff] [blame] | 1119 | |
| 1120 | if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) { |
| 1121 | ret = av_bsf_send_packet(ctx->bsf, pkt); |
| 1122 | if (ret < 0) { |
| 1123 | av_log(avctx, AV_LOG_ERROR, "extract_extradata filter " |
| 1124 | "failed to send input packet\n"); |
| 1125 | return ret; |
| 1126 | } |
| 1127 | ret = av_bsf_receive_packet(ctx->bsf, pkt); |
| 1128 | |
| 1129 | if (ret < 0) { |
| 1130 | av_log(avctx, AV_LOG_ERROR, "extract_extradata filter " |
| 1131 | "failed to receive output packet\n"); |
| 1132 | return ret; |
| 1133 | } |
| 1134 | } |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 1135 | return pkt->size; |
| 1136 | } |
| 1137 | |
| 1138 | /** |
| 1139 | * Queue multiple output frames from the encoder, returning the front-most. |
| 1140 | * In cases where aom_codec_get_cx_data() returns more than 1 frame append |
| 1141 | * the frame queue. Return the head frame if available. |
| 1142 | * @return Stored frame size |
| 1143 | * @return AVERROR(EINVAL) on output size error |
| 1144 | * @return AVERROR(ENOMEM) on coded frame queue data allocation error |
| 1145 | */ |
| 1146 | static int queue_frames(AVCodecContext *avctx, AVPacket *pkt_out) |
| 1147 | { |
| 1148 | AOMContext *ctx = avctx->priv_data; |
| 1149 | const struct aom_codec_cx_pkt *pkt; |
| 1150 | const void *iter = NULL; |
| 1151 | int size = 0; |
| 1152 | |
| 1153 | if (ctx->coded_frame_list) { |
| 1154 | struct FrameListData *cx_frame = ctx->coded_frame_list; |
| 1155 | /* return the leading frame if we've already begun queueing */ |
| 1156 | size = storeframe(avctx, cx_frame, pkt_out); |
| 1157 | if (size < 0) |
| 1158 | return size; |
| 1159 | ctx->coded_frame_list = cx_frame->next; |
| 1160 | free_coded_frame(cx_frame); |
| 1161 | } |
| 1162 | |
| 1163 | /* consume all available output from the encoder before returning. buffers |
| 1164 | * are only good through the next aom_codec call */ |
| 1165 | while ((pkt = aom_codec_get_cx_data(&ctx->encoder, &iter))) { |
| 1166 | switch (pkt->kind) { |
| 1167 | case AOM_CODEC_CX_FRAME_PKT: |
| 1168 | if (!size) { |
| 1169 | struct FrameListData cx_frame; |
| 1170 | |
| 1171 | /* avoid storing the frame when the list is empty and we haven't yet |
| 1172 | * provided a frame for output */ |
James Almer | c42c99c | 2018-03-30 01:35:33 | [diff] [blame] | 1173 | av_assert0(!ctx->coded_frame_list); |
Sam John | e265832 | 2018-09-28 22:09:05 | [diff] [blame] | 1174 | cx_pktcpy(ctx, &cx_frame, pkt); |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 1175 | size = storeframe(avctx, &cx_frame, pkt_out); |
| 1176 | if (size < 0) |
| 1177 | return size; |
| 1178 | } else { |
| 1179 | struct FrameListData *cx_frame = |
| 1180 | av_malloc(sizeof(struct FrameListData)); |
| 1181 | |
| 1182 | if (!cx_frame) { |
| 1183 | av_log(avctx, AV_LOG_ERROR, |
| 1184 | "Frame queue element alloc failed\n"); |
| 1185 | return AVERROR(ENOMEM); |
| 1186 | } |
Sam John | e265832 | 2018-09-28 22:09:05 | [diff] [blame] | 1187 | cx_pktcpy(ctx, cx_frame, pkt); |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 1188 | cx_frame->buf = av_malloc(cx_frame->sz); |
| 1189 | |
| 1190 | if (!cx_frame->buf) { |
| 1191 | av_log(avctx, AV_LOG_ERROR, |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 1192 | "Data buffer alloc (%"SIZE_SPECIFIER" bytes) failed\n", |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 1193 | cx_frame->sz); |
| 1194 | av_freep(&cx_frame); |
| 1195 | return AVERROR(ENOMEM); |
| 1196 | } |
| 1197 | memcpy(cx_frame->buf, pkt->data.frame.buf, pkt->data.frame.sz); |
| 1198 | coded_frame_add(&ctx->coded_frame_list, cx_frame); |
| 1199 | } |
| 1200 | break; |
| 1201 | case AOM_CODEC_STATS_PKT: |
| 1202 | { |
| 1203 | struct aom_fixed_buf *stats = &ctx->twopass_stats; |
James Almer | 2251452 | 2022-08-23 23:32:33 | [diff] [blame] | 1204 | uint8_t *tmp = av_fast_realloc(stats->buf, |
| 1205 | &ctx->twopass_stats_size, |
| 1206 | stats->sz + |
| 1207 | pkt->data.twopass_stats.sz); |
| 1208 | if (!tmp) { |
| 1209 | av_freep(&stats->buf); |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 1210 | stats->sz = 0; |
| 1211 | av_log(avctx, AV_LOG_ERROR, "Stat buffer realloc failed\n"); |
James Almer | 2251452 | 2022-08-23 23:32:33 | [diff] [blame] | 1212 | return AVERROR(ENOMEM); |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 1213 | } |
James Almer | 2251452 | 2022-08-23 23:32:33 | [diff] [blame] | 1214 | stats->buf = tmp; |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 1215 | memcpy((uint8_t *)stats->buf + stats->sz, |
| 1216 | pkt->data.twopass_stats.buf, pkt->data.twopass_stats.sz); |
| 1217 | stats->sz += pkt->data.twopass_stats.sz; |
| 1218 | break; |
| 1219 | } |
Sam John | e265832 | 2018-09-28 22:09:05 | [diff] [blame] | 1220 | #ifdef AOM_FRAME_IS_INTRAONLY |
| 1221 | case AOM_CODEC_PSNR_PKT: |
| 1222 | { |
| 1223 | av_assert0(!ctx->have_sse); |
| 1224 | ctx->sse[0] = pkt->data.psnr.sse[0]; |
| 1225 | ctx->sse[1] = pkt->data.psnr.sse[1]; |
| 1226 | ctx->sse[2] = pkt->data.psnr.sse[2]; |
| 1227 | ctx->sse[3] = pkt->data.psnr.sse[3]; |
| 1228 | ctx->have_sse = 1; |
| 1229 | break; |
| 1230 | } |
| 1231 | #endif |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 1232 | case AOM_CODEC_CUSTOM_PKT: |
| 1233 | // ignore unsupported/unrecognized packet types |
| 1234 | break; |
| 1235 | } |
| 1236 | } |
| 1237 | |
| 1238 | return size; |
| 1239 | } |
| 1240 | |
James Almer | b0cd979 | 2022-07-18 18:36:14 | [diff] [blame] | 1241 | static enum AVPixelFormat aomfmt_to_pixfmt(struct aom_image *img) |
| 1242 | { |
| 1243 | switch (img->fmt) { |
| 1244 | case AOM_IMG_FMT_I420: |
| 1245 | case AOM_IMG_FMT_I42016: |
| 1246 | if (img->bit_depth == 8) |
| 1247 | return img->monochrome ? AV_PIX_FMT_GRAY8 : AV_PIX_FMT_YUV420P; |
| 1248 | else if (img->bit_depth == 10) |
| 1249 | return img->monochrome ? AV_PIX_FMT_GRAY10 : AV_PIX_FMT_YUV420P10; |
| 1250 | else |
| 1251 | return img->monochrome ? AV_PIX_FMT_GRAY12 : AV_PIX_FMT_YUV420P12; |
| 1252 | case AOM_IMG_FMT_I422: |
| 1253 | case AOM_IMG_FMT_I42216: |
| 1254 | if (img->bit_depth == 8) |
| 1255 | return AV_PIX_FMT_YUV422P; |
| 1256 | else if (img->bit_depth == 10) |
| 1257 | return AV_PIX_FMT_YUV422P10; |
| 1258 | else |
| 1259 | return AV_PIX_FMT_YUV422P12; |
| 1260 | case AOM_IMG_FMT_I444: |
| 1261 | case AOM_IMG_FMT_I44416: |
| 1262 | if (img->bit_depth == 8) |
| 1263 | return AV_PIX_FMT_YUV444P; |
| 1264 | else if (img->bit_depth == 10) |
| 1265 | return AV_PIX_FMT_YUV444P10; |
| 1266 | else |
| 1267 | return AV_PIX_FMT_YUV444P12; |
| 1268 | }; |
| 1269 | return AV_PIX_FMT_NONE; |
| 1270 | } |
| 1271 | |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 1272 | static int aom_encode(AVCodecContext *avctx, AVPacket *pkt, |
| 1273 | const AVFrame *frame, int *got_packet) |
| 1274 | { |
| 1275 | AOMContext *ctx = avctx->priv_data; |
| 1276 | struct aom_image *rawimg = NULL; |
| 1277 | int64_t timestamp = 0; |
| 1278 | int res, coded_size; |
| 1279 | aom_enc_frame_flags_t flags = 0; |
| 1280 | |
| 1281 | if (frame) { |
| 1282 | rawimg = &ctx->rawimg; |
| 1283 | rawimg->planes[AOM_PLANE_Y] = frame->data[0]; |
| 1284 | rawimg->planes[AOM_PLANE_U] = frame->data[1]; |
| 1285 | rawimg->planes[AOM_PLANE_V] = frame->data[2]; |
| 1286 | rawimg->stride[AOM_PLANE_Y] = frame->linesize[0]; |
| 1287 | rawimg->stride[AOM_PLANE_U] = frame->linesize[1]; |
| 1288 | rawimg->stride[AOM_PLANE_V] = frame->linesize[2]; |
| 1289 | timestamp = frame->pts; |
| 1290 | switch (frame->color_range) { |
| 1291 | case AVCOL_RANGE_MPEG: |
| 1292 | rawimg->range = AOM_CR_STUDIO_RANGE; |
| 1293 | break; |
| 1294 | case AVCOL_RANGE_JPEG: |
| 1295 | rawimg->range = AOM_CR_FULL_RANGE; |
| 1296 | break; |
| 1297 | } |
| 1298 | |
| 1299 | if (frame->pict_type == AV_PICTURE_TYPE_I) |
| 1300 | flags |= AOM_EFLAG_FORCE_KF; |
| 1301 | } |
| 1302 | |
| 1303 | res = aom_codec_encode(&ctx->encoder, rawimg, timestamp, |
| 1304 | avctx->ticks_per_frame, flags); |
| 1305 | if (res != AOM_CODEC_OK) { |
| 1306 | log_encoder_error(avctx, "Error encoding frame"); |
| 1307 | return AVERROR_INVALIDDATA; |
| 1308 | } |
| 1309 | coded_size = queue_frames(avctx, pkt); |
James Almer | 9f323cb | 2022-08-25 17:22:35 | [diff] [blame] | 1310 | if (coded_size < 0) |
| 1311 | return coded_size; |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 1312 | |
| 1313 | if (!frame && avctx->flags & AV_CODEC_FLAG_PASS1) { |
| 1314 | size_t b64_size = AV_BASE64_SIZE(ctx->twopass_stats.sz); |
| 1315 | |
| 1316 | avctx->stats_out = av_malloc(b64_size); |
| 1317 | if (!avctx->stats_out) { |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 1318 | 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] | 1319 | b64_size); |
| 1320 | return AVERROR(ENOMEM); |
| 1321 | } |
| 1322 | av_base64_encode(avctx->stats_out, b64_size, ctx->twopass_stats.buf, |
| 1323 | ctx->twopass_stats.sz); |
| 1324 | } |
| 1325 | |
| 1326 | *got_packet = !!coded_size; |
James Almer | b0cd979 | 2022-07-18 18:36:14 | [diff] [blame] | 1327 | |
| 1328 | if (*got_packet && avctx->flags & AV_CODEC_FLAG_RECON_FRAME) { |
| 1329 | AVCodecInternal *avci = avctx->internal; |
| 1330 | struct aom_image img; |
| 1331 | |
| 1332 | av_frame_unref(avci->recon_frame); |
| 1333 | |
| 1334 | res = codecctl_imgp(avctx, AV1_GET_NEW_FRAME_IMAGE, &img); |
| 1335 | if (res < 0) |
| 1336 | return res; |
| 1337 | |
| 1338 | avci->recon_frame->format = aomfmt_to_pixfmt(&img); |
| 1339 | if (avci->recon_frame->format == AV_PIX_FMT_NONE) { |
| 1340 | av_log(ctx, AV_LOG_ERROR, |
| 1341 | "Unhandled reconstructed frame colorspace: %d\n", |
| 1342 | img.fmt); |
| 1343 | return AVERROR(ENOSYS); |
| 1344 | } |
| 1345 | |
| 1346 | avci->recon_frame->width = img.d_w; |
| 1347 | avci->recon_frame->height = img.d_h; |
| 1348 | |
| 1349 | res = av_frame_get_buffer(avci->recon_frame, 0); |
| 1350 | if (res < 0) |
| 1351 | return res; |
| 1352 | |
| 1353 | if ((img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) && img.bit_depth == 8) |
| 1354 | ff_aom_image_copy_16_to_8(avci->recon_frame, &img); |
| 1355 | else { |
| 1356 | const uint8_t *planes[4] = { img.planes[0], img.planes[1], img.planes[2] }; |
| 1357 | const int stride[4] = { img.stride[0], img.stride[1], img.stride[2] }; |
| 1358 | |
| 1359 | av_image_copy(avci->recon_frame->data, avci->recon_frame->linesize, planes, |
| 1360 | stride, avci->recon_frame->format, img.d_w, img.d_h); |
| 1361 | } |
| 1362 | } |
| 1363 | |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 1364 | return 0; |
| 1365 | } |
| 1366 | |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 1367 | static const enum AVPixelFormat av1_pix_fmts[] = { |
| 1368 | AV_PIX_FMT_YUV420P, |
| 1369 | AV_PIX_FMT_YUV422P, |
| 1370 | AV_PIX_FMT_YUV444P, |
Lynne | 6a2f3f6 | 2020-07-16 10:39:05 | [diff] [blame] | 1371 | AV_PIX_FMT_GBRP, |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 1372 | AV_PIX_FMT_NONE |
| 1373 | }; |
| 1374 | |
Philip Langdale | 4013582 | 2020-12-08 00:33:29 | [diff] [blame] | 1375 | static const enum AVPixelFormat av1_pix_fmts_with_gray[] = { |
| 1376 | AV_PIX_FMT_YUV420P, |
| 1377 | AV_PIX_FMT_YUV422P, |
| 1378 | AV_PIX_FMT_YUV444P, |
| 1379 | AV_PIX_FMT_GBRP, |
| 1380 | AV_PIX_FMT_GRAY8, |
| 1381 | AV_PIX_FMT_NONE |
| 1382 | }; |
| 1383 | |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 1384 | static const enum AVPixelFormat av1_pix_fmts_highbd[] = { |
| 1385 | AV_PIX_FMT_YUV420P, |
| 1386 | AV_PIX_FMT_YUV422P, |
| 1387 | AV_PIX_FMT_YUV444P, |
Lynne | 6a2f3f6 | 2020-07-16 10:39:05 | [diff] [blame] | 1388 | AV_PIX_FMT_GBRP, |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 1389 | AV_PIX_FMT_YUV420P10, |
| 1390 | AV_PIX_FMT_YUV422P10, |
| 1391 | AV_PIX_FMT_YUV444P10, |
| 1392 | AV_PIX_FMT_YUV420P12, |
| 1393 | AV_PIX_FMT_YUV422P12, |
| 1394 | AV_PIX_FMT_YUV444P12, |
Lynne | 6a2f3f6 | 2020-07-16 10:39:05 | [diff] [blame] | 1395 | AV_PIX_FMT_GBRP10, |
| 1396 | AV_PIX_FMT_GBRP12, |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 1397 | AV_PIX_FMT_NONE |
| 1398 | }; |
| 1399 | |
Philip Langdale | 4013582 | 2020-12-08 00:33:29 | [diff] [blame] | 1400 | static const enum AVPixelFormat av1_pix_fmts_highbd_with_gray[] = { |
| 1401 | AV_PIX_FMT_YUV420P, |
| 1402 | AV_PIX_FMT_YUV422P, |
| 1403 | AV_PIX_FMT_YUV444P, |
| 1404 | AV_PIX_FMT_GBRP, |
| 1405 | AV_PIX_FMT_YUV420P10, |
| 1406 | AV_PIX_FMT_YUV422P10, |
| 1407 | AV_PIX_FMT_YUV444P10, |
| 1408 | AV_PIX_FMT_YUV420P12, |
| 1409 | AV_PIX_FMT_YUV422P12, |
| 1410 | AV_PIX_FMT_YUV444P12, |
| 1411 | AV_PIX_FMT_GBRP10, |
| 1412 | AV_PIX_FMT_GBRP12, |
| 1413 | AV_PIX_FMT_GRAY8, |
| 1414 | AV_PIX_FMT_GRAY10, |
| 1415 | AV_PIX_FMT_GRAY12, |
| 1416 | AV_PIX_FMT_NONE |
| 1417 | }; |
| 1418 | |
Andreas Rheinhardt | 20f9727 | 2022-03-16 20:09:54 | [diff] [blame] | 1419 | static av_cold void av1_init_static(FFCodec *codec) |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 1420 | { |
Philip Langdale | 4013582 | 2020-12-08 00:33:29 | [diff] [blame] | 1421 | int supports_monochrome = aom_codec_version() >= 20001; |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 1422 | aom_codec_caps_t codec_caps = aom_codec_get_caps(aom_codec_av1_cx()); |
| 1423 | if (codec_caps & AOM_CODEC_CAP_HIGHBITDEPTH) |
Andreas Rheinhardt | 20f9727 | 2022-03-16 20:09:54 | [diff] [blame] | 1424 | codec->p.pix_fmts = supports_monochrome ? av1_pix_fmts_highbd_with_gray : |
| 1425 | av1_pix_fmts_highbd; |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 1426 | else |
Andreas Rheinhardt | 20f9727 | 2022-03-16 20:09:54 | [diff] [blame] | 1427 | codec->p.pix_fmts = supports_monochrome ? av1_pix_fmts_with_gray : |
| 1428 | av1_pix_fmts; |
James Almer | 49d37b4 | 2020-05-27 01:27:42 | [diff] [blame] | 1429 | |
| 1430 | if (aom_codec_version_major() < 2) |
Andreas Rheinhardt | 20f9727 | 2022-03-16 20:09:54 | [diff] [blame] | 1431 | codec->p.capabilities |= AV_CODEC_CAP_EXPERIMENTAL; |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 1432 | } |
| 1433 | |
| 1434 | static av_cold int av1_init(AVCodecContext *avctx) |
| 1435 | { |
| 1436 | return aom_init(avctx, aom_codec_av1_cx()); |
| 1437 | } |
| 1438 | |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 1439 | #define OFFSET(x) offsetof(AOMContext, x) |
| 1440 | #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM |
| 1441 | static const AVOption options[] = { |
Gyan Doshi | 076b196 | 2018-08-05 06:03:45 | [diff] [blame] | 1442 | { "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] | 1443 | { "auto-alt-ref", "Enable use of alternate reference " |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 1444 | "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] | 1445 | { "lag-in-frames", "Number of frames to look ahead at for " |
| 1446 | "alternate reference frame selection", OFFSET(lag_in_frames), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE}, |
Sam John via ffmpeg-devel | 995889a | 2019-03-29 16:58:38 | [diff] [blame] | 1447 | { "arnr-max-frames", "altref noise reduction max frame count", OFFSET(arnr_max_frames), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE}, |
| 1448 | { "arnr-strength", "altref noise reduction filter strength", OFFSET(arnr_strength), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 6, VE}, |
| 1449 | { "aq-mode", "adaptive quantization mode", OFFSET(aq_mode), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 4, VE, "aq_mode"}, |
| 1450 | { "none", "Aq not used", 0, AV_OPT_TYPE_CONST, {.i64 = 0}, 0, 0, VE, "aq_mode"}, |
| 1451 | { "variance", "Variance based Aq", 0, AV_OPT_TYPE_CONST, {.i64 = 1}, 0, 0, VE, "aq_mode"}, |
| 1452 | { "complexity", "Complexity based Aq", 0, AV_OPT_TYPE_CONST, {.i64 = 2}, 0, 0, VE, "aq_mode"}, |
| 1453 | { "cyclic", "Cyclic Refresh Aq", 0, AV_OPT_TYPE_CONST, {.i64 = 3}, 0, 0, VE, "aq_mode"}, |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 1454 | { "error-resilience", "Error resilience configuration", OFFSET(error_resilient), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, VE, "er"}, |
| 1455 | { "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] | 1456 | { "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] | 1457 | { "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 }, |
| 1458 | { "drop-threshold", "Frame drop threshold", offsetof(AOMContext, drop_threshold), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, VE }, |
Sam John via ffmpeg-devel | 995889a | 2019-03-29 16:58:38 | [diff] [blame] | 1459 | { "denoise-noise-level", "Amount of noise to be removed", OFFSET(denoise_noise_level), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE}, |
| 1460 | { "denoise-block-size", "Denoise block size ", OFFSET(denoise_block_size), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE}, |
| 1461 | { "undershoot-pct", "Datarate undershoot (min) target (%)", OFFSET(rc_undershoot_pct), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 100, VE}, |
| 1462 | { "overshoot-pct", "Datarate overshoot (max) target (%)", OFFSET(rc_overshoot_pct), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1000, VE}, |
| 1463 | { "minsection-pct", "GOP min bitrate (% of target)", OFFSET(minsection_pct), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 100, VE}, |
| 1464 | { "maxsection-pct", "GOP max bitrate (% of target)", OFFSET(maxsection_pct), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 5000, VE}, |
| 1465 | { "frame-parallel", "Enable frame parallel decodability features", OFFSET(frame_parallel), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE}, |
Mark Thompson | 7070955 | 2018-10-22 22:40:07 | [diff] [blame] | 1466 | { "tiles", "Tile columns x rows", OFFSET(tile_cols), AV_OPT_TYPE_IMAGE_SIZE, { .str = NULL }, 0, 0, VE }, |
| 1467 | { "tile-columns", "Log2 of number of tile columns to use", OFFSET(tile_cols_log2), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 6, VE}, |
| 1468 | { "tile-rows", "Log2 of number of tile rows to use", OFFSET(tile_rows_log2), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 6, VE}, |
James Almer | 0856c5d | 2019-03-29 17:55:00 | [diff] [blame] | 1469 | { "row-mt", "Enable row based multi-threading", OFFSET(row_mt), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE}, |
Sam John via ffmpeg-devel | 995889a | 2019-03-29 16:58:38 | [diff] [blame] | 1470 | { "enable-cdef", "Enable CDEF filtering", OFFSET(enable_cdef), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE}, |
| 1471 | { "enable-global-motion", "Enable global motion", OFFSET(enable_global_motion), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE}, |
James Almer | 0e1ea03 | 2019-04-02 22:54:34 | [diff] [blame] | 1472 | { "enable-intrabc", "Enable intra block copy prediction mode", OFFSET(enable_intrabc), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE}, |
Wang Cao | 8c9d82a | 2019-12-23 20:16:24 | [diff] [blame] | 1473 | { "enable-restoration", "Enable Loop Restoration filtering", OFFSET(enable_restoration), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE}, |
James Zern | 0ece805 | 2020-04-11 22:22:35 | [diff] [blame] | 1474 | { "usage", "Quality and compression efficiency vs speed trade-off", OFFSET(usage), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, VE, "usage"}, |
James Almer | c461500 | 2020-01-23 00:02:17 | [diff] [blame] | 1475 | { "good", "Good quality", 0, AV_OPT_TYPE_CONST, {.i64 = 0 /* AOM_USAGE_GOOD_QUALITY */}, 0, 0, VE, "usage"}, |
| 1476 | { "realtime", "Realtime encoding", 0, AV_OPT_TYPE_CONST, {.i64 = 1 /* AOM_USAGE_REALTIME */}, 0, 0, VE, "usage"}, |
Vignesh Venkatasubramanian | f55c914 | 2022-05-18 17:37:55 | [diff] [blame] | 1477 | { "allintra", "All Intra encoding", 0, AV_OPT_TYPE_CONST, {.i64 = 2 /* AOM_USAGE_ALL_INTRA */}, 0, 0, VE, "usage"}, |
Wang Cao | bd3389e | 2020-04-03 21:00:26 | [diff] [blame] | 1478 | { "tune", "The metric that the encoder tunes for. Automatically chosen by the encoder by default", OFFSET(tune), AV_OPT_TYPE_INT, {.i64 = -1}, -1, AOM_TUNE_SSIM, VE, "tune"}, |
| 1479 | { "psnr", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = AOM_TUNE_PSNR}, 0, 0, VE, "tune"}, |
| 1480 | { "ssim", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = AOM_TUNE_SSIM}, 0, 0, VE, "tune"}, |
James Almer | 82b64e9 | 2020-06-26 02:28:57 | [diff] [blame] | 1481 | FF_AV1_PROFILE_OPTS |
Vignesh Venkatasubramanian | ab05e9a | 2022-05-02 21:37:01 | [diff] [blame] | 1482 | { "still-picture", "Encode in single frame mode (typically used for still AVIF images).", OFFSET(still_picture), AV_OPT_TYPE_BOOL, {.i64 = 0}, -1, 1, VE }, |
Wang Cao | aa5c6f3 | 2020-06-26 00:55:13 | [diff] [blame] | 1483 | { "enable-rect-partitions", "Enable rectangular partitions", OFFSET(enable_rect_partitions), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE}, |
| 1484 | { "enable-1to4-partitions", "Enable 1:4/4:1 partitions", OFFSET(enable_1to4_partitions), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE}, |
| 1485 | { "enable-ab-partitions", "Enable ab shape partitions", OFFSET(enable_ab_partitions), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE}, |
Wang Cao | 297d5a1 | 2020-06-26 00:55:14 | [diff] [blame] | 1486 | { "enable-angle-delta", "Enable angle delta intra prediction", OFFSET(enable_angle_delta), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE}, |
| 1487 | { "enable-cfl-intra", "Enable chroma predicted from luma intra prediction", OFFSET(enable_cfl_intra), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE}, |
| 1488 | { "enable-filter-intra", "Enable filter intra predictor", OFFSET(enable_filter_intra), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE}, |
| 1489 | { "enable-intra-edge-filter", "Enable intra edge filter", OFFSET(enable_intra_edge_filter), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE}, |
| 1490 | { "enable-smooth-intra", "Enable smooth intra prediction mode", OFFSET(enable_smooth_intra), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE}, |
| 1491 | { "enable-paeth-intra", "Enable paeth predictor in intra prediction", OFFSET(enable_paeth_intra), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE}, |
| 1492 | { "enable-palette", "Enable palette prediction mode", OFFSET(enable_palette), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE}, |
Wang Cao | 498ad7b | 2020-07-22 23:12:01 | [diff] [blame] | 1493 | { "enable-flip-idtx", "Enable extended transform type", OFFSET(enable_flip_idtx), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE}, |
| 1494 | { "enable-tx64", "Enable 64-pt transform", OFFSET(enable_tx64), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE}, |
| 1495 | { "reduced-tx-type-set", "Use reduced set of transform types", OFFSET(reduced_tx_type_set), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE}, |
| 1496 | { "use-intra-dct-only", "Use DCT only for INTRA modes", OFFSET(use_intra_dct_only), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE}, |
| 1497 | { "use-inter-dct-only", "Use DCT only for INTER modes", OFFSET(use_inter_dct_only), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE}, |
| 1498 | { "use-intra-default-tx-only", "Use default-transform only for INTRA modes", OFFSET(use_intra_default_tx_only), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE}, |
Wang Cao | 017bf96 | 2020-07-22 23:11:12 | [diff] [blame] | 1499 | { "enable-ref-frame-mvs", "Enable temporal mv prediction", OFFSET(enable_ref_frame_mvs), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE}, |
| 1500 | { "enable-reduced-reference-set", "Use reduced set of single and compound references", OFFSET(enable_reduced_reference_set), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE}, |
| 1501 | { "enable-obmc", "Enable obmc", OFFSET(enable_obmc), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE}, |
| 1502 | { "enable-dual-filter", "Enable dual filter", OFFSET(enable_dual_filter), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE}, |
| 1503 | { "enable-diff-wtd-comp", "Enable difference-weighted compound", OFFSET(enable_diff_wtd_comp), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE}, |
| 1504 | { "enable-dist-wtd-comp", "Enable distance-weighted compound", OFFSET(enable_dist_wtd_comp), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE}, |
| 1505 | { "enable-onesided-comp", "Enable one sided compound", OFFSET(enable_onesided_comp), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE}, |
| 1506 | { "enable-interinter-wedge", "Enable interinter wedge compound", OFFSET(enable_interinter_wedge), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE}, |
| 1507 | { "enable-interintra-wedge", "Enable interintra wedge compound", OFFSET(enable_interintra_wedge), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE}, |
| 1508 | { "enable-masked-comp", "Enable masked compound", OFFSET(enable_masked_comp), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE}, |
| 1509 | { "enable-interintra-comp", "Enable interintra compound", OFFSET(enable_interintra_comp), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE}, |
| 1510 | { "enable-smooth-interintra", "Enable smooth interintra mode", OFFSET(enable_smooth_interintra), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE}, |
Bohan Li | 82aab8a | 2021-02-09 04:04:41 | [diff] [blame] | 1511 | #if AOM_ENCODER_ABI_VERSION >= 23 |
| 1512 | { "aom-params", "Set libaom options using a :-separated list of key=value pairs", OFFSET(aom_params), AV_OPT_TYPE_DICT, { 0 }, 0, 0, VE }, |
| 1513 | #endif |
Sam John via ffmpeg-devel | 995889a | 2019-03-29 16:58:38 | [diff] [blame] | 1514 | { NULL }, |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 1515 | }; |
| 1516 | |
Andreas Rheinhardt | 5aabb25 | 2022-03-16 20:26:11 | [diff] [blame] | 1517 | static const FFCodecDefault defaults[] = { |
elliottk | 711c59b | 2019-08-21 19:18:20 | [diff] [blame] | 1518 | { "b", "0" }, |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 1519 | { "qmin", "-1" }, |
| 1520 | { "qmax", "-1" }, |
| 1521 | { "g", "-1" }, |
| 1522 | { "keyint_min", "-1" }, |
| 1523 | { NULL }, |
| 1524 | }; |
| 1525 | |
| 1526 | static const AVClass class_aom = { |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 1527 | .class_name = "libaom-av1 encoder", |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 1528 | .item_name = av_default_item_name, |
| 1529 | .option = options, |
| 1530 | .version = LIBAVUTIL_VERSION_INT, |
| 1531 | }; |
| 1532 | |
Andreas Rheinhardt | 20f9727 | 2022-03-16 20:09:54 | [diff] [blame] | 1533 | FFCodec ff_libaom_av1_encoder = { |
| 1534 | .p.name = "libaom-av1", |
Andreas Rheinhardt | 48286d4 | 2022-08-29 11:38:02 | [diff] [blame^] | 1535 | CODEC_LONG_NAME("libaom AV1"), |
Andreas Rheinhardt | 20f9727 | 2022-03-16 20:09:54 | [diff] [blame] | 1536 | .p.type = AVMEDIA_TYPE_VIDEO, |
| 1537 | .p.id = AV_CODEC_ID_AV1, |
| 1538 | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | |
James Almer | b0cd979 | 2022-07-18 18:36:14 | [diff] [blame] | 1539 | AV_CODEC_CAP_ENCODER_RECON_FRAME | |
Andreas Rheinhardt | 044daa7 | 2021-04-24 23:43:26 | [diff] [blame] | 1540 | AV_CODEC_CAP_OTHER_THREADS, |
Andreas Rheinhardt | 20f9727 | 2022-03-16 20:09:54 | [diff] [blame] | 1541 | .p.profiles = NULL_IF_CONFIG_SMALL(ff_av1_profiles), |
| 1542 | .p.priv_class = &class_aom, |
| 1543 | .p.wrapper_name = "libaom", |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 1544 | .priv_data_size = sizeof(AOMContext), |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 1545 | .init = av1_init, |
Andreas Rheinhardt | 4243da4 | 2022-03-30 21:28:24 | [diff] [blame] | 1546 | FF_CODEC_ENCODE_CB(aom_encode), |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 1547 | .close = aom_free, |
Andreas Rheinhardt | 6aad120 | 2022-07-09 20:25:41 | [diff] [blame] | 1548 | .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE | |
James Almer | 5bab794 | 2022-08-25 17:11:54 | [diff] [blame] | 1549 | FF_CODEC_CAP_INIT_CLEANUP | |
Andreas Rheinhardt | 6aad120 | 2022-07-09 20:25:41 | [diff] [blame] | 1550 | FF_CODEC_CAP_AUTO_THREADS, |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 1551 | .defaults = defaults, |
James Almer | 99cc3cf | 2018-03-29 02:15:18 | [diff] [blame] | 1552 | .init_static_data = av1_init_static, |
Luca Barbato | 43778a5 | 2018-02-11 20:33:25 | [diff] [blame] | 1553 | }; |