| @@ -28,7 +28,6 @@ · 27 unmodified lines |
| 28 | 28 | #include "av1.h" |
| 29 | 29 | #endif |
| 30 | 30 | |
| 31 | | −#include "libavutil/buffer.h" |
| 32 | 31 | #include "libavutil/hwcontext_cuda.h" |
| 33 | 32 | #include "libavutil/hwcontext.h" |
| 34 | 33 | #include "libavutil/cuda_check.h" |
| @@ -168,27 +167,6 @@ static int nvenc_print_error(AVCodecContext *avctx, NVENCSTATUS err, · 133 unmodified lines |
| 168 | 167 | return ret; |
| 169 | 168 | } |
| 170 | 169 | |
| 171 | | −typedef struct FrameData { |
| 172 | | − int64_t pts; |
| 173 | | − int64_t duration; |
| 174 | | −#if FF_API_REORDERED_OPAQUE |
| 175 | | − int64_t reordered_opaque; |
| 176 | | −#endif |
| 177 | | − |
| 178 | | − void *frame_opaque; |
| 179 | | − AVBufferRef *frame_opaque_ref; |
| 180 | | −} FrameData; |
| 181 | | − |
| 182 | | −static void reorder_queue_flush(AVFifo *queue) |
| 183 | | −{ |
| 184 | | − FrameData fd; |
| 185 | | − |
| 186 | | − av_assert0(queue); |
| 187 | | − |
| 188 | | − while (av_fifo_read(queue, &fd, 1) >= 0) |
| 189 | | − av_buffer_unref(&fd.frame_opaque_ref); |
| 190 | | −} |
| 191 | | − |
| 192 | 170 | typedef struct GUIDTuple { |
| 193 | 171 | const GUID guid; |
| 194 | 172 | int flags; |
| @@ -1789,8 +1767,8 @@ static av_cold int nvenc_setup_surfaces(AVCodecContext *avctx) · 1594 unmodified lines |
| 1789 | 1767 | if (!ctx->surfaces) |
| 1790 | 1768 | return AVERROR(ENOMEM); |
| 1791 | 1769 | |
| 1792 | | − ctx->reorder_queue = av_fifo_alloc2(ctx->nb_surfaces, sizeof(FrameData), 0); |
| 1793 | | − if (!ctx->reorder_queue) |
| 1770 | + ctx->timestamp_list = av_fifo_alloc2(ctx->nb_surfaces, sizeof(int64_t), 0); |
| 1771 | + if (!ctx->timestamp_list) |
| 1794 | 1772 | return AVERROR(ENOMEM); |
| 1795 | 1773 | |
| 1796 | 1774 | ctx->unused_surface_queue = av_fifo_alloc2(ctx->nb_surfaces, sizeof(NvencSurface*), 0); |
| @@ -1874,11 +1852,7 @@ av_cold int ff_nvenc_encode_close(AVCodecContext *avctx) · 77 unmodified lines |
| 1874 | 1852 | p_nvenc->nvEncEncodePicture(ctx->nvencoder, ¶ms); |
| 1875 | 1853 | } |
| 1876 | 1854 | |
| 1877 | | − if (ctx->reorder_queue) { |
| 1878 | | − reorder_queue_flush(ctx->reorder_queue); |
| 1879 | | − av_fifo_freep2(&ctx->reorder_queue); |
| 1880 | | − } |
| 1881 | | − |
| 1855 | + av_fifo_freep2(&ctx->timestamp_list); |
| 1882 | 1856 | av_fifo_freep2(&ctx->output_surface_ready_queue); |
| 1883 | 1857 | av_fifo_freep2(&ctx->output_surface_queue); |
| 1884 | 1858 | av_fifo_freep2(&ctx->unused_surface_queue); |
| @@ -2222,53 +2196,18 @@ static void nvenc_codec_specific_pic_params(AVCodecContext *avctx, · 337 unmodified lines |
| 2222 | 2196 | } |
| 2223 | 2197 | } |
| 2224 | 2198 | |
| 2225 | | −static void reorder_queue_enqueue(AVFifo *queue, const AVCodecContext *avctx, |
| 2226 | | − const AVFrame *frame, AVBufferRef **opaque_ref) |
| 2199 | +static inline void timestamp_queue_enqueue(AVFifo *queue, int64_t timestamp) |
| 2227 | 2200 | { |
| 2228 | | − FrameData fd; |
| 2229 | | − |
| 2230 | | − fd.pts = frame->pts; |
| 2231 | | − fd.duration = frame->duration; |
| 2232 | | −#if FF_API_REORDERED_OPAQUE |
| 2233 | | −FF_DISABLE_DEPRECATION_WARNINGS |
| 2234 | | − fd.reordered_opaque = frame->reordered_opaque; |
| 2235 | | −FF_ENABLE_DEPRECATION_WARNINGS |
| 2236 | | −#endif |
| 2237 | | − fd.frame_opaque = frame->opaque; |
| 2238 | | − fd.frame_opaque_ref = *opaque_ref; |
| 2239 | | − |
| 2240 | | − *opaque_ref = NULL; |
| 2241 | | − |
| 2242 | | − av_fifo_write(queue, &fd, 1); |
| 2201 | + av_fifo_write(queue, ×tamp, 1); |
| 2243 | 2202 | } |
| 2244 | 2203 | |
| 2245 | | −static int64_t reorder_queue_dequeue(AVFifo *queue, AVCodecContext *avctx, |
| 2246 | | − AVPacket *pkt) |
| 2204 | +static inline int64_t timestamp_queue_dequeue(AVFifo *queue) |
| 2247 | 2205 | { |
| 2248 | | − FrameData fd; |
| 2249 | | − |
| 2206 | + int64_t timestamp = AV_NOPTS_VALUE; |
| 2250 | 2207 | // The following call might fail if the queue is empty. |
| 2251 | | − if (av_fifo_read(queue, &fd, 1) < 0) |
| 2252 | | − return AV_NOPTS_VALUE; |
| 2253 | | − |
| 2254 | | − if (pkt) { |
| 2255 | | −#if FF_API_REORDERED_OPAQUE |
| 2256 | | −FF_DISABLE_DEPRECATION_WARNINGS |
| 2257 | | − avctx->reordered_opaque = fd.reordered_opaque; |
| 2258 | | −FF_ENABLE_DEPRECATION_WARNINGS |
| 2259 | | −#endif |
| 2260 | | − pkt->duration = fd.duration; |
| 2208 | + av_fifo_read(queue, ×tamp, 1); |
| 2261 | 2209 | |
| 2262 | | − if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) { |
| 2263 | | − pkt->opaque = fd.frame_opaque; |
| 2264 | | − pkt->opaque_ref = fd.frame_opaque_ref; |
| 2265 | | − fd.frame_opaque_ref = NULL; |
| 2266 | | − } |
| 2267 | | − } |
| 2268 | | − |
| 2269 | | − av_buffer_unref(&fd.frame_opaque_ref); |
| 2270 | | − |
| 2271 | | − return fd.pts; |
| 2210 | + return timestamp; |
| 2272 | 2211 | } |
| 2273 | 2212 | |
| 2274 | 2213 | static int nvenc_set_timestamp(AVCodecContext *avctx, |
| @@ -2276,15 +2215,12 @@ static int nvenc_set_timestamp(AVCodecContext *avctx, · 1 unmodified line |
| 2276 | 2215 | AVPacket *pkt) |
| 2277 | 2216 | { |
| 2278 | 2217 | NvencContext *ctx = avctx->priv_data; |
| 2279 | | − int64_t dts; |
| 2280 | 2218 | |
| 2281 | 2219 | pkt->pts = params->outputTimeStamp; |
| 2282 | 2220 | |
| 2283 | | − dts = reorder_queue_dequeue(ctx->reorder_queue, avctx, pkt); |
| 2284 | | − |
| 2285 | 2221 | if (avctx->codec_descriptor->props & AV_CODEC_PROP_REORDER) { |
| 2286 | 2222 | FF_DISABLE_DEPRECATION_WARNINGS |
| 2287 | | − pkt->dts = dts - |
| 2223 | + pkt->dts = timestamp_queue_dequeue(ctx->timestamp_list) - |
| 2288 | 2224 | #if FF_API_TICKS_PER_FRAME |
| 2289 | 2225 | FFMAX(avctx->ticks_per_frame, 1) * |
| 2290 | 2226 | #endif |
| @@ -2386,7 +2322,7 @@ static int process_output_surface(AVCodecContext *avctx, AVPacket *pkt, NvencSur · 95 unmodified lines |
| 2386 | 2322 | return 0; |
| 2387 | 2323 | |
| 2388 | 2324 | error: |
| 2389 | | − reorder_queue_dequeue(ctx->reorder_queue, avctx, NULL); |
| 2325 | + timestamp_queue_dequeue(ctx->timestamp_list); |
| 2390 | 2326 | |
| 2391 | 2327 | error2: |
| 2392 | 2328 | return res; |
| @@ -2616,8 +2552,6 @@ static int nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame) · 223 unmodified lines |
| 2616 | 2552 | int sei_count = 0; |
| 2617 | 2553 | int i; |
| 2618 | 2554 | |
| 2619 | | − AVBufferRef *opaque_ref = NULL; |
| 2620 | | − |
| 2621 | 2555 | NvencContext *ctx = avctx->priv_data; |
| 2622 | 2556 | NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs; |
| 2623 | 2557 | NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs; |
| @@ -2685,17 +2619,9 @@ static int nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame) · 61 unmodified lines |
| 2685 | 2619 | pic_params.encodePicFlags = NV_ENC_PIC_FLAG_EOS; |
| 2686 | 2620 | } |
| 2687 | 2621 | |
| 2688 | | − // make a reference for enqueing in the reorder queue here, |
| 2689 | | − // so that reorder_queue_enqueue() cannot fail |
| 2690 | | − if (frame && frame->opaque_ref && avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) { |
| 2691 | | − opaque_ref = av_buffer_ref(frame->opaque_ref); |
| 2692 | | − if (!opaque_ref) |
| 2693 | | − return AVERROR(ENOMEM); |
| 2694 | | − } |
| 2695 | | − |
| 2696 | 2622 | res = nvenc_push_context(avctx); |
| 2697 | 2623 | if (res < 0) |
| 2698 | | − goto opaque_ref_fail; |
| 2624 | + return res; |
| 2699 | 2625 | |
| 2700 | 2626 | nv_status = p_nvenc->nvEncEncodePicture(ctx->nvencoder, &pic_params); |
| 2701 | 2627 | |
| @@ -2704,17 +2630,17 @@ static int nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame) · 2 unmodified lines |
| 2704 | 2630 | |
| 2705 | 2631 | res = nvenc_pop_context(avctx); |
| 2706 | 2632 | if (res < 0) |
| 2707 | | − goto opaque_ref_fail; |
| 2633 | + return res; |
| 2708 | 2634 | |
| 2709 | 2635 | if (nv_status != NV_ENC_SUCCESS && |
| 2710 | | − nv_status != NV_ENC_ERR_NEED_MORE_INPUT) { |
| 2711 | | − res = nvenc_print_error(avctx, nv_status, "EncodePicture failed!"); |
| 2712 | | − goto opaque_ref_fail; |
| 2713 | | − } |
| 2636 | + nv_status != NV_ENC_ERR_NEED_MORE_INPUT) |
| 2637 | + return nvenc_print_error(avctx, nv_status, "EncodePicture failed!"); |
| 2714 | 2638 | |
| 2715 | 2639 | if (frame && frame->buf[0]) { |
| 2716 | 2640 | av_fifo_write(ctx->output_surface_queue, &in_surf, 1); |
| 2717 | | − reorder_queue_enqueue(ctx->reorder_queue, avctx, frame, &opaque_ref); |
| 2641 | + |
| 2642 | + if (avctx->codec_descriptor->props & AV_CODEC_PROP_REORDER) |
| 2643 | + timestamp_queue_enqueue(ctx->timestamp_list, frame->pts); |
| 2718 | 2644 | } |
| 2719 | 2645 | |
| 2720 | 2646 | /* all the pending buffers are now ready for output */ |
| @@ -2724,10 +2650,6 @@ static int nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame) · 3 unmodified lines |
| 2724 | 2650 | } |
| 2725 | 2651 | |
| 2726 | 2652 | return 0; |
| 2727 | | − |
| 2728 | | −opaque_ref_fail: |
| 2729 | | − av_buffer_unref(&opaque_ref); |
| 2730 | | − return res; |
| 2731 | 2653 | } |
| 2732 | 2654 | |
| 2733 | 2655 | int ff_nvenc_receive_packet(AVCodecContext *avctx, AVPacket *pkt) |
| @@ -2786,5 +2708,5 @@ av_cold void ff_nvenc_encode_flush(AVCodecContext *avctx) · 52 unmodified lines |
| 2786 | 2708 | NvencContext *ctx = avctx->priv_data; |
| 2787 | 2709 | |
| 2788 | 2710 | nvenc_send_frame(avctx, NULL); |
| 2789 | | − reorder_queue_flush(ctx->reorder_queue); |
| 2711 | + av_fifo_reset2(ctx->timestamp_list); |
| 2790 | 2712 | } |