John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1 | /* |
John Koleszar | c2140b8 | 2010-09-09 08:16:39 -0400 | [diff] [blame] | 2 | * Copyright (c) 2010 The WebM project authors. All Rights Reserved. |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 3 | * |
John Koleszar | 94c52e4 | 2010-06-18 12:39:21 -0400 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
John Koleszar | 09202d8 | 2010-06-04 16:19:40 -0400 | [diff] [blame] | 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
John Koleszar | 94c52e4 | 2010-06-18 12:39:21 -0400 | [diff] [blame] | 7 | * in the file PATENTS. All contributing project authors may |
John Koleszar | 09202d8 | 2010-06-04 16:19:40 -0400 | [diff] [blame] | 8 | * be found in the AUTHORS file in the root of the source tree. |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | |
Minghai Shang | e899859 | 2014-07-14 11:24:17 -0700 | [diff] [blame] | 12 | #include "./vpx_config.h" |
Johann | 14ef4ae | 2015-04-15 09:27:00 -0400 | [diff] [blame] | 13 | #include "./vp8_rtcd.h" |
Johann | d5d9289 | 2015-04-17 16:11:38 -0400 | [diff] [blame] | 14 | #include "./vpx_dsp_rtcd.h" |
Johann | 14ef4ae | 2015-04-15 09:27:00 -0400 | [diff] [blame] | 15 | #include "./vpx_scale_rtcd.h" |
John Koleszar | b749234 | 2010-05-24 11:39:59 -0400 | [diff] [blame] | 16 | #include "vpx/vpx_codec.h" |
| 17 | #include "vpx/internal/vpx_codec_internal.h" |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 18 | #include "vpx_version.h" |
Dmitry Kovalev | b08fab8 | 2014-09-02 18:26:53 -0700 | [diff] [blame] | 19 | #include "vpx_mem/vpx_mem.h" |
Ronald S. Bultje | 54d4895 | 2015-09-30 11:07:35 -0400 | [diff] [blame] | 20 | #include "vpx_ports/vpx_once.h" |
John Koleszar | 02321de | 2011-02-10 14:41:38 -0500 | [diff] [blame] | 21 | #include "vp8/encoder/onyx_int.h" |
John Koleszar | 2bf8fb5 | 2012-05-02 16:37:37 -0700 | [diff] [blame] | 22 | #include "vpx/vp8cx.h" |
John Koleszar | bb7dd5b | 2010-10-14 16:40:12 -0400 | [diff] [blame] | 23 | #include "vp8/encoder/firstpass.h" |
John Koleszar | 02321de | 2011-02-10 14:41:38 -0500 | [diff] [blame] | 24 | #include "vp8/common/onyx.h" |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 28 | struct vp8_extracfg |
| 29 | { |
| 30 | struct vpx_codec_pkt_list *pkt_list; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 31 | int cpu_used; /** available cpu percentage in 1/16*/ |
| 32 | unsigned int enable_auto_alt_ref; /** if encoder decides to uses alternate reference frame */ |
| 33 | unsigned int noise_sensitivity; |
| 34 | unsigned int Sharpness; |
| 35 | unsigned int static_thresh; |
| 36 | unsigned int token_partitions; |
| 37 | unsigned int arnr_max_frames; /* alt_ref Noise Reduction Max Frame Count */ |
| 38 | unsigned int arnr_strength; /* alt_ref Noise Reduction Strength */ |
| 39 | unsigned int arnr_type; /* alt_ref filter type */ |
John Koleszar | b0da9b3 | 2010-12-17 09:43:39 -0500 | [diff] [blame] | 40 | vp8e_tuning tuning; |
Paul Wilkins | e0846c9 | 2011-01-07 18:29:37 +0000 | [diff] [blame] | 41 | unsigned int cq_level; /* constrained quality level */ |
John Koleszar | 1654ae9 | 2011-07-28 09:17:32 -0400 | [diff] [blame] | 42 | unsigned int rc_max_intra_bitrate_pct; |
Marco | af898b5 | 2014-11-10 13:07:05 -0800 | [diff] [blame] | 43 | unsigned int screen_content_mode; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 44 | |
| 45 | }; |
| 46 | |
Dmitry Kovalev | 48274c6 | 2014-08-21 11:21:18 -0700 | [diff] [blame] | 47 | static struct vp8_extracfg default_extracfg = { |
| 48 | NULL, |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 49 | #if !(CONFIG_REALTIME_ONLY) |
Dmitry Kovalev | 48274c6 | 2014-08-21 11:21:18 -0700 | [diff] [blame] | 50 | 0, /* cpu_used */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 51 | #else |
Dmitry Kovalev | 48274c6 | 2014-08-21 11:21:18 -0700 | [diff] [blame] | 52 | 4, /* cpu_used */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 53 | #endif |
Dmitry Kovalev | 48274c6 | 2014-08-21 11:21:18 -0700 | [diff] [blame] | 54 | 0, /* enable_auto_alt_ref */ |
| 55 | 0, /* noise_sensitivity */ |
| 56 | 0, /* Sharpness */ |
| 57 | 0, /* static_thresh */ |
Attila Nagy | 52cf4dc | 2012-02-09 12:37:03 +0200 | [diff] [blame] | 58 | #if (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING) |
Dmitry Kovalev | 48274c6 | 2014-08-21 11:21:18 -0700 | [diff] [blame] | 59 | VP8_EIGHT_TOKENPARTITION, |
Attila Nagy | 52cf4dc | 2012-02-09 12:37:03 +0200 | [diff] [blame] | 60 | #else |
Dmitry Kovalev | 48274c6 | 2014-08-21 11:21:18 -0700 | [diff] [blame] | 61 | VP8_ONE_TOKENPARTITION, /* token_partitions */ |
Attila Nagy | 52cf4dc | 2012-02-09 12:37:03 +0200 | [diff] [blame] | 62 | #endif |
Dmitry Kovalev | 48274c6 | 2014-08-21 11:21:18 -0700 | [diff] [blame] | 63 | 0, /* arnr_max_frames */ |
| 64 | 3, /* arnr_strength */ |
| 65 | 3, /* arnr_type*/ |
| 66 | 0, /* tuning*/ |
| 67 | 10, /* cq_level */ |
| 68 | 0, /* rc_max_intra_bitrate_pct */ |
Marco | af898b5 | 2014-11-10 13:07:05 -0800 | [diff] [blame] | 69 | 0, /* screen_content_mode */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | struct vpx_codec_alg_priv |
| 73 | { |
| 74 | vpx_codec_priv_t base; |
| 75 | vpx_codec_enc_cfg_t cfg; |
| 76 | struct vp8_extracfg vp8_cfg; |
| 77 | VP8_CONFIG oxcf; |
John Koleszar | b0056c3 | 2011-12-20 16:54:54 -0800 | [diff] [blame] | 78 | struct VP8_COMP *cpi; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 79 | unsigned char *cx_data; |
| 80 | unsigned int cx_data_sz; |
| 81 | vpx_image_t preview_img; |
| 82 | unsigned int next_frame_flag; |
| 83 | vp8_postproc_cfg_t preview_ppcfg; |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 84 | /* pkt_list size depends on the maximum number of lagged frames allowed. */ |
| 85 | vpx_codec_pkt_list_decl(64) pkt_list; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 86 | unsigned int fixed_kf_cntr; |
Marco | af898b5 | 2014-11-10 13:07:05 -0800 | [diff] [blame] | 87 | vpx_enc_frame_flags_t control_frame_flags; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | |
| 91 | static vpx_codec_err_t |
| 92 | update_error_state(vpx_codec_alg_priv_t *ctx, |
| 93 | const struct vpx_internal_error_info *error) |
| 94 | { |
| 95 | vpx_codec_err_t res; |
| 96 | |
| 97 | if ((res = error->error_code)) |
| 98 | ctx->base.err_detail = error->has_detail |
| 99 | ? error->detail |
| 100 | : NULL; |
| 101 | |
| 102 | return res; |
| 103 | } |
| 104 | |
| 105 | |
John Koleszar | 79e2b1f | 2010-11-17 09:08:47 -0500 | [diff] [blame] | 106 | #undef ERROR |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 107 | #define ERROR(str) do {\ |
| 108 | ctx->base.err_detail = str;\ |
| 109 | return VPX_CODEC_INVALID_PARAM;\ |
| 110 | } while(0) |
| 111 | |
| 112 | #define RANGE_CHECK(p,memb,lo,hi) do {\ |
Guillermo Ballester Valor | 5a72620 | 2010-06-11 14:33:49 -0400 | [diff] [blame] | 113 | if(!(((p)->memb == lo || (p)->memb > (lo)) && (p)->memb <= hi)) \ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 114 | ERROR(#memb " out of range ["#lo".."#hi"]");\ |
| 115 | } while(0) |
| 116 | |
Guillermo Ballester Valor | 2369068 | 2010-06-11 14:33:49 -0400 | [diff] [blame] | 117 | #define RANGE_CHECK_HI(p,memb,hi) do {\ |
| 118 | if(!((p)->memb <= (hi))) \ |
| 119 | ERROR(#memb " out of range [.."#hi"]");\ |
| 120 | } while(0) |
| 121 | |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 122 | #define RANGE_CHECK_LO(p,memb,lo) do {\ |
| 123 | if(!((p)->memb >= (lo))) \ |
| 124 | ERROR(#memb " out of range ["#lo"..]");\ |
| 125 | } while(0) |
| 126 | |
| 127 | #define RANGE_CHECK_BOOL(p,memb) do {\ |
| 128 | if(!!((p)->memb) != (p)->memb) ERROR(#memb " expected boolean");\ |
| 129 | } while(0) |
| 130 | |
| 131 | static vpx_codec_err_t validate_config(vpx_codec_alg_priv_t *ctx, |
| 132 | const vpx_codec_enc_cfg_t *cfg, |
James Berry | c1c47e8 | 2011-12-07 15:48:00 -0500 | [diff] [blame] | 133 | const struct vp8_extracfg *vp8_cfg, |
| 134 | int finalize) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 135 | { |
Attila Nagy | 1aadced | 2011-04-12 15:01:22 +0300 | [diff] [blame] | 136 | RANGE_CHECK(cfg, g_w, 1, 16383); /* 14 bits available */ |
| 137 | RANGE_CHECK(cfg, g_h, 1, 16383); /* 14 bits available */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 138 | RANGE_CHECK(cfg, g_timebase.den, 1, 1000000000); |
Johnny Klonaris | d02aa04 | 2015-05-31 23:29:16 -0700 | [diff] [blame] | 139 | RANGE_CHECK(cfg, g_timebase.num, 1, 1000000000); |
Guillermo Ballester Valor | 2369068 | 2010-06-11 14:33:49 -0400 | [diff] [blame] | 140 | RANGE_CHECK_HI(cfg, g_profile, 3); |
Guillermo Ballester Valor | 2369068 | 2010-06-11 14:33:49 -0400 | [diff] [blame] | 141 | RANGE_CHECK_HI(cfg, rc_max_quantizer, 63); |
John Koleszar | 2d03f07 | 2011-01-28 11:56:18 -0500 | [diff] [blame] | 142 | RANGE_CHECK_HI(cfg, rc_min_quantizer, cfg->rc_max_quantizer); |
Guillermo Ballester Valor | 2369068 | 2010-06-11 14:33:49 -0400 | [diff] [blame] | 143 | RANGE_CHECK_HI(cfg, g_threads, 64); |
Yunqing Wang | 7f00997 | 2012-07-11 11:43:51 -0700 | [diff] [blame] | 144 | #if CONFIG_REALTIME_ONLY |
Guillermo Ballester Valor | 2369068 | 2010-06-11 14:33:49 -0400 | [diff] [blame] | 145 | RANGE_CHECK_HI(cfg, g_lag_in_frames, 0); |
Yunqing Wang | 7f00997 | 2012-07-11 11:43:51 -0700 | [diff] [blame] | 146 | #elif CONFIG_MULTI_RES_ENCODING |
| 147 | if (ctx->base.enc.total_encoders > 1) |
| 148 | RANGE_CHECK_HI(cfg, g_lag_in_frames, 0); |
| 149 | #else |
| 150 | RANGE_CHECK_HI(cfg, g_lag_in_frames, 25); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 151 | #endif |
Deb Mukherjee | e378a89 | 2013-08-29 16:21:44 -0700 | [diff] [blame] | 152 | RANGE_CHECK(cfg, rc_end_usage, VPX_VBR, VPX_Q); |
John Koleszar | c99f9d7 | 2011-04-11 11:29:23 -0400 | [diff] [blame] | 153 | RANGE_CHECK_HI(cfg, rc_undershoot_pct, 1000); |
| 154 | RANGE_CHECK_HI(cfg, rc_overshoot_pct, 1000); |
Guillermo Ballester Valor | 2369068 | 2010-06-11 14:33:49 -0400 | [diff] [blame] | 155 | RANGE_CHECK_HI(cfg, rc_2pass_vbr_bias_pct, 100); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 156 | RANGE_CHECK(cfg, kf_mode, VPX_KF_DISABLED, VPX_KF_AUTO); |
Yunqing Wang | 7f00997 | 2012-07-11 11:43:51 -0700 | [diff] [blame] | 157 | |
| 158 | /* TODO: add spatial re-sampling support and frame dropping in |
| 159 | * multi-res-encoder.*/ |
| 160 | #if CONFIG_MULTI_RES_ENCODING |
| 161 | if (ctx->base.enc.total_encoders > 1) |
Yunqing Wang | 7f00997 | 2012-07-11 11:43:51 -0700 | [diff] [blame] | 162 | RANGE_CHECK_HI(cfg, rc_resize_allowed, 0); |
Yunqing Wang | 7f00997 | 2012-07-11 11:43:51 -0700 | [diff] [blame] | 163 | #else |
| 164 | RANGE_CHECK_BOOL(cfg, rc_resize_allowed); |
Yunqing Wang | 7f00997 | 2012-07-11 11:43:51 -0700 | [diff] [blame] | 165 | #endif |
Yunqing Wang | 4066c8b | 2012-06-08 11:17:50 -0400 | [diff] [blame] | 166 | RANGE_CHECK_HI(cfg, rc_dropframe_thresh, 100); |
Guillermo Ballester Valor | 2369068 | 2010-06-11 14:33:49 -0400 | [diff] [blame] | 167 | RANGE_CHECK_HI(cfg, rc_resize_up_thresh, 100); |
| 168 | RANGE_CHECK_HI(cfg, rc_resize_down_thresh, 100); |
Yunqing Wang | 7f00997 | 2012-07-11 11:43:51 -0700 | [diff] [blame] | 169 | |
| 170 | #if CONFIG_REALTIME_ONLY |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 171 | RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_ONE_PASS); |
Yunqing Wang | 7f00997 | 2012-07-11 11:43:51 -0700 | [diff] [blame] | 172 | #elif CONFIG_MULTI_RES_ENCODING |
| 173 | if (ctx->base.enc.total_encoders > 1) |
| 174 | RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_ONE_PASS); |
| 175 | #else |
| 176 | RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_LAST_PASS); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 177 | #endif |
| 178 | |
| 179 | /* VP8 does not support a lower bound on the keyframe interval in |
| 180 | * automatic keyframe placement mode. |
| 181 | */ |
| 182 | if (cfg->kf_mode != VPX_KF_DISABLED && cfg->kf_min_dist != cfg->kf_max_dist |
| 183 | && cfg->kf_min_dist > 0) |
| 184 | ERROR("kf_min_dist not supported in auto mode, use 0 " |
| 185 | "or kf_max_dist instead."); |
| 186 | |
| 187 | RANGE_CHECK_BOOL(vp8_cfg, enable_auto_alt_ref); |
Yunqing Wang | 4fd81a9 | 2011-04-11 15:55:04 -0400 | [diff] [blame] | 188 | RANGE_CHECK(vp8_cfg, cpu_used, -16, 16); |
John Koleszar | c8f4c18 | 2012-05-08 11:23:42 -0700 | [diff] [blame] | 189 | |
| 190 | #if CONFIG_REALTIME_ONLY && !CONFIG_TEMPORAL_DENOISING |
| 191 | RANGE_CHECK(vp8_cfg, noise_sensitivity, 0, 0); |
| 192 | #else |
| 193 | RANGE_CHECK_HI(vp8_cfg, noise_sensitivity, 6); |
Stefan Holmer | 9c41143 | 2012-03-06 10:48:18 +0100 | [diff] [blame] | 194 | #endif |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 195 | |
Yunqing Wang | 7f00997 | 2012-07-11 11:43:51 -0700 | [diff] [blame] | 196 | RANGE_CHECK(vp8_cfg, token_partitions, VP8_ONE_TOKENPARTITION, |
| 197 | VP8_EIGHT_TOKENPARTITION); |
Guillermo Ballester Valor | 2369068 | 2010-06-11 14:33:49 -0400 | [diff] [blame] | 198 | RANGE_CHECK_HI(vp8_cfg, Sharpness, 7); |
Adrian Grange | 8ee7284 | 2010-09-30 10:06:09 +0100 | [diff] [blame] | 199 | RANGE_CHECK(vp8_cfg, arnr_max_frames, 0, 15); |
Frank Galligan | 1554272 | 2010-10-04 21:12:22 -0400 | [diff] [blame] | 200 | RANGE_CHECK_HI(vp8_cfg, arnr_strength, 6); |
Adrian Grange | 8ee7284 | 2010-09-30 10:06:09 +0100 | [diff] [blame] | 201 | RANGE_CHECK(vp8_cfg, arnr_type, 1, 3); |
Paul Wilkins | e0846c9 | 2011-01-07 18:29:37 +0000 | [diff] [blame] | 202 | RANGE_CHECK(vp8_cfg, cq_level, 0, 63); |
Marco | 976f7f4 | 2015-04-28 08:29:48 -0700 | [diff] [blame] | 203 | RANGE_CHECK_HI(vp8_cfg, screen_content_mode, 2); |
Deb Mukherjee | e378a89 | 2013-08-29 16:21:44 -0700 | [diff] [blame] | 204 | if (finalize && (cfg->rc_end_usage == VPX_CQ || cfg->rc_end_usage == VPX_Q)) |
James Berry | c1c47e8 | 2011-12-07 15:48:00 -0500 | [diff] [blame] | 205 | RANGE_CHECK(vp8_cfg, cq_level, |
| 206 | cfg->rc_min_quantizer, cfg->rc_max_quantizer); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 207 | |
Attila Nagy | cb791aa | 2011-01-10 11:14:10 +0200 | [diff] [blame] | 208 | #if !(CONFIG_REALTIME_ONLY) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 209 | if (cfg->g_pass == VPX_RC_LAST_PASS) |
| 210 | { |
Adrian Grange | ed40ff9 | 2011-03-10 11:32:48 -0800 | [diff] [blame] | 211 | size_t packet_sz = sizeof(FIRSTPASS_STATS); |
Yaowu Xu | d71ba03 | 2012-08-17 10:05:35 -0700 | [diff] [blame] | 212 | int n_packets = (int)(cfg->rc_twopass_stats_in.sz / |
| 213 | packet_sz); |
John Koleszar | bb7dd5b | 2010-10-14 16:40:12 -0400 | [diff] [blame] | 214 | FIRSTPASS_STATS *stats; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 215 | |
| 216 | if (!cfg->rc_twopass_stats_in.buf) |
| 217 | ERROR("rc_twopass_stats_in.buf not set."); |
| 218 | |
John Koleszar | bb7dd5b | 2010-10-14 16:40:12 -0400 | [diff] [blame] | 219 | if (cfg->rc_twopass_stats_in.sz % packet_sz) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 220 | ERROR("rc_twopass_stats_in.sz indicates truncated packet."); |
| 221 | |
John Koleszar | bb7dd5b | 2010-10-14 16:40:12 -0400 | [diff] [blame] | 222 | if (cfg->rc_twopass_stats_in.sz < 2 * packet_sz) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 223 | ERROR("rc_twopass_stats_in requires at least two packets."); |
| 224 | |
John Koleszar | bb7dd5b | 2010-10-14 16:40:12 -0400 | [diff] [blame] | 225 | stats = (void*)((char *)cfg->rc_twopass_stats_in.buf |
| 226 | + (n_packets - 1) * packet_sz); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 227 | |
John Koleszar | bb7dd5b | 2010-10-14 16:40:12 -0400 | [diff] [blame] | 228 | if ((int)(stats->count + 0.5) != n_packets - 1) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 229 | ERROR("rc_twopass_stats_in missing EOS stats packet"); |
| 230 | } |
Attila Nagy | cb791aa | 2011-01-10 11:14:10 +0200 | [diff] [blame] | 231 | #endif |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 232 | |
Adrian Grange | 217591f | 2011-10-06 15:49:11 -0700 | [diff] [blame] | 233 | RANGE_CHECK(cfg, ts_number_layers, 1, 5); |
| 234 | |
| 235 | if (cfg->ts_number_layers > 1) |
| 236 | { |
Scott Graham | 92963df | 2012-05-08 11:45:35 -0700 | [diff] [blame] | 237 | unsigned int i; |
Adrian Grange | 217591f | 2011-10-06 15:49:11 -0700 | [diff] [blame] | 238 | RANGE_CHECK_HI(cfg, ts_periodicity, 16); |
| 239 | |
| 240 | for (i=1; i<cfg->ts_number_layers; i++) |
Debargha Mukherjee | 1c8567f | 2015-09-04 17:31:51 -0700 | [diff] [blame] | 241 | if (cfg->ts_target_bitrate[i] <= cfg->ts_target_bitrate[i-1] && |
Marco | af898b5 | 2014-11-10 13:07:05 -0800 | [diff] [blame] | 242 | cfg->rc_target_bitrate > 0) |
Adrian Grange | 217591f | 2011-10-06 15:49:11 -0700 | [diff] [blame] | 243 | ERROR("ts_target_bitrate entries are not strictly increasing"); |
| 244 | |
| 245 | RANGE_CHECK(cfg, ts_rate_decimator[cfg->ts_number_layers-1], 1, 1); |
| 246 | for (i=cfg->ts_number_layers-2; i>0; i--) |
| 247 | if (cfg->ts_rate_decimator[i-1] != 2*cfg->ts_rate_decimator[i]) |
| 248 | ERROR("ts_rate_decimator factors are not powers of 2"); |
| 249 | |
| 250 | RANGE_CHECK_HI(cfg, ts_layer_id[i], cfg->ts_number_layers-1); |
| 251 | } |
| 252 | |
Attila Nagy | 52cf4dc | 2012-02-09 12:37:03 +0200 | [diff] [blame] | 253 | #if (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING) |
| 254 | if(cfg->g_threads > (1 << vp8_cfg->token_partitions)) |
| 255 | ERROR("g_threads cannot be bigger than number of token partitions"); |
| 256 | #endif |
| 257 | |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 258 | return VPX_CODEC_OK; |
| 259 | } |
| 260 | |
| 261 | |
| 262 | static vpx_codec_err_t validate_img(vpx_codec_alg_priv_t *ctx, |
| 263 | const vpx_image_t *img) |
| 264 | { |
| 265 | switch (img->fmt) |
| 266 | { |
James Zern | 6cd4a10 | 2010-05-20 23:22:39 -0400 | [diff] [blame] | 267 | case VPX_IMG_FMT_YV12: |
| 268 | case VPX_IMG_FMT_I420: |
| 269 | case VPX_IMG_FMT_VPXI420: |
| 270 | case VPX_IMG_FMT_VPXYV12: |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 271 | break; |
| 272 | default: |
| 273 | ERROR("Invalid image format. Only YV12 and I420 images are supported"); |
| 274 | } |
| 275 | |
| 276 | if ((img->d_w != ctx->cfg.g_w) || (img->d_h != ctx->cfg.g_h)) |
| 277 | ERROR("Image size must match encoder init configuration size"); |
| 278 | |
| 279 | return VPX_CODEC_OK; |
| 280 | } |
| 281 | |
| 282 | |
| 283 | static vpx_codec_err_t set_vp8e_config(VP8_CONFIG *oxcf, |
| 284 | vpx_codec_enc_cfg_t cfg, |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 285 | struct vp8_extracfg vp8_cfg, |
| 286 | vpx_codec_priv_enc_mr_cfg_t *mr_cfg) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 287 | { |
| 288 | oxcf->multi_threaded = cfg.g_threads; |
| 289 | oxcf->Version = cfg.g_profile; |
| 290 | |
| 291 | oxcf->Width = cfg.g_w; |
| 292 | oxcf->Height = cfg.g_h; |
John Koleszar | bdd35c1 | 2011-11-11 10:47:20 -0800 | [diff] [blame] | 293 | oxcf->timebase = cfg.g_timebase; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 294 | |
Adrian Grange | 217591f | 2011-10-06 15:49:11 -0700 | [diff] [blame] | 295 | oxcf->error_resilient_mode = cfg.g_error_resilient; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 296 | |
| 297 | switch (cfg.g_pass) |
| 298 | { |
| 299 | case VPX_RC_ONE_PASS: |
| 300 | oxcf->Mode = MODE_BESTQUALITY; |
| 301 | break; |
| 302 | case VPX_RC_FIRST_PASS: |
| 303 | oxcf->Mode = MODE_FIRSTPASS; |
| 304 | break; |
| 305 | case VPX_RC_LAST_PASS: |
| 306 | oxcf->Mode = MODE_SECONDPASS_BEST; |
| 307 | break; |
| 308 | } |
| 309 | |
James Zern | 7b0b6a2 | 2012-06-13 11:59:12 -0700 | [diff] [blame] | 310 | if (cfg.g_pass == VPX_RC_FIRST_PASS || cfg.g_pass == VPX_RC_ONE_PASS) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 311 | { |
Adrian Grange | 217591f | 2011-10-06 15:49:11 -0700 | [diff] [blame] | 312 | oxcf->allow_lag = 0; |
| 313 | oxcf->lag_in_frames = 0; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 314 | } |
| 315 | else |
| 316 | { |
Adrian Grange | 217591f | 2011-10-06 15:49:11 -0700 | [diff] [blame] | 317 | oxcf->allow_lag = (cfg.g_lag_in_frames) > 0; |
| 318 | oxcf->lag_in_frames = cfg.g_lag_in_frames; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | oxcf->allow_df = (cfg.rc_dropframe_thresh > 0); |
| 322 | oxcf->drop_frames_water_mark = cfg.rc_dropframe_thresh; |
| 323 | |
| 324 | oxcf->allow_spatial_resampling = cfg.rc_resize_allowed; |
| 325 | oxcf->resample_up_water_mark = cfg.rc_resize_up_thresh; |
| 326 | oxcf->resample_down_water_mark = cfg.rc_resize_down_thresh; |
| 327 | |
Deb Mukherjee | e378a89 | 2013-08-29 16:21:44 -0700 | [diff] [blame] | 328 | if (cfg.rc_end_usage == VPX_VBR) { |
| 329 | oxcf->end_usage = USAGE_LOCAL_FILE_PLAYBACK; |
| 330 | } else if (cfg.rc_end_usage == VPX_CBR) { |
| 331 | oxcf->end_usage = USAGE_STREAM_FROM_SERVER; |
| 332 | } else if (cfg.rc_end_usage == VPX_CQ) { |
| 333 | oxcf->end_usage = USAGE_CONSTRAINED_QUALITY; |
| 334 | } else if (cfg.rc_end_usage == VPX_Q) { |
| 335 | oxcf->end_usage = USAGE_CONSTANT_QUALITY; |
Paul Wilkins | e0846c9 | 2011-01-07 18:29:37 +0000 | [diff] [blame] | 336 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 337 | |
Adrian Grange | 217591f | 2011-10-06 15:49:11 -0700 | [diff] [blame] | 338 | oxcf->target_bandwidth = cfg.rc_target_bitrate; |
John Koleszar | 1654ae9 | 2011-07-28 09:17:32 -0400 | [diff] [blame] | 339 | oxcf->rc_max_intra_bitrate_pct = vp8_cfg.rc_max_intra_bitrate_pct; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 340 | |
Adrian Grange | 217591f | 2011-10-06 15:49:11 -0700 | [diff] [blame] | 341 | oxcf->best_allowed_q = cfg.rc_min_quantizer; |
| 342 | oxcf->worst_allowed_q = cfg.rc_max_quantizer; |
| 343 | oxcf->cq_level = vp8_cfg.cq_level; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 344 | oxcf->fixed_q = -1; |
| 345 | |
Adrian Grange | 217591f | 2011-10-06 15:49:11 -0700 | [diff] [blame] | 346 | oxcf->under_shoot_pct = cfg.rc_undershoot_pct; |
| 347 | oxcf->over_shoot_pct = cfg.rc_overshoot_pct; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 348 | |
Adrian Grange | e479379 | 2012-01-13 14:09:40 -0800 | [diff] [blame] | 349 | oxcf->maximum_buffer_size_in_ms = cfg.rc_buf_sz; |
| 350 | oxcf->starting_buffer_level_in_ms = cfg.rc_buf_initial_sz; |
| 351 | oxcf->optimal_buffer_level_in_ms = cfg.rc_buf_optimal_sz; |
| 352 | |
Adrian Grange | 217591f | 2011-10-06 15:49:11 -0700 | [diff] [blame] | 353 | oxcf->maximum_buffer_size = cfg.rc_buf_sz; |
| 354 | oxcf->starting_buffer_level = cfg.rc_buf_initial_sz; |
| 355 | oxcf->optimal_buffer_level = cfg.rc_buf_optimal_sz; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 356 | |
Adrian Grange | 217591f | 2011-10-06 15:49:11 -0700 | [diff] [blame] | 357 | oxcf->two_pass_vbrbias = cfg.rc_2pass_vbr_bias_pct; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 358 | oxcf->two_pass_vbrmin_section = cfg.rc_2pass_vbr_minsection_pct; |
| 359 | oxcf->two_pass_vbrmax_section = cfg.rc_2pass_vbr_maxsection_pct; |
| 360 | |
Adrian Grange | 217591f | 2011-10-06 15:49:11 -0700 | [diff] [blame] | 361 | oxcf->auto_key = cfg.kf_mode == VPX_KF_AUTO |
| 362 | && cfg.kf_min_dist != cfg.kf_max_dist; |
Adrian Grange | 217591f | 2011-10-06 15:49:11 -0700 | [diff] [blame] | 363 | oxcf->key_freq = cfg.kf_max_dist; |
| 364 | |
| 365 | oxcf->number_of_layers = cfg.ts_number_layers; |
| 366 | oxcf->periodicity = cfg.ts_periodicity; |
| 367 | |
| 368 | if (oxcf->number_of_layers > 1) |
| 369 | { |
| 370 | memcpy (oxcf->target_bitrate, cfg.ts_target_bitrate, |
James Zern | f274c21 | 2015-04-23 20:42:19 -0700 | [diff] [blame] | 371 | sizeof(cfg.ts_target_bitrate)); |
Adrian Grange | 217591f | 2011-10-06 15:49:11 -0700 | [diff] [blame] | 372 | memcpy (oxcf->rate_decimator, cfg.ts_rate_decimator, |
James Zern | f274c21 | 2015-04-23 20:42:19 -0700 | [diff] [blame] | 373 | sizeof(cfg.ts_rate_decimator)); |
Adrian Grange | 217591f | 2011-10-06 15:49:11 -0700 | [diff] [blame] | 374 | memcpy (oxcf->layer_id, cfg.ts_layer_id, sizeof(cfg.ts_layer_id)); |
| 375 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 376 | |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 377 | #if CONFIG_MULTI_RES_ENCODING |
Yunqing Wang | c647ec4 | 2011-12-16 16:50:29 -0500 | [diff] [blame] | 378 | /* When mr_cfg is NULL, oxcf->mr_total_resolutions and oxcf->mr_encoder_id |
| 379 | * are both memset to 0, which ensures the correct logic under this |
| 380 | * situation. |
| 381 | */ |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 382 | if(mr_cfg) |
| 383 | { |
| 384 | oxcf->mr_total_resolutions = mr_cfg->mr_total_resolutions; |
| 385 | oxcf->mr_encoder_id = mr_cfg->mr_encoder_id; |
| 386 | oxcf->mr_down_sampling_factor.num = mr_cfg->mr_down_sampling_factor.num; |
| 387 | oxcf->mr_down_sampling_factor.den = mr_cfg->mr_down_sampling_factor.den; |
| 388 | oxcf->mr_low_res_mode_info = mr_cfg->mr_low_res_mode_info; |
| 389 | } |
Johann | 80b344d | 2014-12-16 12:22:10 -0800 | [diff] [blame] | 390 | #else |
| 391 | (void)mr_cfg; |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 392 | #endif |
| 393 | |
Adrian Grange | 217591f | 2011-10-06 15:49:11 -0700 | [diff] [blame] | 394 | oxcf->cpu_used = vp8_cfg.cpu_used; |
| 395 | oxcf->encode_breakout = vp8_cfg.static_thresh; |
| 396 | oxcf->play_alternate = vp8_cfg.enable_auto_alt_ref; |
| 397 | oxcf->noise_sensitivity = vp8_cfg.noise_sensitivity; |
| 398 | oxcf->Sharpness = vp8_cfg.Sharpness; |
| 399 | oxcf->token_partitions = vp8_cfg.token_partitions; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 400 | |
Adrian Grange | 217591f | 2011-10-06 15:49:11 -0700 | [diff] [blame] | 401 | oxcf->two_pass_stats_in = cfg.rc_twopass_stats_in; |
| 402 | oxcf->output_pkt_list = vp8_cfg.pkt_list; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 403 | |
Adrian Grange | 217591f | 2011-10-06 15:49:11 -0700 | [diff] [blame] | 404 | oxcf->arnr_max_frames = vp8_cfg.arnr_max_frames; |
| 405 | oxcf->arnr_strength = vp8_cfg.arnr_strength; |
| 406 | oxcf->arnr_type = vp8_cfg.arnr_type; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 407 | |
Adrian Grange | 217591f | 2011-10-06 15:49:11 -0700 | [diff] [blame] | 408 | oxcf->tuning = vp8_cfg.tuning; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 409 | |
Marco | af898b5 | 2014-11-10 13:07:05 -0800 | [diff] [blame] | 410 | oxcf->screen_content_mode = vp8_cfg.screen_content_mode; |
| 411 | |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 412 | /* |
| 413 | printf("Current VP8 Settings: \n"); |
| 414 | printf("target_bandwidth: %d\n", oxcf->target_bandwidth); |
| 415 | printf("noise_sensitivity: %d\n", oxcf->noise_sensitivity); |
| 416 | printf("Sharpness: %d\n", oxcf->Sharpness); |
| 417 | printf("cpu_used: %d\n", oxcf->cpu_used); |
| 418 | printf("Mode: %d\n", oxcf->Mode); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 419 | printf("auto_key: %d\n", oxcf->auto_key); |
| 420 | printf("key_freq: %d\n", oxcf->key_freq); |
| 421 | printf("end_usage: %d\n", oxcf->end_usage); |
| 422 | printf("under_shoot_pct: %d\n", oxcf->under_shoot_pct); |
John Koleszar | c99f9d7 | 2011-04-11 11:29:23 -0400 | [diff] [blame] | 423 | printf("over_shoot_pct: %d\n", oxcf->over_shoot_pct); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 424 | printf("starting_buffer_level: %d\n", oxcf->starting_buffer_level); |
| 425 | printf("optimal_buffer_level: %d\n", oxcf->optimal_buffer_level); |
| 426 | printf("maximum_buffer_size: %d\n", oxcf->maximum_buffer_size); |
| 427 | printf("fixed_q: %d\n", oxcf->fixed_q); |
| 428 | printf("worst_allowed_q: %d\n", oxcf->worst_allowed_q); |
| 429 | printf("best_allowed_q: %d\n", oxcf->best_allowed_q); |
| 430 | printf("allow_spatial_resampling: %d\n", oxcf->allow_spatial_resampling); |
| 431 | printf("resample_down_water_mark: %d\n", oxcf->resample_down_water_mark); |
| 432 | printf("resample_up_water_mark: %d\n", oxcf->resample_up_water_mark); |
| 433 | printf("allow_df: %d\n", oxcf->allow_df); |
| 434 | printf("drop_frames_water_mark: %d\n", oxcf->drop_frames_water_mark); |
| 435 | printf("two_pass_vbrbias: %d\n", oxcf->two_pass_vbrbias); |
| 436 | printf("two_pass_vbrmin_section: %d\n", oxcf->two_pass_vbrmin_section); |
| 437 | printf("two_pass_vbrmax_section: %d\n", oxcf->two_pass_vbrmax_section); |
| 438 | printf("allow_lag: %d\n", oxcf->allow_lag); |
| 439 | printf("lag_in_frames: %d\n", oxcf->lag_in_frames); |
| 440 | printf("play_alternate: %d\n", oxcf->play_alternate); |
| 441 | printf("Version: %d\n", oxcf->Version); |
| 442 | printf("multi_threaded: %d\n", oxcf->multi_threaded); |
| 443 | printf("encode_breakout: %d\n", oxcf->encode_breakout); |
| 444 | */ |
| 445 | return VPX_CODEC_OK; |
| 446 | } |
| 447 | |
| 448 | static vpx_codec_err_t vp8e_set_config(vpx_codec_alg_priv_t *ctx, |
| 449 | const vpx_codec_enc_cfg_t *cfg) |
| 450 | { |
| 451 | vpx_codec_err_t res; |
| 452 | |
Alex Converse | 581731a | 2015-01-16 16:02:05 -0800 | [diff] [blame] | 453 | if (cfg->g_w != ctx->cfg.g_w || cfg->g_h != ctx->cfg.g_h) |
| 454 | { |
| 455 | if (cfg->g_lag_in_frames > 1 || cfg->g_pass != VPX_RC_ONE_PASS) |
| 456 | ERROR("Cannot change width or height after initialization"); |
| 457 | if ((ctx->cpi->initial_width && (int)cfg->g_w > ctx->cpi->initial_width) || |
| 458 | (ctx->cpi->initial_height && (int)cfg->g_h > ctx->cpi->initial_height)) |
Yaowu Xu | 47c55ac | 2015-07-29 16:23:14 -0700 | [diff] [blame] | 459 | ERROR("Cannot increase width or height larger than their initial values"); |
Alex Converse | 581731a | 2015-01-16 16:02:05 -0800 | [diff] [blame] | 460 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 461 | |
| 462 | /* Prevent increasing lag_in_frames. This check is stricter than it needs |
| 463 | * to be -- the limit is not increasing past the first lag_in_frames |
| 464 | * value, but we don't track the initial config, only the last successful |
| 465 | * config. |
| 466 | */ |
| 467 | if ((cfg->g_lag_in_frames > ctx->cfg.g_lag_in_frames)) |
| 468 | ERROR("Cannot increase lag_in_frames"); |
| 469 | |
James Berry | c1c47e8 | 2011-12-07 15:48:00 -0500 | [diff] [blame] | 470 | res = validate_config(ctx, cfg, &ctx->vp8_cfg, 0); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 471 | |
| 472 | if (!res) |
| 473 | { |
| 474 | ctx->cfg = *cfg; |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 475 | set_vp8e_config(&ctx->oxcf, ctx->cfg, ctx->vp8_cfg, NULL); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 476 | vp8_change_config(ctx->cpi, &ctx->oxcf); |
| 477 | } |
| 478 | |
| 479 | return res; |
| 480 | } |
| 481 | |
Dmitry Kovalev | 8a8b662 | 2014-05-27 16:45:58 -0700 | [diff] [blame] | 482 | static vpx_codec_err_t get_quantizer(vpx_codec_alg_priv_t *ctx, va_list args) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 483 | { |
Dmitry Kovalev | 8a8b662 | 2014-05-27 16:45:58 -0700 | [diff] [blame] | 484 | int *const arg = va_arg(args, int *); |
| 485 | if (arg == NULL) |
| 486 | return VPX_CODEC_INVALID_PARAM; |
| 487 | *arg = vp8_get_quantizer(ctx->cpi); |
| 488 | return VPX_CODEC_OK; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 489 | } |
| 490 | |
Dmitry Kovalev | 8a8b662 | 2014-05-27 16:45:58 -0700 | [diff] [blame] | 491 | static vpx_codec_err_t get_quantizer64(vpx_codec_alg_priv_t *ctx, va_list args) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 492 | { |
Dmitry Kovalev | 8a8b662 | 2014-05-27 16:45:58 -0700 | [diff] [blame] | 493 | int *const arg = va_arg(args, int *); |
| 494 | if (arg == NULL) |
| 495 | return VPX_CODEC_INVALID_PARAM; |
| 496 | *arg = vp8_reverse_trans(vp8_get_quantizer(ctx->cpi)); |
| 497 | return VPX_CODEC_OK; |
| 498 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 499 | |
Dmitry Kovalev | 8a8b662 | 2014-05-27 16:45:58 -0700 | [diff] [blame] | 500 | static vpx_codec_err_t update_extracfg(vpx_codec_alg_priv_t *ctx, |
| 501 | const struct vp8_extracfg *extra_cfg) |
| 502 | { |
| 503 | const vpx_codec_err_t res = validate_config(ctx, &ctx->cfg, extra_cfg, 0); |
| 504 | if (res == VPX_CODEC_OK) { |
| 505 | ctx->vp8_cfg = *extra_cfg; |
| 506 | set_vp8e_config(&ctx->oxcf, ctx->cfg, ctx->vp8_cfg, NULL); |
| 507 | vp8_change_config(ctx->cpi, &ctx->oxcf); |
| 508 | } |
| 509 | return res; |
| 510 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 511 | |
Dmitry Kovalev | 8a8b662 | 2014-05-27 16:45:58 -0700 | [diff] [blame] | 512 | static vpx_codec_err_t set_cpu_used(vpx_codec_alg_priv_t *ctx, va_list args) |
| 513 | { |
| 514 | struct vp8_extracfg extra_cfg = ctx->vp8_cfg; |
| 515 | extra_cfg.cpu_used = CAST(VP8E_SET_CPUUSED, args); |
| 516 | return update_extracfg(ctx, &extra_cfg); |
| 517 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 518 | |
Dmitry Kovalev | 8a8b662 | 2014-05-27 16:45:58 -0700 | [diff] [blame] | 519 | static vpx_codec_err_t set_enable_auto_alt_ref(vpx_codec_alg_priv_t *ctx, |
| 520 | va_list args) |
| 521 | { |
| 522 | struct vp8_extracfg extra_cfg = ctx->vp8_cfg; |
| 523 | extra_cfg.enable_auto_alt_ref = CAST(VP8E_SET_ENABLEAUTOALTREF, args); |
| 524 | return update_extracfg(ctx, &extra_cfg); |
| 525 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 526 | |
Dmitry Kovalev | 8a8b662 | 2014-05-27 16:45:58 -0700 | [diff] [blame] | 527 | static vpx_codec_err_t set_noise_sensitivity(vpx_codec_alg_priv_t *ctx, |
| 528 | va_list args) |
| 529 | { |
| 530 | struct vp8_extracfg extra_cfg = ctx->vp8_cfg; |
| 531 | extra_cfg.noise_sensitivity = CAST(VP8E_SET_NOISE_SENSITIVITY, args); |
| 532 | return update_extracfg(ctx, &extra_cfg); |
| 533 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 534 | |
Dmitry Kovalev | 8a8b662 | 2014-05-27 16:45:58 -0700 | [diff] [blame] | 535 | static vpx_codec_err_t set_sharpness(vpx_codec_alg_priv_t *ctx, va_list args) |
| 536 | { |
| 537 | struct vp8_extracfg extra_cfg = ctx->vp8_cfg; |
| 538 | extra_cfg.Sharpness = CAST(VP8E_SET_SHARPNESS, args); |
| 539 | return update_extracfg(ctx, &extra_cfg); |
| 540 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 541 | |
Dmitry Kovalev | 8a8b662 | 2014-05-27 16:45:58 -0700 | [diff] [blame] | 542 | static vpx_codec_err_t set_static_thresh(vpx_codec_alg_priv_t *ctx, |
| 543 | va_list args) |
| 544 | { |
| 545 | struct vp8_extracfg extra_cfg = ctx->vp8_cfg; |
| 546 | extra_cfg.static_thresh = CAST(VP8E_SET_STATIC_THRESHOLD, args); |
| 547 | return update_extracfg(ctx, &extra_cfg); |
| 548 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 549 | |
Dmitry Kovalev | 8a8b662 | 2014-05-27 16:45:58 -0700 | [diff] [blame] | 550 | static vpx_codec_err_t set_token_partitions(vpx_codec_alg_priv_t *ctx, |
| 551 | va_list args) |
| 552 | { |
| 553 | struct vp8_extracfg extra_cfg = ctx->vp8_cfg; |
| 554 | extra_cfg.token_partitions = CAST(VP8E_SET_TOKEN_PARTITIONS, args); |
| 555 | return update_extracfg(ctx, &extra_cfg); |
| 556 | } |
| 557 | |
| 558 | static vpx_codec_err_t set_arnr_max_frames(vpx_codec_alg_priv_t *ctx, |
| 559 | va_list args) |
| 560 | { |
| 561 | struct vp8_extracfg extra_cfg = ctx->vp8_cfg; |
| 562 | extra_cfg.arnr_max_frames = CAST(VP8E_SET_ARNR_MAXFRAMES, args); |
| 563 | return update_extracfg(ctx, &extra_cfg); |
| 564 | } |
| 565 | |
| 566 | static vpx_codec_err_t set_arnr_strength(vpx_codec_alg_priv_t *ctx, |
| 567 | va_list args) |
| 568 | { |
| 569 | struct vp8_extracfg extra_cfg = ctx->vp8_cfg; |
| 570 | extra_cfg.arnr_strength = CAST(VP8E_SET_ARNR_STRENGTH, args); |
| 571 | return update_extracfg(ctx, &extra_cfg); |
| 572 | } |
| 573 | |
| 574 | static vpx_codec_err_t set_arnr_type(vpx_codec_alg_priv_t *ctx, va_list args) |
| 575 | { |
| 576 | struct vp8_extracfg extra_cfg = ctx->vp8_cfg; |
| 577 | extra_cfg.arnr_type = CAST(VP8E_SET_ARNR_TYPE, args); |
| 578 | return update_extracfg(ctx, &extra_cfg); |
| 579 | } |
| 580 | |
| 581 | static vpx_codec_err_t set_tuning(vpx_codec_alg_priv_t *ctx, va_list args) |
| 582 | { |
| 583 | struct vp8_extracfg extra_cfg = ctx->vp8_cfg; |
| 584 | extra_cfg.tuning = CAST(VP8E_SET_TUNING, args); |
| 585 | return update_extracfg(ctx, &extra_cfg); |
| 586 | } |
| 587 | |
| 588 | static vpx_codec_err_t set_cq_level(vpx_codec_alg_priv_t *ctx, va_list args) |
| 589 | { |
| 590 | struct vp8_extracfg extra_cfg = ctx->vp8_cfg; |
| 591 | extra_cfg.cq_level = CAST(VP8E_SET_CQ_LEVEL, args); |
| 592 | return update_extracfg(ctx, &extra_cfg); |
| 593 | } |
| 594 | |
| 595 | static vpx_codec_err_t set_rc_max_intra_bitrate_pct(vpx_codec_alg_priv_t *ctx, |
| 596 | va_list args) |
| 597 | { |
| 598 | struct vp8_extracfg extra_cfg = ctx->vp8_cfg; |
| 599 | extra_cfg.rc_max_intra_bitrate_pct = |
| 600 | CAST(VP8E_SET_MAX_INTRA_BITRATE_PCT, args); |
| 601 | return update_extracfg(ctx, &extra_cfg); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 602 | } |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 603 | |
Marco | af898b5 | 2014-11-10 13:07:05 -0800 | [diff] [blame] | 604 | static vpx_codec_err_t set_screen_content_mode(vpx_codec_alg_priv_t *ctx, |
| 605 | va_list args) |
| 606 | { |
| 607 | struct vp8_extracfg extra_cfg = ctx->vp8_cfg; |
| 608 | extra_cfg.screen_content_mode = |
| 609 | CAST(VP8E_SET_SCREEN_CONTENT_MODE, args); |
| 610 | return update_extracfg(ctx, &extra_cfg); |
| 611 | } |
| 612 | |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 613 | static vpx_codec_err_t vp8e_mr_alloc_mem(const vpx_codec_enc_cfg_t *cfg, |
| 614 | void **mem_loc) |
| 615 | { |
Yunqing Wang | ad479a9 | 2012-05-23 13:40:24 -0400 | [diff] [blame] | 616 | vpx_codec_err_t res = 0; |
| 617 | |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 618 | #if CONFIG_MULTI_RES_ENCODING |
Yunqing Wang | 65dd157 | 2012-05-16 15:06:42 -0400 | [diff] [blame] | 619 | LOWER_RES_FRAME_INFO *shared_mem_loc; |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 620 | int mb_rows = ((cfg->g_w + 15) >>4); |
| 621 | int mb_cols = ((cfg->g_h + 15) >>4); |
| 622 | |
Yunqing Wang | 65dd157 | 2012-05-16 15:06:42 -0400 | [diff] [blame] | 623 | shared_mem_loc = calloc(1, sizeof(LOWER_RES_FRAME_INFO)); |
| 624 | if(!shared_mem_loc) |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 625 | { |
Yunqing Wang | ad479a9 | 2012-05-23 13:40:24 -0400 | [diff] [blame] | 626 | res = VPX_CODEC_MEM_ERROR; |
Yunqing Wang | 65dd157 | 2012-05-16 15:06:42 -0400 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | shared_mem_loc->mb_info = calloc(mb_rows*mb_cols, sizeof(LOWER_RES_MB_INFO)); |
| 630 | if(!(shared_mem_loc->mb_info)) |
| 631 | { |
Yunqing Wang | ad479a9 | 2012-05-23 13:40:24 -0400 | [diff] [blame] | 632 | res = VPX_CODEC_MEM_ERROR; |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 633 | } |
| 634 | else |
Yunqing Wang | 65dd157 | 2012-05-16 15:06:42 -0400 | [diff] [blame] | 635 | { |
| 636 | *mem_loc = (void *)shared_mem_loc; |
Yunqing Wang | ad479a9 | 2012-05-23 13:40:24 -0400 | [diff] [blame] | 637 | res = VPX_CODEC_OK; |
Yunqing Wang | 65dd157 | 2012-05-16 15:06:42 -0400 | [diff] [blame] | 638 | } |
Johann | 80b344d | 2014-12-16 12:22:10 -0800 | [diff] [blame] | 639 | #else |
| 640 | (void)cfg; |
| 641 | (void)mem_loc; |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 642 | #endif |
Yunqing Wang | ad479a9 | 2012-05-23 13:40:24 -0400 | [diff] [blame] | 643 | return res; |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | static vpx_codec_err_t vp8e_init(vpx_codec_ctx_t *ctx, |
| 647 | vpx_codec_priv_enc_mr_cfg_t *mr_cfg) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 648 | { |
John Koleszar | 2bf8fb5 | 2012-05-02 16:37:37 -0700 | [diff] [blame] | 649 | vpx_codec_err_t res = VPX_CODEC_OK; |
Dmitry Kovalev | b08fab8 | 2014-09-02 18:26:53 -0700 | [diff] [blame] | 650 | |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 651 | |
John Koleszar | a9c7597 | 2012-11-08 17:09:30 -0800 | [diff] [blame] | 652 | vp8_rtcd(); |
Johann | d5d9289 | 2015-04-17 16:11:38 -0400 | [diff] [blame] | 653 | vpx_dsp_rtcd(); |
Johann | 14ef4ae | 2015-04-15 09:27:00 -0400 | [diff] [blame] | 654 | vpx_scale_rtcd(); |
John Koleszar | 8df79e9 | 2012-06-15 15:40:13 -0700 | [diff] [blame] | 655 | |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 656 | if (!ctx->priv) |
| 657 | { |
Dmitry Kovalev | b08fab8 | 2014-09-02 18:26:53 -0700 | [diff] [blame] | 658 | struct vpx_codec_alg_priv *priv = |
| 659 | (struct vpx_codec_alg_priv *)vpx_calloc(1, sizeof(*priv)); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 660 | |
Attila Nagy | e6db21e | 2011-02-22 15:02:05 +0200 | [diff] [blame] | 661 | if (!priv) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 662 | { |
Attila Nagy | e6db21e | 2011-02-22 15:02:05 +0200 | [diff] [blame] | 663 | return VPX_CODEC_MEM_ERROR; |
| 664 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 665 | |
Dmitry Kovalev | 73edeb0 | 2014-08-20 17:02:10 -0700 | [diff] [blame] | 666 | ctx->priv = (vpx_codec_priv_t *)priv; |
Attila Nagy | e6db21e | 2011-02-22 15:02:05 +0200 | [diff] [blame] | 667 | ctx->priv->init_flags = ctx->init_flags; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 668 | |
Attila Nagy | e6db21e | 2011-02-22 15:02:05 +0200 | [diff] [blame] | 669 | if (ctx->config.enc) |
| 670 | { |
| 671 | /* Update the reference to the config structure to an |
| 672 | * internal copy. |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 673 | */ |
Dmitry Kovalev | 73edeb0 | 2014-08-20 17:02:10 -0700 | [diff] [blame] | 674 | priv->cfg = *ctx->config.enc; |
| 675 | ctx->config.enc = &priv->cfg; |
Attila Nagy | e6db21e | 2011-02-22 15:02:05 +0200 | [diff] [blame] | 676 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 677 | |
Dmitry Kovalev | 48274c6 | 2014-08-21 11:21:18 -0700 | [diff] [blame] | 678 | priv->vp8_cfg = default_extracfg; |
Attila Nagy | e6db21e | 2011-02-22 15:02:05 +0200 | [diff] [blame] | 679 | priv->vp8_cfg.pkt_list = &priv->pkt_list.head; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 680 | |
Attila Nagy | e6db21e | 2011-02-22 15:02:05 +0200 | [diff] [blame] | 681 | priv->cx_data_sz = priv->cfg.g_w * priv->cfg.g_h * 3 / 2 * 2; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 682 | |
James Berry | bac6c22 | 2011-10-24 11:50:27 -0400 | [diff] [blame] | 683 | if (priv->cx_data_sz < 32768) priv->cx_data_sz = 32768; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 684 | |
Attila Nagy | e6db21e | 2011-02-22 15:02:05 +0200 | [diff] [blame] | 685 | priv->cx_data = malloc(priv->cx_data_sz); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 686 | |
Attila Nagy | e6db21e | 2011-02-22 15:02:05 +0200 | [diff] [blame] | 687 | if (!priv->cx_data) |
| 688 | { |
| 689 | return VPX_CODEC_MEM_ERROR; |
| 690 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 691 | |
Yunqing Wang | 7f00997 | 2012-07-11 11:43:51 -0700 | [diff] [blame] | 692 | if(mr_cfg) |
| 693 | ctx->priv->enc.total_encoders = mr_cfg->mr_total_resolutions; |
| 694 | else |
| 695 | ctx->priv->enc.total_encoders = 1; |
| 696 | |
Ronald S. Bultje | 54d4895 | 2015-09-30 11:07:35 -0400 | [diff] [blame] | 697 | once(vp8_initialize_enc); |
| 698 | |
James Berry | c1c47e8 | 2011-12-07 15:48:00 -0500 | [diff] [blame] | 699 | res = validate_config(priv, &priv->cfg, &priv->vp8_cfg, 0); |
Attila Nagy | e6db21e | 2011-02-22 15:02:05 +0200 | [diff] [blame] | 700 | |
| 701 | if (!res) |
| 702 | { |
Dmitry Kovalev | 73edeb0 | 2014-08-20 17:02:10 -0700 | [diff] [blame] | 703 | set_vp8e_config(&priv->oxcf, priv->cfg, priv->vp8_cfg, mr_cfg); |
| 704 | priv->cpi = vp8_create_compressor(&priv->oxcf); |
| 705 | if (!priv->cpi) |
Attila Nagy | e6db21e | 2011-02-22 15:02:05 +0200 | [diff] [blame] | 706 | res = VPX_CODEC_MEM_ERROR; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 707 | } |
| 708 | } |
| 709 | |
| 710 | return res; |
| 711 | } |
| 712 | |
| 713 | static vpx_codec_err_t vp8e_destroy(vpx_codec_alg_priv_t *ctx) |
| 714 | { |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 715 | #if CONFIG_MULTI_RES_ENCODING |
| 716 | /* Free multi-encoder shared memory */ |
| 717 | if (ctx->oxcf.mr_total_resolutions > 0 && (ctx->oxcf.mr_encoder_id == ctx->oxcf.mr_total_resolutions-1)) |
Yunqing Wang | 65dd157 | 2012-05-16 15:06:42 -0400 | [diff] [blame] | 718 | { |
| 719 | LOWER_RES_FRAME_INFO *shared_mem_loc = (LOWER_RES_FRAME_INFO *)ctx->oxcf.mr_low_res_mode_info; |
| 720 | free(shared_mem_loc->mb_info); |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 721 | free(ctx->oxcf.mr_low_res_mode_info); |
Yunqing Wang | 65dd157 | 2012-05-16 15:06:42 -0400 | [diff] [blame] | 722 | } |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 723 | #endif |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 724 | |
| 725 | free(ctx->cx_data); |
| 726 | vp8_remove_compressor(&ctx->cpi); |
Dmitry Kovalev | b08fab8 | 2014-09-02 18:26:53 -0700 | [diff] [blame] | 727 | vpx_free(ctx); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 728 | return VPX_CODEC_OK; |
| 729 | } |
| 730 | |
| 731 | static vpx_codec_err_t image2yuvconfig(const vpx_image_t *img, |
| 732 | YV12_BUFFER_CONFIG *yv12) |
| 733 | { |
Johann | 69dc876 | 2014-08-15 10:50:40 -0700 | [diff] [blame] | 734 | const int y_w = img->d_w; |
| 735 | const int y_h = img->d_h; |
| 736 | const int uv_w = (img->d_w + 1) / 2; |
| 737 | const int uv_h = (img->d_h + 1) / 2; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 738 | vpx_codec_err_t res = VPX_CODEC_OK; |
John Koleszar | b6c7191 | 2010-05-24 21:45:05 -0400 | [diff] [blame] | 739 | yv12->y_buffer = img->planes[VPX_PLANE_Y]; |
| 740 | yv12->u_buffer = img->planes[VPX_PLANE_U]; |
| 741 | yv12->v_buffer = img->planes[VPX_PLANE_V]; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 742 | |
Johann | 69dc876 | 2014-08-15 10:50:40 -0700 | [diff] [blame] | 743 | yv12->y_crop_width = y_w; |
| 744 | yv12->y_crop_height = y_h; |
| 745 | yv12->y_width = y_w; |
| 746 | yv12->y_height = y_h; |
| 747 | yv12->uv_crop_width = uv_w; |
| 748 | yv12->uv_crop_height = uv_h; |
| 749 | yv12->uv_width = uv_w; |
| 750 | yv12->uv_height = uv_h; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 751 | |
John Koleszar | b6c7191 | 2010-05-24 21:45:05 -0400 | [diff] [blame] | 752 | yv12->y_stride = img->stride[VPX_PLANE_Y]; |
| 753 | yv12->uv_stride = img->stride[VPX_PLANE_U]; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 754 | |
John Koleszar | b6c7191 | 2010-05-24 21:45:05 -0400 | [diff] [blame] | 755 | yv12->border = (img->stride[VPX_PLANE_Y] - img->w) / 2; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 756 | return res; |
| 757 | } |
| 758 | |
| 759 | static void pick_quickcompress_mode(vpx_codec_alg_priv_t *ctx, |
| 760 | unsigned long duration, |
| 761 | unsigned long deadline) |
| 762 | { |
| 763 | unsigned int new_qc; |
| 764 | |
| 765 | #if !(CONFIG_REALTIME_ONLY) |
| 766 | /* Use best quality mode if no deadline is given. */ |
| 767 | new_qc = MODE_BESTQUALITY; |
| 768 | |
| 769 | if (deadline) |
| 770 | { |
| 771 | uint64_t duration_us; |
| 772 | |
| 773 | /* Convert duration parameter from stream timebase to microseconds */ |
| 774 | duration_us = (uint64_t)duration * 1000000 |
| 775 | * (uint64_t)ctx->cfg.g_timebase.num |
| 776 | / (uint64_t)ctx->cfg.g_timebase.den; |
| 777 | |
| 778 | /* If the deadline is more that the duration this frame is to be shown, |
| 779 | * use good quality mode. Otherwise use realtime mode. |
| 780 | */ |
| 781 | new_qc = (deadline > duration_us) ? MODE_GOODQUALITY : MODE_REALTIME; |
| 782 | } |
| 783 | |
| 784 | #else |
| 785 | new_qc = MODE_REALTIME; |
| 786 | #endif |
| 787 | |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 788 | if (ctx->cfg.g_pass == VPX_RC_FIRST_PASS) |
| 789 | new_qc = MODE_FIRSTPASS; |
| 790 | else if (ctx->cfg.g_pass == VPX_RC_LAST_PASS) |
| 791 | new_qc = (new_qc == MODE_BESTQUALITY) |
| 792 | ? MODE_SECONDPASS_BEST |
| 793 | : MODE_SECONDPASS; |
| 794 | |
| 795 | if (ctx->oxcf.Mode != new_qc) |
| 796 | { |
| 797 | ctx->oxcf.Mode = new_qc; |
| 798 | vp8_change_config(ctx->cpi, &ctx->oxcf); |
| 799 | } |
| 800 | } |
| 801 | |
Marco | af898b5 | 2014-11-10 13:07:05 -0800 | [diff] [blame] | 802 | static vpx_codec_err_t set_reference_and_update(vpx_codec_alg_priv_t *ctx, |
| 803 | int flags) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 804 | { |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 805 | |
| 806 | /* Handle Flags */ |
| 807 | if (((flags & VP8_EFLAG_NO_UPD_GF) && (flags & VP8_EFLAG_FORCE_GF)) |
| 808 | || ((flags & VP8_EFLAG_NO_UPD_ARF) && (flags & VP8_EFLAG_FORCE_ARF))) |
| 809 | { |
| 810 | ctx->base.err_detail = "Conflicting flags."; |
| 811 | return VPX_CODEC_INVALID_PARAM; |
| 812 | } |
| 813 | |
| 814 | if (flags & (VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_GF |
| 815 | | VP8_EFLAG_NO_REF_ARF)) |
| 816 | { |
| 817 | int ref = 7; |
| 818 | |
| 819 | if (flags & VP8_EFLAG_NO_REF_LAST) |
Johann | 965d868 | 2012-05-23 16:08:37 -0700 | [diff] [blame] | 820 | ref ^= VP8_LAST_FRAME; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 821 | |
| 822 | if (flags & VP8_EFLAG_NO_REF_GF) |
Johann | 965d868 | 2012-05-23 16:08:37 -0700 | [diff] [blame] | 823 | ref ^= VP8_GOLD_FRAME; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 824 | |
| 825 | if (flags & VP8_EFLAG_NO_REF_ARF) |
Johann | 965d868 | 2012-05-23 16:08:37 -0700 | [diff] [blame] | 826 | ref ^= VP8_ALTR_FRAME; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 827 | |
| 828 | vp8_use_as_reference(ctx->cpi, ref); |
| 829 | } |
| 830 | |
| 831 | if (flags & (VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF |
| 832 | | VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_FORCE_GF |
| 833 | | VP8_EFLAG_FORCE_ARF)) |
| 834 | { |
| 835 | int upd = 7; |
| 836 | |
| 837 | if (flags & VP8_EFLAG_NO_UPD_LAST) |
Johann | 965d868 | 2012-05-23 16:08:37 -0700 | [diff] [blame] | 838 | upd ^= VP8_LAST_FRAME; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 839 | |
| 840 | if (flags & VP8_EFLAG_NO_UPD_GF) |
Johann | 965d868 | 2012-05-23 16:08:37 -0700 | [diff] [blame] | 841 | upd ^= VP8_GOLD_FRAME; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 842 | |
| 843 | if (flags & VP8_EFLAG_NO_UPD_ARF) |
Johann | 965d868 | 2012-05-23 16:08:37 -0700 | [diff] [blame] | 844 | upd ^= VP8_ALTR_FRAME; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 845 | |
| 846 | vp8_update_reference(ctx->cpi, upd); |
| 847 | } |
| 848 | |
| 849 | if (flags & VP8_EFLAG_NO_UPD_ENTROPY) |
| 850 | { |
| 851 | vp8_update_entropy(ctx->cpi, 0); |
| 852 | } |
| 853 | |
Marco | af898b5 | 2014-11-10 13:07:05 -0800 | [diff] [blame] | 854 | return VPX_CODEC_OK; |
| 855 | } |
| 856 | |
| 857 | static vpx_codec_err_t vp8e_encode(vpx_codec_alg_priv_t *ctx, |
| 858 | const vpx_image_t *img, |
| 859 | vpx_codec_pts_t pts, |
| 860 | unsigned long duration, |
| 861 | vpx_enc_frame_flags_t flags, |
| 862 | unsigned long deadline) |
| 863 | { |
| 864 | vpx_codec_err_t res = VPX_CODEC_OK; |
| 865 | |
| 866 | if (!ctx->cfg.rc_target_bitrate) |
| 867 | return res; |
| 868 | |
Marco | af898b5 | 2014-11-10 13:07:05 -0800 | [diff] [blame] | 869 | if (img) |
| 870 | res = validate_img(ctx, img); |
| 871 | |
| 872 | if (!res) |
| 873 | res = validate_config(ctx, &ctx->cfg, &ctx->vp8_cfg, 1); |
| 874 | |
| 875 | pick_quickcompress_mode(ctx, duration, deadline); |
| 876 | vpx_codec_pkt_list_init(&ctx->pkt_list); |
| 877 | |
| 878 | // If no flags are set in the encode call, then use the frame flags as |
| 879 | // defined via the control function: vp8e_set_frame_flags. |
| 880 | if (!flags) { |
| 881 | flags = ctx->control_frame_flags; |
| 882 | } |
| 883 | ctx->control_frame_flags = 0; |
| 884 | |
James Zern | c052309 | 2015-08-24 20:07:48 -0700 | [diff] [blame] | 885 | if (!res) |
| 886 | res = set_reference_and_update(ctx, flags); |
Marco | af898b5 | 2014-11-10 13:07:05 -0800 | [diff] [blame] | 887 | |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 888 | /* Handle fixed keyframe intervals */ |
| 889 | if (ctx->cfg.kf_mode == VPX_KF_AUTO |
| 890 | && ctx->cfg.kf_min_dist == ctx->cfg.kf_max_dist) |
| 891 | { |
| 892 | if (++ctx->fixed_kf_cntr > ctx->cfg.kf_min_dist) |
| 893 | { |
| 894 | flags |= VPX_EFLAG_FORCE_KF; |
Andoni Morales Alastruey | 4814016 | 2011-02-07 18:04:02 +0100 | [diff] [blame] | 895 | ctx->fixed_kf_cntr = 1; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 896 | } |
| 897 | } |
| 898 | |
| 899 | /* Initialize the encoder instance on the first frame*/ |
| 900 | if (!res && ctx->cpi) |
| 901 | { |
| 902 | unsigned int lib_flags; |
| 903 | YV12_BUFFER_CONFIG sd; |
James Zern | b45065d | 2011-07-25 18:44:59 -0700 | [diff] [blame] | 904 | int64_t dst_time_stamp, dst_end_time_stamp; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 905 | unsigned long size, cx_data_sz; |
| 906 | unsigned char *cx_data; |
James Berry | bc71511 | 2011-10-12 11:18:50 -0400 | [diff] [blame] | 907 | unsigned char *cx_data_end; |
| 908 | int comp_data_state = 0; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 909 | |
| 910 | /* Set up internal flags */ |
| 911 | if (ctx->base.init_flags & VPX_CODEC_USE_PSNR) |
| 912 | ((VP8_COMP *)ctx->cpi)->b_calculate_psnr = 1; |
| 913 | |
Stefan Holmer | 7296b3f | 2011-06-13 16:42:27 +0200 | [diff] [blame] | 914 | if (ctx->base.init_flags & VPX_CODEC_USE_OUTPUT_PARTITION) |
| 915 | ((VP8_COMP *)ctx->cpi)->output_partition = 1; |
| 916 | |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 917 | /* Convert API flags to internal codec lib flags */ |
| 918 | lib_flags = (flags & VPX_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0; |
| 919 | |
| 920 | /* vp8 use 10,000,000 ticks/second as time stamp */ |
| 921 | dst_time_stamp = pts * 10000000 * ctx->cfg.g_timebase.num / ctx->cfg.g_timebase.den; |
| 922 | dst_end_time_stamp = (pts + duration) * 10000000 * ctx->cfg.g_timebase.num / ctx->cfg.g_timebase.den; |
| 923 | |
| 924 | if (img != NULL) |
| 925 | { |
| 926 | res = image2yuvconfig(img, &sd); |
| 927 | |
| 928 | if (vp8_receive_raw_frame(ctx->cpi, ctx->next_frame_flag | lib_flags, |
| 929 | &sd, dst_time_stamp, dst_end_time_stamp)) |
| 930 | { |
| 931 | VP8_COMP *cpi = (VP8_COMP *)ctx->cpi; |
| 932 | res = update_error_state(ctx, &cpi->common.error); |
| 933 | } |
| 934 | |
| 935 | /* reset for next frame */ |
| 936 | ctx->next_frame_flag = 0; |
| 937 | } |
| 938 | |
| 939 | cx_data = ctx->cx_data; |
| 940 | cx_data_sz = ctx->cx_data_sz; |
James Berry | bc71511 | 2011-10-12 11:18:50 -0400 | [diff] [blame] | 941 | cx_data_end = ctx->cx_data + cx_data_sz; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 942 | lib_flags = 0; |
| 943 | |
James Berry | bc71511 | 2011-10-12 11:18:50 -0400 | [diff] [blame] | 944 | while (cx_data_sz >= ctx->cx_data_sz / 2) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 945 | { |
James Berry | bc71511 | 2011-10-12 11:18:50 -0400 | [diff] [blame] | 946 | comp_data_state = vp8_get_compressed_data(ctx->cpi, |
| 947 | &lib_flags, |
| 948 | &size, |
| 949 | cx_data, |
| 950 | cx_data_end, |
| 951 | &dst_time_stamp, |
| 952 | &dst_end_time_stamp, |
| 953 | !img); |
| 954 | |
| 955 | if(comp_data_state == VPX_CODEC_CORRUPT_FRAME) |
| 956 | return VPX_CODEC_CORRUPT_FRAME; |
| 957 | else if(comp_data_state == -1) |
| 958 | break; |
| 959 | |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 960 | if (size) |
| 961 | { |
| 962 | vpx_codec_pts_t round, delta; |
| 963 | vpx_codec_cx_pkt_t pkt; |
| 964 | VP8_COMP *cpi = (VP8_COMP *)ctx->cpi; |
| 965 | |
| 966 | /* Add the frame packet to the list of returned packets. */ |
Dmitry Kovalev | 947748e | 2014-04-25 17:39:48 -0700 | [diff] [blame] | 967 | round = (vpx_codec_pts_t)10000000 |
James Zern | 429743c | 2012-08-07 17:12:10 -0700 | [diff] [blame] | 968 | * ctx->cfg.g_timebase.num / 2 - 1; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 969 | delta = (dst_end_time_stamp - dst_time_stamp); |
| 970 | pkt.kind = VPX_CODEC_CX_FRAME_PKT; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 971 | pkt.data.frame.pts = |
| 972 | (dst_time_stamp * ctx->cfg.g_timebase.den + round) |
| 973 | / ctx->cfg.g_timebase.num / 10000000; |
Yaowu Xu | d71ba03 | 2012-08-17 10:05:35 -0700 | [diff] [blame] | 974 | pkt.data.frame.duration = (unsigned long) |
| 975 | ((delta * ctx->cfg.g_timebase.den + round) |
| 976 | / ctx->cfg.g_timebase.num / 10000000); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 977 | pkt.data.frame.flags = lib_flags << 16; |
| 978 | |
| 979 | if (lib_flags & FRAMEFLAGS_KEY) |
| 980 | pkt.data.frame.flags |= VPX_FRAME_IS_KEY; |
| 981 | |
| 982 | if (!cpi->common.show_frame) |
| 983 | { |
| 984 | pkt.data.frame.flags |= VPX_FRAME_IS_INVISIBLE; |
| 985 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 986 | /* This timestamp should be as close as possible to the |
| 987 | * prior PTS so that if a decoder uses pts to schedule when |
| 988 | * to do this, we start right after last frame was decoded. |
| 989 | * Invisible frames have no duration. |
| 990 | */ |
Frank Galligan | 45e6494 | 2010-10-05 17:46:37 -0400 | [diff] [blame] | 991 | pkt.data.frame.pts = ((cpi->last_time_stamp_seen |
| 992 | * ctx->cfg.g_timebase.den + round) |
| 993 | / ctx->cfg.g_timebase.num / 10000000) + 1; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 994 | pkt.data.frame.duration = 0; |
| 995 | } |
| 996 | |
John Koleszar | 37de0b8 | 2011-07-07 10:38:23 -0400 | [diff] [blame] | 997 | if (cpi->droppable) |
| 998 | pkt.data.frame.flags |= VPX_FRAME_IS_DROPPABLE; |
| 999 | |
Stefan Holmer | 7296b3f | 2011-06-13 16:42:27 +0200 | [diff] [blame] | 1000 | if (cpi->output_partition) |
| 1001 | { |
| 1002 | int i; |
| 1003 | const int num_partitions = |
| 1004 | (1 << cpi->common.multi_token_partition) + 1; |
Attila Nagy | 0afcc76 | 2011-07-20 14:09:42 +0300 | [diff] [blame] | 1005 | |
| 1006 | pkt.data.frame.flags |= VPX_FRAME_IS_FRAGMENT; |
| 1007 | |
Stefan Holmer | 7296b3f | 2011-06-13 16:42:27 +0200 | [diff] [blame] | 1008 | for (i = 0; i < num_partitions; ++i) |
| 1009 | { |
Attila Nagy | 52cf4dc | 2012-02-09 12:37:03 +0200 | [diff] [blame] | 1010 | #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING |
| 1011 | pkt.data.frame.buf = cpi->partition_d[i]; |
| 1012 | #else |
Stefan Holmer | 7296b3f | 2011-06-13 16:42:27 +0200 | [diff] [blame] | 1013 | pkt.data.frame.buf = cx_data; |
Attila Nagy | 52cf4dc | 2012-02-09 12:37:03 +0200 | [diff] [blame] | 1014 | cx_data += cpi->partition_sz[i]; |
| 1015 | cx_data_sz -= cpi->partition_sz[i]; |
| 1016 | #endif |
Stefan Holmer | 7296b3f | 2011-06-13 16:42:27 +0200 | [diff] [blame] | 1017 | pkt.data.frame.sz = cpi->partition_sz[i]; |
| 1018 | pkt.data.frame.partition_id = i; |
| 1019 | /* don't set the fragment bit for the last partition */ |
Attila Nagy | 0afcc76 | 2011-07-20 14:09:42 +0300 | [diff] [blame] | 1020 | if (i == (num_partitions - 1)) |
| 1021 | pkt.data.frame.flags &= ~VPX_FRAME_IS_FRAGMENT; |
Stefan Holmer | 7296b3f | 2011-06-13 16:42:27 +0200 | [diff] [blame] | 1022 | vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt); |
Stefan Holmer | 7296b3f | 2011-06-13 16:42:27 +0200 | [diff] [blame] | 1023 | } |
Attila Nagy | 52cf4dc | 2012-02-09 12:37:03 +0200 | [diff] [blame] | 1024 | #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING |
| 1025 | /* In lagged mode the encoder can buffer multiple frames. |
| 1026 | * We don't want this in partitioned output because |
| 1027 | * partitions are spread all over the output buffer. |
| 1028 | * So, force an exit! |
| 1029 | */ |
| 1030 | cx_data_sz -= ctx->cx_data_sz / 2; |
| 1031 | #endif |
Stefan Holmer | 7296b3f | 2011-06-13 16:42:27 +0200 | [diff] [blame] | 1032 | } |
| 1033 | else |
| 1034 | { |
| 1035 | pkt.data.frame.buf = cx_data; |
| 1036 | pkt.data.frame.sz = size; |
| 1037 | pkt.data.frame.partition_id = -1; |
| 1038 | vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt); |
| 1039 | cx_data += size; |
| 1040 | cx_data_sz -= size; |
| 1041 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1042 | } |
| 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | return res; |
| 1047 | } |
| 1048 | |
| 1049 | |
| 1050 | static const vpx_codec_cx_pkt_t *vp8e_get_cxdata(vpx_codec_alg_priv_t *ctx, |
| 1051 | vpx_codec_iter_t *iter) |
| 1052 | { |
| 1053 | return vpx_codec_pkt_list_get(&ctx->pkt_list.head, iter); |
| 1054 | } |
| 1055 | |
| 1056 | static vpx_codec_err_t vp8e_set_reference(vpx_codec_alg_priv_t *ctx, |
Dmitry Kovalev | 8a8b662 | 2014-05-27 16:45:58 -0700 | [diff] [blame] | 1057 | va_list args) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1058 | { |
| 1059 | vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *); |
| 1060 | |
| 1061 | if (data) |
| 1062 | { |
| 1063 | vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data; |
| 1064 | YV12_BUFFER_CONFIG sd; |
| 1065 | |
| 1066 | image2yuvconfig(&frame->img, &sd); |
| 1067 | vp8_set_reference(ctx->cpi, frame->frame_type, &sd); |
| 1068 | return VPX_CODEC_OK; |
| 1069 | } |
| 1070 | else |
| 1071 | return VPX_CODEC_INVALID_PARAM; |
| 1072 | |
| 1073 | } |
| 1074 | |
| 1075 | static vpx_codec_err_t vp8e_get_reference(vpx_codec_alg_priv_t *ctx, |
Dmitry Kovalev | 8a8b662 | 2014-05-27 16:45:58 -0700 | [diff] [blame] | 1076 | va_list args) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1077 | { |
| 1078 | |
| 1079 | vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *); |
| 1080 | |
| 1081 | if (data) |
| 1082 | { |
| 1083 | vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data; |
| 1084 | YV12_BUFFER_CONFIG sd; |
| 1085 | |
| 1086 | image2yuvconfig(&frame->img, &sd); |
| 1087 | vp8_get_reference(ctx->cpi, frame->frame_type, &sd); |
| 1088 | return VPX_CODEC_OK; |
| 1089 | } |
| 1090 | else |
| 1091 | return VPX_CODEC_INVALID_PARAM; |
| 1092 | } |
| 1093 | |
| 1094 | static vpx_codec_err_t vp8e_set_previewpp(vpx_codec_alg_priv_t *ctx, |
Dmitry Kovalev | 8a8b662 | 2014-05-27 16:45:58 -0700 | [diff] [blame] | 1095 | va_list args) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1096 | { |
James Zern | 76640f8 | 2010-08-20 16:06:56 -0400 | [diff] [blame] | 1097 | #if CONFIG_POSTPROC |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1098 | vp8_postproc_cfg_t *data = va_arg(args, vp8_postproc_cfg_t *); |
| 1099 | |
| 1100 | if (data) |
| 1101 | { |
| 1102 | ctx->preview_ppcfg = *((vp8_postproc_cfg_t *)data); |
| 1103 | return VPX_CODEC_OK; |
| 1104 | } |
| 1105 | else |
| 1106 | return VPX_CODEC_INVALID_PARAM; |
James Zern | 76640f8 | 2010-08-20 16:06:56 -0400 | [diff] [blame] | 1107 | #else |
| 1108 | (void)ctx; |
James Zern | 76640f8 | 2010-08-20 16:06:56 -0400 | [diff] [blame] | 1109 | (void)args; |
| 1110 | return VPX_CODEC_INCAPABLE; |
| 1111 | #endif |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | |
| 1115 | static vpx_image_t *vp8e_get_preview(vpx_codec_alg_priv_t *ctx) |
| 1116 | { |
| 1117 | |
| 1118 | YV12_BUFFER_CONFIG sd; |
Fritz Koenig | 647df00 | 2010-11-04 16:03:36 -0700 | [diff] [blame] | 1119 | vp8_ppflags_t flags = {0}; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1120 | |
Fritz Koenig | 647df00 | 2010-11-04 16:03:36 -0700 | [diff] [blame] | 1121 | if (ctx->preview_ppcfg.post_proc_flag) |
| 1122 | { |
| 1123 | flags.post_proc_flag = ctx->preview_ppcfg.post_proc_flag; |
| 1124 | flags.deblocking_level = ctx->preview_ppcfg.deblocking_level; |
| 1125 | flags.noise_level = ctx->preview_ppcfg.noise_level; |
| 1126 | } |
| 1127 | |
| 1128 | if (0 == vp8_get_preview_raw_frame(ctx->cpi, &sd, &flags)) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1129 | { |
| 1130 | |
| 1131 | /* |
James Zern | 6cd4a10 | 2010-05-20 23:22:39 -0400 | [diff] [blame] | 1132 | vpx_img_wrap(&ctx->preview_img, VPX_IMG_FMT_YV12, |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1133 | sd.y_width + 2*VP8BORDERINPIXELS, |
| 1134 | sd.y_height + 2*VP8BORDERINPIXELS, |
| 1135 | 1, |
| 1136 | sd.buffer_alloc); |
| 1137 | vpx_img_set_rect(&ctx->preview_img, |
| 1138 | VP8BORDERINPIXELS, VP8BORDERINPIXELS, |
| 1139 | sd.y_width, sd.y_height); |
| 1140 | */ |
| 1141 | |
| 1142 | ctx->preview_img.bps = 12; |
John Koleszar | b6c7191 | 2010-05-24 21:45:05 -0400 | [diff] [blame] | 1143 | ctx->preview_img.planes[VPX_PLANE_Y] = sd.y_buffer; |
| 1144 | ctx->preview_img.planes[VPX_PLANE_U] = sd.u_buffer; |
| 1145 | ctx->preview_img.planes[VPX_PLANE_V] = sd.v_buffer; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1146 | |
James Zern | 4fc6c88 | 2013-07-12 14:11:53 -0700 | [diff] [blame] | 1147 | ctx->preview_img.fmt = VPX_IMG_FMT_I420; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1148 | ctx->preview_img.x_chroma_shift = 1; |
| 1149 | ctx->preview_img.y_chroma_shift = 1; |
| 1150 | |
James Berry | ddacf1c | 2011-02-08 14:23:18 -0500 | [diff] [blame] | 1151 | ctx->preview_img.d_w = sd.y_width; |
| 1152 | ctx->preview_img.d_h = sd.y_height; |
John Koleszar | b6c7191 | 2010-05-24 21:45:05 -0400 | [diff] [blame] | 1153 | ctx->preview_img.stride[VPX_PLANE_Y] = sd.y_stride; |
| 1154 | ctx->preview_img.stride[VPX_PLANE_U] = sd.uv_stride; |
| 1155 | ctx->preview_img.stride[VPX_PLANE_V] = sd.uv_stride; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1156 | ctx->preview_img.w = sd.y_width; |
| 1157 | ctx->preview_img.h = sd.y_height; |
| 1158 | |
| 1159 | return &ctx->preview_img; |
| 1160 | } |
| 1161 | else |
| 1162 | return NULL; |
| 1163 | } |
| 1164 | |
Marco | af898b5 | 2014-11-10 13:07:05 -0800 | [diff] [blame] | 1165 | static vpx_codec_err_t vp8e_set_frame_flags(vpx_codec_alg_priv_t *ctx, |
| 1166 | va_list args) |
| 1167 | { |
| 1168 | int frame_flags = va_arg(args, int); |
| 1169 | ctx->control_frame_flags = frame_flags; |
| 1170 | return set_reference_and_update(ctx, frame_flags); |
| 1171 | } |
| 1172 | |
| 1173 | static vpx_codec_err_t vp8e_set_temporal_layer_id(vpx_codec_alg_priv_t *ctx, |
| 1174 | va_list args) |
| 1175 | { |
| 1176 | int layer_id = va_arg(args, int); |
| 1177 | if (layer_id < 0 || layer_id >= (int)ctx->cfg.ts_number_layers) { |
| 1178 | return VPX_CODEC_INVALID_PARAM; |
| 1179 | } |
| 1180 | ctx->cpi->temporal_layer_id = layer_id; |
| 1181 | return VPX_CODEC_OK; |
| 1182 | } |
| 1183 | |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1184 | static vpx_codec_err_t vp8e_set_roi_map(vpx_codec_alg_priv_t *ctx, |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1185 | va_list args) |
| 1186 | { |
| 1187 | vpx_roi_map_t *data = va_arg(args, vpx_roi_map_t *); |
| 1188 | |
| 1189 | if (data) |
| 1190 | { |
| 1191 | vpx_roi_map_t *roi = (vpx_roi_map_t *)data; |
| 1192 | |
| 1193 | if (!vp8_set_roimap(ctx->cpi, roi->roi_map, roi->rows, roi->cols, roi->delta_q, roi->delta_lf, roi->static_threshold)) |
| 1194 | return VPX_CODEC_OK; |
| 1195 | else |
| 1196 | return VPX_CODEC_INVALID_PARAM; |
| 1197 | } |
| 1198 | else |
| 1199 | return VPX_CODEC_INVALID_PARAM; |
| 1200 | } |
| 1201 | |
| 1202 | |
| 1203 | static vpx_codec_err_t vp8e_set_activemap(vpx_codec_alg_priv_t *ctx, |
Dmitry Kovalev | 8a8b662 | 2014-05-27 16:45:58 -0700 | [diff] [blame] | 1204 | va_list args) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1205 | { |
| 1206 | vpx_active_map_t *data = va_arg(args, vpx_active_map_t *); |
| 1207 | |
| 1208 | if (data) |
| 1209 | { |
| 1210 | |
| 1211 | vpx_active_map_t *map = (vpx_active_map_t *)data; |
| 1212 | |
| 1213 | if (!vp8_set_active_map(ctx->cpi, map->active_map, map->rows, map->cols)) |
| 1214 | return VPX_CODEC_OK; |
| 1215 | else |
| 1216 | return VPX_CODEC_INVALID_PARAM; |
| 1217 | } |
| 1218 | else |
| 1219 | return VPX_CODEC_INVALID_PARAM; |
| 1220 | } |
| 1221 | |
| 1222 | static vpx_codec_err_t vp8e_set_scalemode(vpx_codec_alg_priv_t *ctx, |
Dmitry Kovalev | 8a8b662 | 2014-05-27 16:45:58 -0700 | [diff] [blame] | 1223 | va_list args) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1224 | { |
| 1225 | |
| 1226 | vpx_scaling_mode_t *data = va_arg(args, vpx_scaling_mode_t *); |
| 1227 | |
| 1228 | if (data) |
| 1229 | { |
| 1230 | int res; |
| 1231 | vpx_scaling_mode_t scalemode = *(vpx_scaling_mode_t *)data ; |
Frank Galligan | bc45f23 | 2013-01-11 15:34:05 -0800 | [diff] [blame] | 1232 | res = vp8_set_internal_size(ctx->cpi, |
| 1233 | (VPX_SCALING)scalemode.h_scaling_mode, |
| 1234 | (VPX_SCALING)scalemode.v_scaling_mode); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1235 | |
| 1236 | if (!res) |
| 1237 | { |
| 1238 | /*force next frame a key frame to effect scaling mode */ |
| 1239 | ctx->next_frame_flag |= FRAMEFLAGS_KEY; |
| 1240 | return VPX_CODEC_OK; |
| 1241 | } |
| 1242 | else |
| 1243 | return VPX_CODEC_INVALID_PARAM; |
| 1244 | } |
| 1245 | else |
| 1246 | return VPX_CODEC_INVALID_PARAM; |
| 1247 | } |
| 1248 | |
| 1249 | |
| 1250 | static vpx_codec_ctrl_fn_map_t vp8e_ctf_maps[] = |
| 1251 | { |
| 1252 | {VP8_SET_REFERENCE, vp8e_set_reference}, |
| 1253 | {VP8_COPY_REFERENCE, vp8e_get_reference}, |
| 1254 | {VP8_SET_POSTPROC, vp8e_set_previewpp}, |
Marco | af898b5 | 2014-11-10 13:07:05 -0800 | [diff] [blame] | 1255 | {VP8E_SET_FRAME_FLAGS, vp8e_set_frame_flags}, |
| 1256 | {VP8E_SET_TEMPORAL_LAYER_ID, vp8e_set_temporal_layer_id}, |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1257 | {VP8E_SET_ROI_MAP, vp8e_set_roi_map}, |
| 1258 | {VP8E_SET_ACTIVEMAP, vp8e_set_activemap}, |
| 1259 | {VP8E_SET_SCALEMODE, vp8e_set_scalemode}, |
Dmitry Kovalev | 8a8b662 | 2014-05-27 16:45:58 -0700 | [diff] [blame] | 1260 | {VP8E_SET_CPUUSED, set_cpu_used}, |
| 1261 | {VP8E_SET_NOISE_SENSITIVITY, set_noise_sensitivity}, |
| 1262 | {VP8E_SET_ENABLEAUTOALTREF, set_enable_auto_alt_ref}, |
| 1263 | {VP8E_SET_SHARPNESS, set_sharpness}, |
| 1264 | {VP8E_SET_STATIC_THRESHOLD, set_static_thresh}, |
| 1265 | {VP8E_SET_TOKEN_PARTITIONS, set_token_partitions}, |
| 1266 | {VP8E_GET_LAST_QUANTIZER, get_quantizer}, |
| 1267 | {VP8E_GET_LAST_QUANTIZER_64, get_quantizer64}, |
| 1268 | {VP8E_SET_ARNR_MAXFRAMES, set_arnr_max_frames}, |
| 1269 | {VP8E_SET_ARNR_STRENGTH , set_arnr_strength}, |
| 1270 | {VP8E_SET_ARNR_TYPE , set_arnr_type}, |
| 1271 | {VP8E_SET_TUNING, set_tuning}, |
| 1272 | {VP8E_SET_CQ_LEVEL, set_cq_level}, |
| 1273 | {VP8E_SET_MAX_INTRA_BITRATE_PCT, set_rc_max_intra_bitrate_pct}, |
Marco | af898b5 | 2014-11-10 13:07:05 -0800 | [diff] [blame] | 1274 | {VP8E_SET_SCREEN_CONTENT_MODE, set_screen_content_mode}, |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1275 | { -1, NULL}, |
| 1276 | }; |
| 1277 | |
| 1278 | static vpx_codec_enc_cfg_map_t vp8e_usage_cfg_map[] = |
| 1279 | { |
| 1280 | { |
| 1281 | 0, |
| 1282 | { |
| 1283 | 0, /* g_usage */ |
| 1284 | 0, /* g_threads */ |
| 1285 | 0, /* g_profile */ |
| 1286 | |
| 1287 | 320, /* g_width */ |
| 1288 | 240, /* g_height */ |
Deb Mukherjee | 5acfafb | 2014-08-26 12:35:15 -0700 | [diff] [blame] | 1289 | VPX_BITS_8, /* g_bit_depth */ |
| 1290 | 8, /* g_input_bit_depth */ |
| 1291 | |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1292 | {1, 30}, /* g_timebase */ |
| 1293 | |
| 1294 | 0, /* g_error_resilient */ |
| 1295 | |
| 1296 | VPX_RC_ONE_PASS, /* g_pass */ |
| 1297 | |
| 1298 | 0, /* g_lag_in_frames */ |
| 1299 | |
John Koleszar | 2321621 | 2010-09-02 09:32:03 -0400 | [diff] [blame] | 1300 | 0, /* rc_dropframe_thresh */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1301 | 0, /* rc_resize_allowed */ |
Adrian Grange | f7bd127 | 2014-04-09 14:51:29 -0700 | [diff] [blame] | 1302 | 1, /* rc_scaled_width */ |
| 1303 | 1, /* rc_scaled_height */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1304 | 60, /* rc_resize_down_thresold */ |
| 1305 | 30, /* rc_resize_up_thresold */ |
| 1306 | |
| 1307 | VPX_VBR, /* rc_end_usage */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1308 | {0}, /* rc_twopass_stats_in */ |
Pengchong Jin | f349b07 | 2014-07-14 09:13:38 -0700 | [diff] [blame] | 1309 | {0}, /* rc_firstpass_mb_stats_in */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1310 | 256, /* rc_target_bandwidth */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1311 | 4, /* rc_min_quantizer */ |
| 1312 | 63, /* rc_max_quantizer */ |
John Koleszar | c99f9d7 | 2011-04-11 11:29:23 -0400 | [diff] [blame] | 1313 | 100, /* rc_undershoot_pct */ |
| 1314 | 100, /* rc_overshoot_pct */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1315 | |
| 1316 | 6000, /* rc_max_buffer_size */ |
| 1317 | 4000, /* rc_buffer_initial_size; */ |
| 1318 | 5000, /* rc_buffer_optimal_size; */ |
| 1319 | |
| 1320 | 50, /* rc_two_pass_vbrbias */ |
| 1321 | 0, /* rc_two_pass_vbrmin_section */ |
| 1322 | 400, /* rc_two_pass_vbrmax_section */ |
| 1323 | |
| 1324 | /* keyframing settings (kf) */ |
| 1325 | VPX_KF_AUTO, /* g_kfmode*/ |
| 1326 | 0, /* kf_min_dist */ |
Ralph Giles | 2a0d7b1 | 2012-01-05 10:39:38 -0600 | [diff] [blame] | 1327 | 128, /* kf_max_dist */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1328 | |
Ivan Maltz | 01b35c3 | 2013-09-05 08:55:47 -0700 | [diff] [blame] | 1329 | VPX_SS_DEFAULT_LAYERS, /* ss_number_layers */ |
Minghai Shang | e899859 | 2014-07-14 11:24:17 -0700 | [diff] [blame] | 1330 | {0}, |
Minghai Shang | 8c196b2 | 2014-02-26 13:30:50 -0800 | [diff] [blame] | 1331 | {0}, /* ss_target_bitrate */ |
Adrian Grange | 217591f | 2011-10-06 15:49:11 -0700 | [diff] [blame] | 1332 | 1, /* ts_number_layers */ |
| 1333 | {0}, /* ts_target_bitrate */ |
| 1334 | {0}, /* ts_rate_decimator */ |
| 1335 | 0, /* ts_periodicity */ |
| 1336 | {0}, /* ts_layer_id */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1337 | }}, |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1338 | }; |
| 1339 | |
| 1340 | |
| 1341 | #ifndef VERSION_STRING |
| 1342 | #define VERSION_STRING |
| 1343 | #endif |
John Koleszar | fa7a55b | 2010-09-21 10:35:52 -0400 | [diff] [blame] | 1344 | CODEC_INTERFACE(vpx_codec_vp8_cx) = |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1345 | { |
John Koleszar | 317a666 | 2010-06-10 12:08:01 -0400 | [diff] [blame] | 1346 | "WebM Project VP8 Encoder" VERSION_STRING, |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1347 | VPX_CODEC_INTERNAL_ABI_VERSION, |
Stefan Holmer | 7296b3f | 2011-06-13 16:42:27 +0200 | [diff] [blame] | 1348 | VPX_CODEC_CAP_ENCODER | VPX_CODEC_CAP_PSNR | |
| 1349 | VPX_CODEC_CAP_OUTPUT_PARTITION, |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1350 | /* vpx_codec_caps_t caps; */ |
| 1351 | vp8e_init, /* vpx_codec_init_fn_t init; */ |
| 1352 | vp8e_destroy, /* vpx_codec_destroy_fn_t destroy; */ |
| 1353 | vp8e_ctf_maps, /* vpx_codec_ctrl_fn_map_t *ctrl_maps; */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1354 | { |
Dmitry Kovalev | cd6d984 | 2014-08-20 17:16:28 -0700 | [diff] [blame] | 1355 | NULL, /* vpx_codec_peek_si_fn_t peek_si; */ |
| 1356 | NULL, /* vpx_codec_get_si_fn_t get_si; */ |
| 1357 | NULL, /* vpx_codec_decode_fn_t decode; */ |
| 1358 | NULL, /* vpx_codec_frame_get_fn_t frame_get; */ |
Adrian Grange | f29961d | 2015-02-12 09:31:40 -0800 | [diff] [blame] | 1359 | NULL, /* vpx_codec_set_fb_fn_t set_fb_fn; */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1360 | }, |
| 1361 | { |
Jim Bankoski | 8a774e1 | 2014-07-31 06:27:57 -0700 | [diff] [blame] | 1362 | 1, /* 1 cfg map */ |
Adrian Grange | f29961d | 2015-02-12 09:31:40 -0800 | [diff] [blame] | 1363 | vp8e_usage_cfg_map, /* vpx_codec_enc_cfg_map_t cfg_maps; */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1364 | vp8e_encode, /* vpx_codec_encode_fn_t encode; */ |
Adrian Grange | f29961d | 2015-02-12 09:31:40 -0800 | [diff] [blame] | 1365 | vp8e_get_cxdata, /* vpx_codec_get_cx_data_fn_t get_cx_data; */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1366 | vp8e_set_config, |
Dmitry Kovalev | cd6d984 | 2014-08-20 17:16:28 -0700 | [diff] [blame] | 1367 | NULL, |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1368 | vp8e_get_preview, |
Yunqing Wang | aa7335e | 2011-10-25 15:14:16 -0400 | [diff] [blame] | 1369 | vp8e_mr_alloc_mem, |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1370 | } /* encoder functions */ |
| 1371 | }; |