[FFmpeg-devel] [PATCH] avcodec/frame_thread_encoder: remove usage of avcodec_encode_video2()

James Almer jamrial at gmail.com
Sun May 24 18:02:27 EEST 2020


On 5/17/2020 11:07 AM, James Almer wrote:
> Call the encoder's internal AVCodec.encode2() function instead.
> 
> Signed-off-by: James Almer <jamrial at gmail.com>
> ---
>  libavcodec/encode.c               | 20 ++++++++++++--------
>  libavcodec/frame_thread_encoder.c | 14 ++++++++------
>  2 files changed, 20 insertions(+), 14 deletions(-)
> 
> diff --git a/libavcodec/encode.c b/libavcodec/encode.c
> index 9ed2cf0f59..1358f33ea5 100644
> --- a/libavcodec/encode.c
> +++ b/libavcodec/encode.c
> @@ -277,14 +277,12 @@ int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
>          return AVERROR(ENOSYS);
>      }
>  
> -    if(CONFIG_FRAME_THREAD_ENCODER &&
> -       avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
> -        return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
> -
>      if ((avctx->flags&AV_CODEC_FLAG_PASS1) && avctx->stats_out)
>          avctx->stats_out[0] = '\0';
>  
> -    if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
> +    if (!frame &&
> +        !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
> +          (avctx->internal->frame_thread_encoder && avctx->active_thread_type & FF_THREAD_FRAME))) {
>          av_packet_unref(avpkt);
>          return 0;
>      }
> @@ -299,7 +297,15 @@ int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
>  
>      av_assert0(avctx->codec->encode2);
>  
> -    ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
> +
> +    if (CONFIG_FRAME_THREAD_ENCODER &&
> +        avctx->internal->frame_thread_encoder && avctx->active_thread_type & FF_THREAD_FRAME)
> +        ret = ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
> +    else {
> +        ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
> +        if (!ret && *got_packet_ptr && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
> +            avpkt->pts = avpkt->dts = frame->pts;
> +    }
>      av_assert0(ret <= 0);
>  
>      emms_c();
> @@ -326,8 +332,6 @@ int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
>      if (!ret) {
>          if (!*got_packet_ptr)
>              avpkt->size = 0;
> -        else if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
> -            avpkt->pts = avpkt->dts = frame->pts;
>  
>          if (needs_realloc && avpkt->data) {
>              ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
> diff --git a/libavcodec/frame_thread_encoder.c b/libavcodec/frame_thread_encoder.c
> index 949bc69f81..cea0ea2af3 100644
> --- a/libavcodec/frame_thread_encoder.c
> +++ b/libavcodec/frame_thread_encoder.c
> @@ -66,7 +66,7 @@ static void * attribute_align_arg worker(void *v){
>      AVPacket *pkt = NULL;
>  
>      while (!atomic_load(&c->exit)) {
> -        int got_packet, ret;
> +        int got_packet = 0, ret;
>          AVFrame *frame;
>          Task task;
>  
> @@ -86,19 +86,21 @@ static void * attribute_align_arg worker(void *v){
>          pthread_mutex_unlock(&c->task_fifo_mutex);
>          frame = task.indata;
>  
> -        ret = avcodec_encode_video2(avctx, pkt, frame, &got_packet);
> -        pthread_mutex_lock(&c->buffer_mutex);
> -        av_frame_unref(frame);
> -        pthread_mutex_unlock(&c->buffer_mutex);
> -        av_frame_free(&frame);
> +        ret = avctx->codec->encode2(avctx, pkt, frame, &got_packet);
>          if(got_packet) {
>              int ret2 = av_packet_make_refcounted(pkt);
>              if (ret >= 0 && ret2 < 0)
>                  ret = ret2;
> +            if (!ret)
> +                pkt->pts = pkt->dts = frame->pts;
>          } else {
>              pkt->data = NULL;
>              pkt->size = 0;
>          }
> +        pthread_mutex_lock(&c->buffer_mutex);
> +        av_frame_unref(frame);
> +        pthread_mutex_unlock(&c->buffer_mutex);
> +        av_frame_free(&frame);
>          pthread_mutex_lock(&c->finished_task_mutex);
>          c->finished_tasks[task.index].outdata = pkt; pkt = NULL;
>          c->finished_tasks[task.index].return_code = ret;

Will push soon if no one objects.


More information about the ffmpeg-devel mailing list