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