Martin Storsjö | 8a3d9ca | 2013-12-12 15:13:55 | [diff] [blame] | 1 | /* |
| 2 | * OpenH264 video encoder |
| 3 | * Copyright (C) 2014 Martin Storsjo |
| 4 | * |
Michael Niedermayer | c09eeca | 2015-01-06 23:48:23 | [diff] [blame] | 5 | * This file is part of FFmpeg. |
Martin Storsjö | 8a3d9ca | 2013-12-12 15:13:55 | [diff] [blame] | 6 | * |
Michael Niedermayer | c09eeca | 2015-01-06 23:48:23 | [diff] [blame] | 7 | * FFmpeg is free software; you can redistribute it and/or |
Martin Storsjö | 8a3d9ca | 2013-12-12 15:13:55 | [diff] [blame] | 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation; either |
| 10 | * version 2.1 of the License, or (at your option) any later version. |
| 11 | * |
Michael Niedermayer | c09eeca | 2015-01-06 23:48:23 | [diff] [blame] | 12 | * FFmpeg is distributed in the hope that it will be useful, |
Martin Storsjö | 8a3d9ca | 2013-12-12 15:13:55 | [diff] [blame] | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
Michael Niedermayer | c09eeca | 2015-01-06 23:48:23 | [diff] [blame] | 18 | * License along with FFmpeg; if not, write to the Free Software |
Martin Storsjö | 8a3d9ca | 2013-12-12 15:13:55 | [diff] [blame] | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #include <wels/codec_api.h> |
| 23 | #include <wels/codec_ver.h> |
| 24 | |
| 25 | #include "libavutil/attributes.h" |
| 26 | #include "libavutil/common.h" |
Andreas Rheinhardt | 790f793 | 2024-03-25 00:30:37 | [diff] [blame] | 27 | #include "libavutil/mem.h" |
Martin Storsjö | 8a3d9ca | 2013-12-12 15:13:55 | [diff] [blame] | 28 | #include "libavutil/opt.h" |
Luca Barbato | 81c95eb | 2015-12-08 17:12:33 | [diff] [blame] | 29 | #include "libavutil/internal.h" |
Martin Storsjö | 8a3d9ca | 2013-12-12 15:13:55 | [diff] [blame] | 30 | #include "libavutil/intreadwrite.h" |
| 31 | #include "libavutil/mathematics.h" |
| 32 | |
| 33 | #include "avcodec.h" |
Andreas Rheinhardt | a688f3c | 2022-03-16 17:18:28 | [diff] [blame] | 34 | #include "codec_internal.h" |
Andreas Rheinhardt | 0812a60 | 2021-04-24 23:43:26 | [diff] [blame] | 35 | #include "encode.h" |
Martin Storsjö | c5d326f | 2016-06-23 21:58:17 | [diff] [blame] | 36 | #include "libopenh264.h" |
Martin Storsjö | 8a3d9ca | 2013-12-12 15:13:55 | [diff] [blame] | 37 | |
Martin Storsjö | 293676c | 2016-07-08 20:21:41 | [diff] [blame] | 38 | #if !OPENH264_VER_AT_LEAST(1, 6) |
| 39 | #define SM_SIZELIMITED_SLICE SM_DYN_SLICE |
| 40 | #endif |
| 41 | |
Linjie Fu | e5f097e | 2020-04-29 03:00:47 | [diff] [blame] | 42 | #define TARGET_BITRATE_DEFAULT 2*1000*1000 |
| 43 | |
Martin Storsjö | 8a3d9ca | 2013-12-12 15:13:55 | [diff] [blame] | 44 | typedef struct SVCContext { |
| 45 | const AVClass *av_class; |
| 46 | ISVCEncoder *encoder; |
| 47 | int slice_mode; |
| 48 | int loopfilter; |
Linjie Fu | d3a7bdd | 2020-05-06 13:47:50 | [diff] [blame] | 49 | int profile; |
Mario Gasparoni | c3e5c47 | 2015-10-08 17:28:55 | [diff] [blame] | 50 | int max_nal_size; |
Martin Storsjö | 9e14a99 | 2015-10-28 09:06:36 | [diff] [blame] | 51 | int skip_frames; |
Martin Storsjö | 8edaf62 | 2015-10-28 09:12:42 | [diff] [blame] | 52 | int skipped; |
Linjie Fu | e4d37ab | 2020-05-06 13:47:52 | [diff] [blame] | 53 | int coder; |
Linjie Fu | 75fc3f9 | 2020-04-29 03:00:48 | [diff] [blame] | 54 | |
| 55 | // rate control mode |
| 56 | int rc_mode; |
Martin Storsjö | 8a3d9ca | 2013-12-12 15:13:55 | [diff] [blame] | 57 | } SVCContext; |
| 58 | |
| 59 | #define OFFSET(x) offsetof(SVCContext, x) |
| 60 | #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM |
Linjie Fu | 59a9204 | 2020-04-29 03:00:50 | [diff] [blame] | 61 | #define DEPRECATED AV_OPT_FLAG_DEPRECATED |
Martin Storsjö | 8a3d9ca | 2013-12-12 15:13:55 | [diff] [blame] | 62 | static const AVOption options[] = { |
Stefano Sabatini | ae72b57 | 2015-09-08 13:53:42 | [diff] [blame] | 63 | { "loopfilter", "enable loop filter", OFFSET(loopfilter), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, VE }, |
Anton Khirnov | 1e7d200 | 2024-02-11 14:41:05 | [diff] [blame] | 64 | { "profile", "set profile restrictions", OFFSET(profile), AV_OPT_TYPE_INT, { .i64 = AV_PROFILE_UNKNOWN }, AV_PROFILE_UNKNOWN, 0xffff, VE, .unit = "profile" }, |
| 65 | #define PROFILE(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, { .i64 = value }, 0, 0, VE, .unit = "profile" |
Andreas Rheinhardt | 8238bc0 | 2023-09-02 12:57:41 | [diff] [blame] | 66 | { PROFILE("constrained_baseline", AV_PROFILE_H264_CONSTRAINED_BASELINE) }, |
| 67 | { PROFILE("main", AV_PROFILE_H264_MAIN) }, |
| 68 | { PROFILE("high", AV_PROFILE_H264_HIGH) }, |
Linjie Fu | d3a7bdd | 2020-05-06 13:47:50 | [diff] [blame] | 69 | #undef PROFILE |
Stefano Sabatini | 6e891d5 | 2015-12-14 18:08:53 | [diff] [blame] | 70 | { "max_nal_size", "set maximum NAL size in bytes", OFFSET(max_nal_size), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE }, |
| 71 | { "allow_skip_frames", "allow skipping frames to hit the target bitrate", OFFSET(skip_frames), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, |
Anton Khirnov | 1e7d200 | 2024-02-11 14:41:05 | [diff] [blame] | 72 | { "coder", "Coder type", OFFSET(coder), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE, .unit = "coder" }, |
| 73 | { "default", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = -1 }, INT_MIN, INT_MAX, VE, .unit = "coder" }, |
| 74 | { "cavlc", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, VE, .unit = "coder" }, |
| 75 | { "cabac", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, VE, .unit = "coder" }, |
| 76 | { "vlc", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, VE, .unit = "coder" }, |
| 77 | { "ac", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, VE, .unit = "coder" }, |
Linjie Fu | 75fc3f9 | 2020-04-29 03:00:48 | [diff] [blame] | 78 | |
Anton Khirnov | 1e7d200 | 2024-02-11 14:41:05 | [diff] [blame] | 79 | { "rc_mode", "Select rate control mode", OFFSET(rc_mode), AV_OPT_TYPE_INT, { .i64 = RC_QUALITY_MODE }, RC_OFF_MODE, RC_TIMESTAMP_MODE, VE, .unit = "rc_mode" }, |
| 80 | { "off", "bit rate control off", 0, AV_OPT_TYPE_CONST, { .i64 = RC_OFF_MODE }, 0, 0, VE, .unit = "rc_mode" }, |
| 81 | { "quality", "quality mode", 0, AV_OPT_TYPE_CONST, { .i64 = RC_QUALITY_MODE }, 0, 0, VE, .unit = "rc_mode" }, |
| 82 | { "bitrate", "bitrate mode", 0, AV_OPT_TYPE_CONST, { .i64 = RC_BITRATE_MODE }, 0, 0, VE, .unit = "rc_mode" }, |
| 83 | { "buffer", "using buffer status to adjust the video quality (no bitrate control)", 0, AV_OPT_TYPE_CONST, { .i64 = RC_BUFFERBASED_MODE }, 0, 0, VE, .unit = "rc_mode" }, |
Linjie Fu | 75fc3f9 | 2020-04-29 03:00:48 | [diff] [blame] | 84 | #if OPENH264_VER_AT_LEAST(1, 4) |
Anton Khirnov | 1e7d200 | 2024-02-11 14:41:05 | [diff] [blame] | 85 | { "timestamp", "bit rate control based on timestamp", 0, AV_OPT_TYPE_CONST, { .i64 = RC_TIMESTAMP_MODE }, 0, 0, VE, .unit = "rc_mode" }, |
Linjie Fu | 75fc3f9 | 2020-04-29 03:00:48 | [diff] [blame] | 86 | #endif |
| 87 | |
Martin Storsjö | 8a3d9ca | 2013-12-12 15:13:55 | [diff] [blame] | 88 | { NULL } |
| 89 | }; |
| 90 | |
| 91 | static const AVClass class = { |
Tobias Rapp | 7992c28 | 2018-11-12 16:04:43 | [diff] [blame] | 92 | .class_name = "libopenh264enc", |
Anton Khirnov | 08bebeb | 2024-01-19 12:33:28 | [diff] [blame] | 93 | .item_name = av_default_item_name, |
Diego Biurrun | 97cfe1d | 2017-06-10 14:45:06 | [diff] [blame] | 94 | .option = options, |
| 95 | .version = LIBAVUTIL_VERSION_INT, |
Martin Storsjö | 8a3d9ca | 2013-12-12 15:13:55 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | static av_cold int svc_encode_close(AVCodecContext *avctx) |
| 99 | { |
| 100 | SVCContext *s = avctx->priv_data; |
| 101 | |
| 102 | if (s->encoder) |
| 103 | WelsDestroySVCEncoder(s->encoder); |
Martin Storsjö | 8edaf62 | 2015-10-28 09:12:42 | [diff] [blame] | 104 | if (s->skipped > 0) |
| 105 | av_log(avctx, AV_LOG_WARNING, "%d frames skipped\n", s->skipped); |
Martin Storsjö | 8a3d9ca | 2013-12-12 15:13:55 | [diff] [blame] | 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | static av_cold int svc_encode_init(AVCodecContext *avctx) |
| 110 | { |
| 111 | SVCContext *s = avctx->priv_data; |
| 112 | SEncParamExt param = { 0 }; |
Gregory J. Wolfe | 1a4c5fe | 2015-09-09 19:32:42 | [diff] [blame] | 113 | int log_level; |
Gregory J. Wolfe | e8c45b9 | 2015-09-09 19:32:42 | [diff] [blame] | 114 | WelsTraceCallback callback_function; |
Anton Khirnov | 11c9bd6 | 2015-10-03 13:19:10 | [diff] [blame] | 115 | AVCPBProperties *props; |
Martin Storsjö | 8a3d9ca | 2013-12-12 15:13:55 | [diff] [blame] | 116 | |
Martin Storsjö | 8a3d9ca | 2013-12-12 15:13:55 | [diff] [blame] | 117 | if (WelsCreateSVCEncoder(&s->encoder)) { |
| 118 | av_log(avctx, AV_LOG_ERROR, "Unable to create encoder\n"); |
| 119 | return AVERROR_UNKNOWN; |
| 120 | } |
| 121 | |
Gregory J. Wolfe | 1a4c5fe | 2015-09-09 19:32:42 | [diff] [blame] | 122 | // Pass all libopenh264 messages to our callback, to allow ourselves to filter them. |
| 123 | log_level = WELS_LOG_DETAIL; |
| 124 | (*s->encoder)->SetOption(s->encoder, ENCODER_OPTION_TRACE_LEVEL, &log_level); |
Gregory J. Wolfe | e8c45b9 | 2015-09-09 19:32:42 | [diff] [blame] | 125 | |
| 126 | // Set the logging callback function to one that uses av_log() (see implementation above). |
Martin Storsjö | c5d326f | 2016-06-23 21:58:17 | [diff] [blame] | 127 | callback_function = (WelsTraceCallback) ff_libopenh264_trace_callback; |
Diego Biurrun | 800d91d | 2015-10-30 14:26:25 | [diff] [blame] | 128 | (*s->encoder)->SetOption(s->encoder, ENCODER_OPTION_TRACE_CALLBACK, &callback_function); |
Gregory J. Wolfe | e8c45b9 | 2015-09-09 19:32:42 | [diff] [blame] | 129 | |
| 130 | // Set the AVCodecContext as the libopenh264 callback context so that it can be passed to av_log(). |
Diego Biurrun | 800d91d | 2015-10-30 14:26:25 | [diff] [blame] | 131 | (*s->encoder)->SetOption(s->encoder, ENCODER_OPTION_TRACE_CALLBACK_CONTEXT, &avctx); |
Gregory J. Wolfe | e8c45b9 | 2015-09-09 19:32:42 | [diff] [blame] | 132 | |
Martin Storsjö | 8a3d9ca | 2013-12-12 15:13:55 | [diff] [blame] | 133 | (*s->encoder)->GetDefaultParams(s->encoder, ¶m); |
| 134 | |
Jun Zhao | becfdaa | 2020-07-26 11:16:43 | [diff] [blame] | 135 | if (avctx->framerate.num > 0 && avctx->framerate.den > 0) { |
| 136 | param.fMaxFrameRate = av_q2d(avctx->framerate); |
| 137 | } else { |
James Almer | 7d07723 | 2025-02-19 15:12:20 | [diff] [blame] | 138 | param.fMaxFrameRate = 1.0 / av_q2d(avctx->time_base); |
Jun Zhao | becfdaa | 2020-07-26 11:16:43 | [diff] [blame] | 139 | } |
Martin Storsjö | 8a3d9ca | 2013-12-12 15:13:55 | [diff] [blame] | 140 | param.iPicWidth = avctx->width; |
| 141 | param.iPicHeight = avctx->height; |
Linjie Fu | e5f097e | 2020-04-29 03:00:47 | [diff] [blame] | 142 | param.iTargetBitrate = avctx->bit_rate > 0 ? avctx->bit_rate : TARGET_BITRATE_DEFAULT; |
Martin Storsjö | 8a3d9ca | 2013-12-12 15:13:55 | [diff] [blame] | 143 | param.iMaxBitrate = FFMAX(avctx->rc_max_rate, avctx->bit_rate); |
Linjie Fu | 75fc3f9 | 2020-04-29 03:00:48 | [diff] [blame] | 144 | param.iRCMode = s->rc_mode; |
Linjie Fu | 433ece8 | 2020-04-29 03:00:46 | [diff] [blame] | 145 | if (avctx->qmax >= 0) |
| 146 | param.iMaxQp = av_clip(avctx->qmax, 1, 51); |
| 147 | if (avctx->qmin >= 0) |
| 148 | param.iMinQp = av_clip(avctx->qmin, 1, param.iMaxQp); |
Martin Storsjö | 8a3d9ca | 2013-12-12 15:13:55 | [diff] [blame] | 149 | param.iTemporalLayerNum = 1; |
| 150 | param.iSpatialLayerNum = 1; |
| 151 | param.bEnableDenoise = 0; |
| 152 | param.bEnableBackgroundDetection = 1; |
| 153 | param.bEnableAdaptiveQuant = 1; |
Martin Storsjö | 9e14a99 | 2015-10-28 09:06:36 | [diff] [blame] | 154 | param.bEnableFrameSkip = s->skip_frames; |
Martin Storsjö | 8a3d9ca | 2013-12-12 15:13:55 | [diff] [blame] | 155 | param.bEnableLongTermReference = 0; |
| 156 | param.iLtrMarkPeriod = 30; |
Linjie Fu | e5f097e | 2020-04-29 03:00:47 | [diff] [blame] | 157 | if (avctx->gop_size >= 0) |
| 158 | param.uiIntraPeriod = avctx->gop_size; |
Martin Storsjö | 25c29d3 | 2015-03-05 11:57:44 | [diff] [blame] | 159 | #if OPENH264_VER_AT_LEAST(1, 4) |
| 160 | param.eSpsPpsIdStrategy = CONSTANT_ID; |
| 161 | #else |
Martin Storsjö | 8a3d9ca | 2013-12-12 15:13:55 | [diff] [blame] | 162 | param.bEnableSpsPpsIdAddition = 0; |
Martin Storsjö | 25c29d3 | 2015-03-05 11:57:44 | [diff] [blame] | 163 | #endif |
Martin Storsjö | 8a3d9ca | 2013-12-12 15:13:55 | [diff] [blame] | 164 | param.bPrefixNalAddingCtrl = 0; |
| 165 | param.iLoopFilterDisableIdc = !s->loopfilter; |
Limin Wang | edd305e | 2022-01-14 09:48:54 | [diff] [blame] | 166 | param.iEntropyCodingModeFlag = s->coder >= 0 ? s->coder : 1; |
Martin Storsjö | 8a3d9ca | 2013-12-12 15:13:55 | [diff] [blame] | 167 | param.iMultipleThreadIdc = avctx->thread_count; |
Linjie Fu | d3a7bdd | 2020-05-06 13:47:50 | [diff] [blame] | 168 | |
Linjie Fu | e3e2702 | 2020-05-06 13:47:51 | [diff] [blame] | 169 | /* Allow specifying the libopenh264 profile through AVCodecContext. */ |
Andreas Rheinhardt | 8238bc0 | 2023-09-02 12:57:41 | [diff] [blame] | 170 | if (AV_PROFILE_UNKNOWN == s->profile && |
| 171 | AV_PROFILE_UNKNOWN != avctx->profile) |
Linjie Fu | e3e2702 | 2020-05-06 13:47:51 | [diff] [blame] | 172 | switch (avctx->profile) { |
Andreas Rheinhardt | 8238bc0 | 2023-09-02 12:57:41 | [diff] [blame] | 173 | case AV_PROFILE_H264_HIGH: |
| 174 | case AV_PROFILE_H264_MAIN: |
| 175 | case AV_PROFILE_H264_CONSTRAINED_BASELINE: |
Linjie Fu | e3e2702 | 2020-05-06 13:47:51 | [diff] [blame] | 176 | s->profile = avctx->profile; |
| 177 | break; |
| 178 | default: |
| 179 | av_log(avctx, AV_LOG_WARNING, |
| 180 | "Unsupported avctx->profile: %d.\n", avctx->profile); |
| 181 | break; |
| 182 | } |
| 183 | |
Andreas Rheinhardt | 8238bc0 | 2023-09-02 12:57:41 | [diff] [blame] | 184 | if (s->profile == AV_PROFILE_UNKNOWN && s->coder >= 0) |
| 185 | s->profile = s->coder == 0 ? AV_PROFILE_H264_CONSTRAINED_BASELINE : |
Linjie Fu | d3a7bdd | 2020-05-06 13:47:50 | [diff] [blame] | 186 | #if OPENH264_VER_AT_LEAST(1, 8) |
Andreas Rheinhardt | 8238bc0 | 2023-09-02 12:57:41 | [diff] [blame] | 187 | AV_PROFILE_H264_HIGH; |
Linjie Fu | d3a7bdd | 2020-05-06 13:47:50 | [diff] [blame] | 188 | #else |
Andreas Rheinhardt | 8238bc0 | 2023-09-02 12:57:41 | [diff] [blame] | 189 | AV_PROFILE_H264_MAIN; |
Linjie Fu | d3a7bdd | 2020-05-06 13:47:50 | [diff] [blame] | 190 | #endif |
| 191 | |
| 192 | switch (s->profile) { |
Andreas Rheinhardt | 8238bc0 | 2023-09-02 12:57:41 | [diff] [blame] | 193 | case AV_PROFILE_H264_HIGH: |
Limin Wang | edd305e | 2022-01-14 09:48:54 | [diff] [blame] | 194 | av_log(avctx, AV_LOG_VERBOSE, "Using %s, " |
| 195 | "select EProfileIdc PRO_HIGH in libopenh264.\n", |
| 196 | param.iEntropyCodingModeFlag ? "CABAC" : "CAVLC"); |
Linjie Fu | d3a7bdd | 2020-05-06 13:47:50 | [diff] [blame] | 197 | break; |
Andreas Rheinhardt | 8238bc0 | 2023-09-02 12:57:41 | [diff] [blame] | 198 | case AV_PROFILE_H264_MAIN: |
Limin Wang | edd305e | 2022-01-14 09:48:54 | [diff] [blame] | 199 | av_log(avctx, AV_LOG_VERBOSE, "Using %s, " |
| 200 | "select EProfileIdc PRO_MAIN in libopenh264.\n", |
| 201 | param.iEntropyCodingModeFlag ? "CABAC" : "CAVLC"); |
Linjie Fu | d3a7bdd | 2020-05-06 13:47:50 | [diff] [blame] | 202 | break; |
Andreas Rheinhardt | 8238bc0 | 2023-09-02 12:57:41 | [diff] [blame] | 203 | case AV_PROFILE_H264_CONSTRAINED_BASELINE: |
| 204 | case AV_PROFILE_UNKNOWN: |
| 205 | s->profile = AV_PROFILE_H264_CONSTRAINED_BASELINE; |
Linjie Fu | d3a7bdd | 2020-05-06 13:47:50 | [diff] [blame] | 206 | param.iEntropyCodingModeFlag = 0; |
| 207 | av_log(avctx, AV_LOG_VERBOSE, "Using CAVLC, " |
| 208 | "select EProfileIdc PRO_BASELINE in libopenh264.\n"); |
| 209 | break; |
| 210 | default: |
Andreas Rheinhardt | 8238bc0 | 2023-09-02 12:57:41 | [diff] [blame] | 211 | s->profile = AV_PROFILE_H264_CONSTRAINED_BASELINE; |
Linjie Fu | d3a7bdd | 2020-05-06 13:47:50 | [diff] [blame] | 212 | param.iEntropyCodingModeFlag = 0; |
| 213 | av_log(avctx, AV_LOG_WARNING, "Unsupported profile, " |
| 214 | "select EProfileIdc PRO_BASELINE in libopenh264.\n"); |
| 215 | break; |
| 216 | } |
Martin Storsjö | 8a3d9ca | 2013-12-12 15:13:55 | [diff] [blame] | 217 | |
| 218 | param.sSpatialLayers[0].iVideoWidth = param.iPicWidth; |
| 219 | param.sSpatialLayers[0].iVideoHeight = param.iPicHeight; |
| 220 | param.sSpatialLayers[0].fFrameRate = param.fMaxFrameRate; |
| 221 | param.sSpatialLayers[0].iSpatialBitrate = param.iTargetBitrate; |
| 222 | param.sSpatialLayers[0].iMaxSpatialBitrate = param.iMaxBitrate; |
Limin Wang | f74e90c | 2022-01-14 09:48:10 | [diff] [blame] | 223 | param.sSpatialLayers[0].uiProfileIdc = s->profile; |
Martin Storsjö | 8a3d9ca | 2013-12-12 15:13:55 | [diff] [blame] | 224 | |
Valery Kot | be827e1 | 2018-11-01 13:15:11 | [diff] [blame] | 225 | #if OPENH264_VER_AT_LEAST(1, 7) |
| 226 | if (avctx->sample_aspect_ratio.num && avctx->sample_aspect_ratio.den) { |
| 227 | // Table E-1. |
| 228 | static const AVRational sar_idc[] = { |
| 229 | { 0, 0 }, // Unspecified (never written here). |
| 230 | { 1, 1 }, { 12, 11 }, { 10, 11 }, { 16, 11 }, |
| 231 | { 40, 33 }, { 24, 11 }, { 20, 11 }, { 32, 11 }, |
| 232 | { 80, 33 }, { 18, 11 }, { 15, 11 }, { 64, 33 }, |
| 233 | { 160, 99 }, // Last 3 are unknown to openh264: { 4, 3 }, { 3, 2 }, { 2, 1 }, |
| 234 | }; |
| 235 | static const ESampleAspectRatio asp_idc[] = { |
| 236 | ASP_UNSPECIFIED, |
| 237 | ASP_1x1, ASP_12x11, ASP_10x11, ASP_16x11, |
| 238 | ASP_40x33, ASP_24x11, ASP_20x11, ASP_32x11, |
| 239 | ASP_80x33, ASP_18x11, ASP_15x11, ASP_64x33, |
| 240 | ASP_160x99, |
| 241 | }; |
| 242 | int num, den, i; |
| 243 | |
| 244 | av_reduce(&num, &den, avctx->sample_aspect_ratio.num, |
| 245 | avctx->sample_aspect_ratio.den, 65535); |
| 246 | |
| 247 | for (i = 1; i < FF_ARRAY_ELEMS(sar_idc); i++) { |
| 248 | if (num == sar_idc[i].num && |
| 249 | den == sar_idc[i].den) |
| 250 | break; |
| 251 | } |
| 252 | if (i == FF_ARRAY_ELEMS(sar_idc)) { |
| 253 | param.sSpatialLayers[0].eAspectRatio = ASP_EXT_SAR; |
| 254 | param.sSpatialLayers[0].sAspectRatioExtWidth = num; |
| 255 | param.sSpatialLayers[0].sAspectRatioExtHeight = den; |
| 256 | } else { |
| 257 | param.sSpatialLayers[0].eAspectRatio = asp_idc[i]; |
| 258 | } |
| 259 | param.sSpatialLayers[0].bAspectRatioPresent = true; |
Linjie Fu | 917d28d | 2020-04-06 11:14:45 | [diff] [blame] | 260 | } else { |
Valery Kot | be827e1 | 2018-11-01 13:15:11 | [diff] [blame] | 261 | param.sSpatialLayers[0].bAspectRatioPresent = false; |
| 262 | } |
| 263 | #endif |
| 264 | |
Martin Storsjö | 31aa533 | 2016-06-23 22:06:12 | [diff] [blame] | 265 | if ((avctx->slices > 1) && (s->max_nal_size)) { |
| 266 | av_log(avctx, AV_LOG_ERROR, |
| 267 | "Invalid combination -slices %d and -max_nal_size %d.\n", |
| 268 | avctx->slices, s->max_nal_size); |
Martin Storsjö | 7a76371 | 2016-07-14 19:24:55 | [diff] [blame] | 269 | return AVERROR(EINVAL); |
Mario Gasparoni | c3e5c47 | 2015-10-08 17:28:55 | [diff] [blame] | 270 | } |
| 271 | |
Martin Storsjö | 8a3d9ca | 2013-12-12 15:13:55 | [diff] [blame] | 272 | if (avctx->slices > 1) |
| 273 | s->slice_mode = SM_FIXEDSLCNUM_SLICE; |
Mario Gasparoni | c3e5c47 | 2015-10-08 17:28:55 | [diff] [blame] | 274 | |
| 275 | if (s->max_nal_size) |
Martin Storsjö | 293676c | 2016-07-08 20:21:41 | [diff] [blame] | 276 | s->slice_mode = SM_SIZELIMITED_SLICE; |
Mario Gasparoni | c3e5c47 | 2015-10-08 17:28:55 | [diff] [blame] | 277 | |
Martin Storsjö | 293676c | 2016-07-08 20:21:41 | [diff] [blame] | 278 | #if OPENH264_VER_AT_LEAST(1, 6) |
| 279 | param.sSpatialLayers[0].sSliceArgument.uiSliceMode = s->slice_mode; |
| 280 | param.sSpatialLayers[0].sSliceArgument.uiSliceNum = avctx->slices; |
| 281 | #else |
Martin Storsjö | 8a3d9ca | 2013-12-12 15:13:55 | [diff] [blame] | 282 | param.sSpatialLayers[0].sSliceCfg.uiSliceMode = s->slice_mode; |
| 283 | param.sSpatialLayers[0].sSliceCfg.sSliceArgument.uiSliceNum = avctx->slices; |
Martin Storsjö | 293676c | 2016-07-08 20:21:41 | [diff] [blame] | 284 | #endif |
Linjie Fu | 9310361 | 2020-04-29 03:00:49 | [diff] [blame] | 285 | if (avctx->slices == 0 && s->slice_mode == SM_FIXEDSLCNUM_SLICE) |
| 286 | av_log(avctx, AV_LOG_WARNING, "Slice count will be set automatically\n"); |
Martin Storsjö | 8a3d9ca | 2013-12-12 15:13:55 | [diff] [blame] | 287 | |
Martin Storsjö | 293676c | 2016-07-08 20:21:41 | [diff] [blame] | 288 | if (s->slice_mode == SM_SIZELIMITED_SLICE) { |
Linjie Fu | 917d28d | 2020-04-06 11:14:45 | [diff] [blame] | 289 | if (s->max_nal_size) { |
Mario Gasparoni | c3e5c47 | 2015-10-08 17:28:55 | [diff] [blame] | 290 | param.uiMaxNalSize = s->max_nal_size; |
Martin Storsjö | 293676c | 2016-07-08 20:21:41 | [diff] [blame] | 291 | #if OPENH264_VER_AT_LEAST(1, 6) |
| 292 | param.sSpatialLayers[0].sSliceArgument.uiSliceSizeConstraint = s->max_nal_size; |
| 293 | #else |
Mario Gasparoni | c3e5c47 | 2015-10-08 17:28:55 | [diff] [blame] | 294 | param.sSpatialLayers[0].sSliceCfg.sSliceArgument.uiSliceSizeConstraint = s->max_nal_size; |
Martin Storsjö | 293676c | 2016-07-08 20:21:41 | [diff] [blame] | 295 | #endif |
Mario Gasparoni | c3e5c47 | 2015-10-08 17:28:55 | [diff] [blame] | 296 | } else { |
Vittorio Giovara | 936f0d9 | 2015-09-13 21:45:24 | [diff] [blame] | 297 | av_log(avctx, AV_LOG_ERROR, "Invalid -max_nal_size, " |
| 298 | "specify a valid max_nal_size to use -slice_mode dyn\n"); |
Martin Storsjö | 7a76371 | 2016-07-14 19:24:55 | [diff] [blame] | 299 | return AVERROR(EINVAL); |
Mario Gasparoni | c3e5c47 | 2015-10-08 17:28:55 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | |
Limin Wang | 008cc90 | 2022-01-14 05:53:19 | [diff] [blame] | 303 | #if OPENH264_VER_AT_LEAST(1, 6) |
| 304 | param.sSpatialLayers[0].uiVideoFormat = VF_UNDEF; |
Jun Zhao | de5e257 | 2023-04-18 09:08:52 | [diff] [blame] | 305 | |
Limin Wang | 008cc90 | 2022-01-14 05:53:19 | [diff] [blame] | 306 | if (avctx->color_range != AVCOL_RANGE_UNSPECIFIED) { |
Limin Wang | 008cc90 | 2022-01-14 05:53:19 | [diff] [blame] | 307 | param.sSpatialLayers[0].bFullRange = (avctx->color_range == AVCOL_RANGE_JPEG); |
Jun Zhao | de5e257 | 2023-04-18 09:08:52 | [diff] [blame] | 308 | } else if (avctx->pix_fmt == AV_PIX_FMT_YUVJ420P) |
| 309 | param.sSpatialLayers[0].bFullRange = 1; |
Limin Wang | 008cc90 | 2022-01-14 05:53:19 | [diff] [blame] | 310 | |
| 311 | if (avctx->colorspace != AVCOL_SPC_UNSPECIFIED || |
| 312 | avctx->color_primaries != AVCOL_PRI_UNSPECIFIED || |
| 313 | avctx->color_trc != AVCOL_TRC_UNSPECIFIED) { |
Limin Wang | 008cc90 | 2022-01-14 05:53:19 | [diff] [blame] | 314 | param.sSpatialLayers[0].bColorDescriptionPresent = true; |
| 315 | } |
| 316 | |
| 317 | if (avctx->colorspace != AVCOL_SPC_UNSPECIFIED) |
| 318 | param.sSpatialLayers[0].uiColorMatrix = avctx->colorspace; |
| 319 | if (avctx->color_primaries != AVCOL_PRI_UNSPECIFIED) |
| 320 | param.sSpatialLayers[0].uiColorPrimaries = avctx->color_primaries; |
| 321 | if (avctx->color_trc != AVCOL_TRC_UNSPECIFIED) |
| 322 | param.sSpatialLayers[0].uiTransferCharacteristics = avctx->color_trc; |
Jun Zhao | 73ddcad | 2023-04-18 15:19:18 | [diff] [blame] | 323 | |
| 324 | param.sSpatialLayers[0].bVideoSignalTypePresent = |
| 325 | (param.sSpatialLayers[0].bFullRange || param.sSpatialLayers[0].bColorDescriptionPresent); |
Limin Wang | 008cc90 | 2022-01-14 05:53:19 | [diff] [blame] | 326 | #endif |
| 327 | |
Martin Storsjö | 8a3d9ca | 2013-12-12 15:13:55 | [diff] [blame] | 328 | if ((*s->encoder)->InitializeExt(s->encoder, ¶m) != cmResultSuccess) { |
| 329 | av_log(avctx, AV_LOG_ERROR, "Initialize failed\n"); |
Martin Storsjö | 7a76371 | 2016-07-14 19:24:55 | [diff] [blame] | 330 | return AVERROR_UNKNOWN; |
Martin Storsjö | 8a3d9ca | 2013-12-12 15:13:55 | [diff] [blame] | 331 | } |
| 332 | |
Vittorio Giovara | 7c6eb0a | 2015-06-29 19:59:37 | [diff] [blame] | 333 | if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) { |
Martin Storsjö | 8a3d9ca | 2013-12-12 15:13:55 | [diff] [blame] | 334 | SFrameBSInfo fbi = { 0 }; |
| 335 | int i, size = 0; |
| 336 | (*s->encoder)->EncodeParameterSets(s->encoder, &fbi); |
| 337 | for (i = 0; i < fbi.sLayerInfo[0].iNalCount; i++) |
| 338 | size += fbi.sLayerInfo[0].pNalLengthInByte[i]; |
Vittorio Giovara | 059a934 | 2015-06-29 21:48:34 | [diff] [blame] | 339 | avctx->extradata = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE); |
Martin Storsjö | 7a76371 | 2016-07-14 19:24:55 | [diff] [blame] | 340 | if (!avctx->extradata) |
| 341 | return AVERROR(ENOMEM); |
Martin Storsjö | 8a3d9ca | 2013-12-12 15:13:55 | [diff] [blame] | 342 | avctx->extradata_size = size; |
| 343 | memcpy(avctx->extradata, fbi.sLayerInfo[0].pBsBuf, size); |
| 344 | } |
| 345 | |
James Almer | 0231df5 | 2023-09-04 12:39:07 | [diff] [blame] | 346 | props = ff_encode_add_cpb_side_data(avctx); |
Martin Storsjö | 7a76371 | 2016-07-14 19:24:55 | [diff] [blame] | 347 | if (!props) |
| 348 | return AVERROR(ENOMEM); |
Anton Khirnov | 11c9bd6 | 2015-10-03 13:19:10 | [diff] [blame] | 349 | props->max_bitrate = param.iMaxBitrate; |
| 350 | props->avg_bitrate = param.iTargetBitrate; |
| 351 | |
Martin Storsjö | 8a3d9ca | 2013-12-12 15:13:55 | [diff] [blame] | 352 | return 0; |
Martin Storsjö | 8a3d9ca | 2013-12-12 15:13:55 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | static int svc_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, |
| 356 | const AVFrame *frame, int *got_packet) |
| 357 | { |
| 358 | SVCContext *s = avctx->priv_data; |
| 359 | SFrameBSInfo fbi = { 0 }; |
| 360 | int i, ret; |
| 361 | int encoded; |
| 362 | SSourcePicture sp = { 0 }; |
| 363 | int size = 0, layer, first_layer = 0; |
| 364 | int layer_size[MAX_LAYER_NUM_OF_FRAME] = { 0 }; |
| 365 | |
| 366 | sp.iColorFormat = videoFormatI420; |
| 367 | for (i = 0; i < 3; i++) { |
| 368 | sp.iStride[i] = frame->linesize[i]; |
| 369 | sp.pData[i] = frame->data[i]; |
| 370 | } |
| 371 | sp.iPicWidth = avctx->width; |
| 372 | sp.iPicHeight = avctx->height; |
| 373 | |
Valery Kot | 67fd8df | 2018-03-16 13:50:34 | [diff] [blame] | 374 | if (frame->pict_type == AV_PICTURE_TYPE_I) { |
| 375 | (*s->encoder)->ForceIntraFrame(s->encoder, true); |
| 376 | } |
| 377 | |
Martin Storsjö | 8a3d9ca | 2013-12-12 15:13:55 | [diff] [blame] | 378 | encoded = (*s->encoder)->EncodeFrame(s->encoder, &sp, &fbi); |
| 379 | if (encoded != cmResultSuccess) { |
| 380 | av_log(avctx, AV_LOG_ERROR, "EncodeFrame failed\n"); |
| 381 | return AVERROR_UNKNOWN; |
| 382 | } |
| 383 | if (fbi.eFrameType == videoFrameTypeSkip) { |
Martin Storsjö | 8edaf62 | 2015-10-28 09:12:42 | [diff] [blame] | 384 | s->skipped++; |
Martin Storsjö | 8a3d9ca | 2013-12-12 15:13:55 | [diff] [blame] | 385 | av_log(avctx, AV_LOG_DEBUG, "frame skipped\n"); |
| 386 | return 0; |
| 387 | } |
| 388 | first_layer = 0; |
Martin Storsjö | 3852e2c | 2015-01-07 22:03:09 | [diff] [blame] | 389 | // Normal frames are returned with one single layer, while IDR |
Martin Storsjö | 8a3d9ca | 2013-12-12 15:13:55 | [diff] [blame] | 390 | // frames have two layers, where the first layer contains the SPS/PPS. |
| 391 | // If using global headers, don't include the SPS/PPS in the returned |
| 392 | // packet - thus, only return one layer. |
Vittorio Giovara | 7c6eb0a | 2015-06-29 19:59:37 | [diff] [blame] | 393 | if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) |
Martin Storsjö | 8a3d9ca | 2013-12-12 15:13:55 | [diff] [blame] | 394 | first_layer = fbi.iLayerNum - 1; |
| 395 | |
| 396 | for (layer = first_layer; layer < fbi.iLayerNum; layer++) { |
| 397 | for (i = 0; i < fbi.sLayerInfo[layer].iNalCount; i++) |
| 398 | layer_size[layer] += fbi.sLayerInfo[layer].pNalLengthInByte[i]; |
| 399 | size += layer_size[layer]; |
| 400 | } |
Martin Storsjö | 6996fd2 | 2015-01-28 20:17:29 | [diff] [blame] | 401 | av_log(avctx, AV_LOG_DEBUG, "%d slices\n", fbi.sLayerInfo[fbi.iLayerNum - 1].iNalCount); |
Martin Storsjö | 8a3d9ca | 2013-12-12 15:13:55 | [diff] [blame] | 402 | |
Andreas Rheinhardt | 0812a60 | 2021-04-24 23:43:26 | [diff] [blame] | 403 | if ((ret = ff_get_encode_buffer(avctx, avpkt, size, 0))) |
Martin Storsjö | 8a3d9ca | 2013-12-12 15:13:55 | [diff] [blame] | 404 | return ret; |
Andreas Rheinhardt | 0812a60 | 2021-04-24 23:43:26 | [diff] [blame] | 405 | |
Martin Storsjö | 8a3d9ca | 2013-12-12 15:13:55 | [diff] [blame] | 406 | size = 0; |
| 407 | for (layer = first_layer; layer < fbi.iLayerNum; layer++) { |
| 408 | memcpy(avpkt->data + size, fbi.sLayerInfo[layer].pBsBuf, layer_size[layer]); |
| 409 | size += layer_size[layer]; |
| 410 | } |
| 411 | avpkt->pts = frame->pts; |
| 412 | if (fbi.eFrameType == videoFrameTypeIDR) |
| 413 | avpkt->flags |= AV_PKT_FLAG_KEY; |
| 414 | *got_packet = 1; |
| 415 | return 0; |
| 416 | } |
| 417 | |
Andreas Rheinhardt | 5aabb25 | 2022-03-16 20:26:11 | [diff] [blame] | 418 | static const FFCodecDefault svc_enc_defaults[] = { |
Linjie Fu | e5f097e | 2020-04-29 03:00:47 | [diff] [blame] | 419 | { "b", "0" }, |
| 420 | { "g", "-1" }, |
Linjie Fu | 433ece8 | 2020-04-29 03:00:46 | [diff] [blame] | 421 | { "qmin", "-1" }, |
| 422 | { "qmax", "-1" }, |
| 423 | { NULL }, |
| 424 | }; |
| 425 | |
Andreas Rheinhardt | 20f9727 | 2022-03-16 20:09:54 | [diff] [blame] | 426 | const FFCodec ff_libopenh264_encoder = { |
| 427 | .p.name = "libopenh264", |
Andreas Rheinhardt | 48286d4 | 2022-08-29 11:38:02 | [diff] [blame] | 428 | CODEC_LONG_NAME("OpenH264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"), |
Andreas Rheinhardt | 20f9727 | 2022-03-16 20:09:54 | [diff] [blame] | 429 | .p.type = AVMEDIA_TYPE_VIDEO, |
| 430 | .p.id = AV_CODEC_ID_H264, |
Anton Khirnov | 8d73f3c | 2022-11-27 12:37:10 | [diff] [blame] | 431 | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_OTHER_THREADS | |
| 432 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, |
Martin Storsjö | 8a3d9ca | 2013-12-12 15:13:55 | [diff] [blame] | 433 | .priv_data_size = sizeof(SVCContext), |
| 434 | .init = svc_encode_init, |
Andreas Rheinhardt | 4243da4 | 2022-03-30 21:28:24 | [diff] [blame] | 435 | FF_CODEC_ENCODE_CB(svc_encode_frame), |
Martin Storsjö | 8a3d9ca | 2013-12-12 15:13:55 | [diff] [blame] | 436 | .close = svc_encode_close, |
Andreas Rheinhardt | 21b23ce | 2022-07-09 22:05:45 | [diff] [blame] | 437 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | |
Anton Khirnov | 8a12907 | 2021-03-09 17:00:44 | [diff] [blame] | 438 | FF_CODEC_CAP_AUTO_THREADS, |
Andreas Rheinhardt | 0971fcf | 2025-03-07 00:19:27 | [diff] [blame] | 439 | CODEC_PIXFMTS(AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P), |
Niklas Haas | 703288c | 2023-10-11 14:09:33 | [diff] [blame] | 440 | .color_ranges = AVCOL_RANGE_MPEG | AVCOL_RANGE_JPEG, |
Linjie Fu | 433ece8 | 2020-04-29 03:00:46 | [diff] [blame] | 441 | .defaults = svc_enc_defaults, |
Andreas Rheinhardt | 20f9727 | 2022-03-16 20:09:54 | [diff] [blame] | 442 | .p.priv_class = &class, |
| 443 | .p.wrapper_name = "libopenh264", |
Martin Storsjö | 8a3d9ca | 2013-12-12 15:13:55 | [diff] [blame] | 444 | }; |