Unified · Split
| @@ -2075,6 +2075,101 @@ static int output_ready(AVCodecContext *avctx, int flush) · 2074 unmodified lines | ||
| 2075 | 2075 | return (nb_ready > 0) && (nb_ready + nb_pending >= ctx->async_depth); |
| 2076 | 2076 | } |
| 2077 | 2077 | |
| 2078 | +static int prepare_sei_data_array(AVCodecContext *avctx, const AVFrame *frame) | |
| 2079 | +{ | |
| 2080 | + NvencContext *ctx = avctx->priv_data; | |
| 2081 | + int sei_count = 0; | |
| 2082 | + int i, res; | |
| 2083 | + | |
| 2084 | + if (ctx->a53_cc && av_frame_get_side_data(frame, AV_FRAME_DATA_A53_CC)) { | |
| 2085 | + void *a53_data = NULL; | |
| 2086 | + size_t a53_size = 0; | |
| 2087 | + | |
| 2088 | + if (ff_alloc_a53_sei(frame, 0, &a53_data, &a53_size) < 0) { | |
| 2089 | + av_log(ctx, AV_LOG_ERROR, "Not enough memory for closed captions, skipping\n"); | |
| 2090 | + } | |
| 2091 | + | |
| 2092 | + if (a53_data) { | |
| 2093 | + void *tmp = av_fast_realloc(ctx->sei_data, | |
| 2094 | + &ctx->sei_data_size, | |
| 2095 | + (sei_count + 1) * sizeof(*ctx->sei_data)); | |
| 2096 | + if (!tmp) { | |
| 2097 | + av_free(a53_data); | |
| 2098 | + res = AVERROR(ENOMEM); | |
| 2099 | + goto error; | |
| 2100 | + } else { | |
| 2101 | + ctx->sei_data = tmp; | |
| 2102 | + ctx->sei_data[sei_count].payloadSize = (uint32_t)a53_size; | |
| 2103 | + ctx->sei_data[sei_count].payloadType = 4; | |
| 2104 | + ctx->sei_data[sei_count].payload = (uint8_t*)a53_data; | |
| 2105 | + sei_count++; | |
| 2106 | + } | |
| 2107 | + } | |
| 2108 | + } | |
| 2109 | + | |
| 2110 | + if (ctx->s12m_tc && av_frame_get_side_data(frame, AV_FRAME_DATA_S12M_TIMECODE)) { | |
| 2111 | + void *tc_data = NULL; | |
| 2112 | + size_t tc_size = 0; | |
| 2113 | + | |
| 2114 | + if (ff_alloc_timecode_sei(frame, avctx->framerate, 0, &tc_data, &tc_size) < 0) { | |
| 2115 | + av_log(ctx, AV_LOG_ERROR, "Not enough memory for timecode sei, skipping\n"); | |
| 2116 | + } | |
| 2117 | + | |
| 2118 | + if (tc_data) { | |
| 2119 | + void *tmp = av_fast_realloc(ctx->sei_data, | |
| 2120 | + &ctx->sei_data_size, | |
| 2121 | + (sei_count + 1) * sizeof(*ctx->sei_data)); | |
| 2122 | + if (!tmp) { | |
| 2123 | + av_free(tc_data); | |
| 2124 | + res = AVERROR(ENOMEM); | |
| 2125 | + goto error; | |
| 2126 | + } else { | |
| 2127 | + ctx->sei_data = tmp; | |
| 2128 | + ctx->sei_data[sei_count].payloadSize = (uint32_t)tc_size; | |
| 2129 | + ctx->sei_data[sei_count].payloadType = SEI_TYPE_TIME_CODE; | |
| 2130 | + ctx->sei_data[sei_count].payload = (uint8_t*)tc_data; | |
| 2131 | + sei_count++; | |
| 2132 | + } | |
| 2133 | + } | |
| 2134 | + } | |
| 2135 | + | |
| 2136 | + for (i = 0; i < frame->nb_side_data; i++) { | |
| 2137 | + AVFrameSideData *side_data = frame->side_data[i]; | |
| 2138 | + void *tmp; | |
| 2139 | + | |
| 2140 | + if (side_data->type != AV_FRAME_DATA_SEI_UNREGISTERED) | |
| 2141 | + continue; | |
| 2142 | + | |
| 2143 | + tmp = av_fast_realloc(ctx->sei_data, | |
| 2144 | + &ctx->sei_data_size, | |
| 2145 | + (sei_count + 1) * sizeof(*ctx->sei_data)); | |
| 2146 | + if (!tmp) { | |
| 2147 | + res = AVERROR(ENOMEM); | |
| 2148 | + goto error; | |
| 2149 | + } else { | |
| 2150 | + ctx->sei_data = tmp; | |
| 2151 | + ctx->sei_data[sei_count].payloadSize = side_data->size; | |
| 2152 | + ctx->sei_data[sei_count].payloadType = SEI_TYPE_USER_DATA_UNREGISTERED; | |
| 2153 | + ctx->sei_data[sei_count].payload = av_memdup(side_data->data, side_data->size); | |
| 2154 | + | |
| 2155 | + if (!ctx->sei_data[sei_count].payload) { | |
| 2156 | + res = AVERROR(ENOMEM); | |
| 2157 | + goto error; | |
| 2158 | + } | |
| 2159 | + | |
| 2160 | + sei_count++; | |
| 2161 | + } | |
| 2162 | + } | |
| 2163 | + | |
| 2164 | + return sei_count; | |
| 2165 | + | |
| 2166 | +error: | |
| 2167 | + for (i = 0; i < sei_count; i++) | |
| 2168 | + av_freep(&(ctx->sei_data[i].payload)); | |
| 2169 | + | |
| 2170 | + return res; | |
| 2171 | +} | |
| 2172 | + | |
| 2078 | 2173 | static void reconfig_encoder(AVCodecContext *avctx, const AVFrame *frame) |
| 2079 | 2174 | { |
| 2080 | 2175 | NvencContext *ctx = avctx->priv_data; |
| @@ -2230,85 +2325,10 @@ static int nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame) · 149 unmodified lines | ||
| 2230 | 2325 | |
| 2231 | 2326 | pic_params.inputTimeStamp = frame->pts; |
| 2232 | 2327 | |
| 2233 | − if (ctx->a53_cc && av_frame_get_side_data(frame, AV_FRAME_DATA_A53_CC)) { | |
| 2234 | − void *a53_data = NULL; | |
| 2235 | − size_t a53_size = 0; | |
| 2236 | − | |
| 2237 | − if (ff_alloc_a53_sei(frame, 0, &a53_data, &a53_size) < 0) { | |
| 2238 | − av_log(ctx, AV_LOG_ERROR, "Not enough memory for closed captions, skipping\n"); | |
| 2239 | − } | |
| 2240 | − | |
| 2241 | − if (a53_data) { | |
| 2242 | − void *tmp = av_fast_realloc(ctx->sei_data, | |
| 2243 | − &ctx->sei_data_size, | |
| 2244 | − (sei_count + 1) * sizeof(*ctx->sei_data)); | |
| 2245 | − if (!tmp) { | |
| 2246 | − av_free(a53_data); | |
| 2247 | − res = AVERROR(ENOMEM); | |
| 2248 | − goto sei_failed; | |
| 2249 | − } else { | |
| 2250 | − ctx->sei_data = tmp; | |
| 2251 | − ctx->sei_data[sei_count].payloadSize = (uint32_t)a53_size; | |
| 2252 | − ctx->sei_data[sei_count].payloadType = 4; | |
| 2253 | − ctx->sei_data[sei_count].payload = (uint8_t*)a53_data; | |
| 2254 | − sei_count++; | |
| 2255 | − } | |
| 2256 | − } | |
| 2257 | − } | |
| 2258 | − | |
| 2259 | − if (ctx->s12m_tc && av_frame_get_side_data(frame, AV_FRAME_DATA_S12M_TIMECODE)) { | |
| 2260 | − void *tc_data = NULL; | |
| 2261 | − size_t tc_size = 0; | |
| 2262 | − | |
| 2263 | − if (ff_alloc_timecode_sei(frame, avctx->framerate, 0, &tc_data, &tc_size) < 0) { | |
| 2264 | − av_log(ctx, AV_LOG_ERROR, "Not enough memory for timecode sei, skipping\n"); | |
| 2265 | − } | |
| 2266 | − | |
| 2267 | − if (tc_data) { | |
| 2268 | − void *tmp = av_fast_realloc(ctx->sei_data, | |
| 2269 | − &ctx->sei_data_size, | |
| 2270 | − (sei_count + 1) * sizeof(*ctx->sei_data)); | |
| 2271 | − if (!tmp) { | |
| 2272 | − av_free(tc_data); | |
| 2273 | − res = AVERROR(ENOMEM); | |
| 2274 | − goto sei_failed; | |
| 2275 | − } else { | |
| 2276 | − ctx->sei_data = tmp; | |
| 2277 | − ctx->sei_data[sei_count].payloadSize = (uint32_t)tc_size; | |
| 2278 | − ctx->sei_data[sei_count].payloadType = SEI_TYPE_TIME_CODE; | |
| 2279 | − ctx->sei_data[sei_count].payload = (uint8_t*)tc_data; | |
| 2280 | − sei_count++; | |
| 2281 | − } | |
| 2282 | − } | |
| 2283 | − } | |
| 2284 | − | |
| 2285 | − for (int j = 0; j < frame->nb_side_data; j++) { | |
| 2286 | − AVFrameSideData *side_data = frame->side_data[j]; | |
| 2287 | − void *tmp; | |
| 2288 | − | |
| 2289 | − if (side_data->type != AV_FRAME_DATA_SEI_UNREGISTERED) | |
| 2290 | − continue; | |
| 2291 | − | |
| 2292 | − tmp = av_fast_realloc(ctx->sei_data, | |
| 2293 | − &ctx->sei_data_size, | |
| 2294 | − (sei_count + 1) * sizeof(*ctx->sei_data)); | |
| 2295 | − if (!tmp) { | |
| 2296 | − res = AVERROR(ENOMEM); | |
| 2297 | − goto sei_failed; | |
| 2298 | − } else { | |
| 2299 | − ctx->sei_data = tmp; | |
| 2300 | − ctx->sei_data[sei_count].payloadSize = side_data->size; | |
| 2301 | − ctx->sei_data[sei_count].payloadType = SEI_TYPE_USER_DATA_UNREGISTERED; | |
| 2302 | − ctx->sei_data[sei_count].payload = av_memdup(side_data->data, side_data->size); | |
| 2303 | − | |
| 2304 | − if (!ctx->sei_data[sei_count].payload) { | |
| 2305 | − res = AVERROR(ENOMEM); | |
| 2306 | − goto sei_failed; | |
| 2307 | − } | |
| 2308 | − | |
| 2309 | − sei_count++; | |
| 2310 | − } | |
| 2311 | − } | |
| 2328 | + res = prepare_sei_data_array(avctx, frame); | |
| 2329 | + if (res < 0) | |
| 2330 | + return res; | |
| 2331 | + sei_count = res; | |
| 2312 | 2332 | |
| 2313 | 2333 | nvenc_codec_specific_pic_params(avctx, &pic_params, ctx->sei_data, sei_count); |
| 2314 | 2334 | } else { |
| @@ -2346,12 +2366,6 @@ static int nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame) · 31 unmodified lines | ||
| 2346 | 2366 | } |
| 2347 | 2367 | |
| 2348 | 2368 | return 0; |
| 2349 | − | |
| 2350 | −sei_failed: | |
| 2351 | − for (i = 0; i < sei_count; i++) | |
| 2352 | − av_freep(&(ctx->sei_data[i].payload)); | |
| 2353 | − | |
| 2354 | − return res; | |
| 2355 | 2369 | } |
| 2356 | 2370 | |
| 2357 | 2371 | int ff_nvenc_receive_packet(AVCodecContext *avctx, AVPacket *pkt) |