From patchwork Wed Feb 3 02:57:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 25362 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 8D8D244B763 for ; Wed, 3 Feb 2021 04:58:25 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 72260689D6E; Wed, 3 Feb 2021 04:58:25 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qt1-f178.google.com (mail-qt1-f178.google.com [209.85.160.178]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 35650687F4E for ; Wed, 3 Feb 2021 04:58:19 +0200 (EET) Received: by mail-qt1-f178.google.com with SMTP id v3so16635184qtw.4 for ; Tue, 02 Feb 2021 18:58:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=EkeynGQ6qB1hRyyflBQMzpHz2idXrkYDVK8jE7dab9o=; b=sMFali4gQCbYN3jsld+JcRJNPlxS06sDUqBCyitto1M4LT5G+D5yM97fau/h+1jDjS H6OQrmysV8LN/hooMKCXW+kxNJzYVKl4Xxg2jglPE4uS0iP9K23l3X5/ICwYrEcGbkjO Z6MlVeczVGp824fSZAQ0ne18qRD4BySF55uevJw+8B3MenDZRxQZ0ApINSJ8zFPEKHXR QIWSDpLWOOdRJy/BaRrny9Jy8H2mxmeEPM0ZGs5burx3QHeYHv/SMcsAlsfv3RB2/88e eFXjb00kfjhciOFAgu6J/eq6zezUlpEMhRh6Rr/LiZtIi7a4GXvlZh8l1nogMlFhD0kP U41Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=EkeynGQ6qB1hRyyflBQMzpHz2idXrkYDVK8jE7dab9o=; b=twsAgowMrvM+wcT8B7M0kHrXVT39WGRdAiaSNFc2CaBshSf5SEs8Bd/jaRU6o7ILzP EiesGATYv+RvlL+NHeCQJsID2+dCo2ZxR1ZH8t8L4RB63lZetK7yUv8ACHBjjVwkCymD SYxVlJ2whmip5UEykMYtouCc5rjvuEByBcOfHuzuk0MzDjeYofLVUyUWmnTF37fixNLz E+SdpyuxiJ/Yg8updkF8QHOM8FmVeBSfSPD8nwa2SxFDunCMzacv0ottLf32b8pLgBPi wG4T2f3pmUzn8VXu3hN+9bjIwYw1fRKoHicZ7NB4CbDvPL4OfdEVP7iGBJBSMjcmllkN gefg== X-Gm-Message-State: AOAM530wIiWNmlCoBlc7e2Tor2jC4GA2HK92SvG1U5dUwNPDhDrD10Qo Nam2SlvKlJUIAUxLeneFSvtInYoHAGdP9Q== X-Google-Smtp-Source: ABdhPJzP6tLpnH3y8mf0EyJLLkdNZiMYxuuLN5pyOSCFFCjTm957yRWkw8FONZnaYR8Xw55w7z06Sw== X-Received: by 2002:ac8:1408:: with SMTP id k8mr868889qtj.204.1612321097112; Tue, 02 Feb 2021 18:58:17 -0800 (PST) Received: from localhost.localdomain ([181.23.64.183]) by smtp.gmail.com with ESMTPSA id t71sm682133qka.86.2021.02.02.18.58.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Feb 2021 18:58:16 -0800 (PST) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Tue, 2 Feb 2021 23:57:54 -0300 Message-Id: <20210203025754.6613-1-jamrial@gmail.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: References: MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 09/10] fftools/ffplay: use av_packet_alloc() to allocate packets X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: James Almer --- fftools/ffplay.c | 204 +++++++++++++++++++++++++++++------------------ 1 file changed, 127 insertions(+), 77 deletions(-) diff --git a/fftools/ffplay.c b/fftools/ffplay.c index 9ff0425163..d48edc0797 100644 --- a/fftools/ffplay.c +++ b/fftools/ffplay.c @@ -36,6 +36,7 @@ #include "libavutil/pixdesc.h" #include "libavutil/imgutils.h" #include "libavutil/dict.h" +#include "libavutil/fifo.h" #include "libavutil/parseutils.h" #include "libavutil/samplefmt.h" #include "libavutil/avassert.h" @@ -111,13 +112,12 @@ const int program_birth_year = 2003; static unsigned sws_flags = SWS_BICUBIC; typedef struct MyAVPacketList { - AVPacket pkt; - struct MyAVPacketList *next; + AVPacket *pkt; int serial; } MyAVPacketList; typedef struct PacketQueue { - MyAVPacketList *first_pkt, *last_pkt; + AVFifoBuffer *pkt_list; int nb_packets; int size; int64_t duration; @@ -187,7 +187,8 @@ enum { }; typedef struct Decoder { - AVPacket pkt; + AVPacket *pkt; + AVPacket *tmp_pkt; PacketQueue *queue; AVCodecContext *avctx; int pkt_serial; @@ -361,7 +362,7 @@ static int filter_nbthreads = 0; static int is_full_screen; static int64_t audio_callback_time; -static AVPacket flush_pkt; +static uint8_t flush_pkt; #define FF_QUIT_EVENT (SDL_USEREVENT + 2) @@ -427,28 +428,25 @@ int64_t get_valid_channel_layout(int64_t channel_layout, int channels) static int packet_queue_put_private(PacketQueue *q, AVPacket *pkt) { - MyAVPacketList *pkt1; + MyAVPacketList pkt1; if (q->abort_request) return -1; - pkt1 = av_malloc(sizeof(MyAVPacketList)); - if (!pkt1) - return -1; - pkt1->pkt = *pkt; - pkt1->next = NULL; - if (pkt == &flush_pkt) + if (av_fifo_space(q->pkt_list) < sizeof(pkt1)) { + if (av_fifo_grow(q->pkt_list, sizeof(pkt1)) < 0) + return -1; + } + + if (pkt->data == &flush_pkt) q->serial++; - pkt1->serial = q->serial; + pkt1.pkt = pkt; + pkt1.serial = q->serial; - if (!q->last_pkt) - q->first_pkt = pkt1; - else - q->last_pkt->next = pkt1; - q->last_pkt = pkt1; + av_fifo_generic_write(q->pkt_list, &pkt1, sizeof(pkt1), NULL); q->nb_packets++; - q->size += pkt1->pkt.size + sizeof(*pkt1); - q->duration += pkt1->pkt.duration; + q->size += pkt1.pkt->size + sizeof(pkt1); + q->duration += pkt1.pkt->duration; /* XXX: should duplicate packet data in DV case */ SDL_CondSignal(q->cond); return 0; @@ -456,32 +454,66 @@ static int packet_queue_put_private(PacketQueue *q, AVPacket *pkt) static int packet_queue_put(PacketQueue *q, AVPacket *pkt) { + AVPacket *pkt1; int ret; + pkt1 = av_packet_clone(pkt); + if (!pkt1) + return -1; SDL_LockMutex(q->mutex); - ret = packet_queue_put_private(q, pkt); + ret = packet_queue_put_private(q, pkt1); SDL_UnlockMutex(q->mutex); - - if (pkt != &flush_pkt && ret < 0) + if (ret < 0) { + av_packet_free(&pkt1); av_packet_unref(pkt); + } + + return ret; +} + +static int packet_queue_put_flushpacket(PacketQueue *q) +{ + AVPacket *pkt; + int ret; + + pkt = av_packet_alloc(); + if (!pkt) + return -1; + pkt->data = &flush_pkt; + SDL_LockMutex(q->mutex); + ret = packet_queue_put_private(q, pkt); + SDL_UnlockMutex(q->mutex); + if (ret < 0) + av_packet_free(&pkt); return ret; } static int packet_queue_put_nullpacket(PacketQueue *q, int stream_index) { - AVPacket pkt1, *pkt = &pkt1; - av_init_packet(pkt); - pkt->data = NULL; - pkt->size = 0; + AVPacket *pkt; + int ret; + + pkt = av_packet_alloc(); + if (!pkt) + return -1; pkt->stream_index = stream_index; - return packet_queue_put(q, pkt); + SDL_LockMutex(q->mutex); + ret = packet_queue_put_private(q, pkt); + SDL_UnlockMutex(q->mutex); + if (ret < 0) + av_packet_free(&pkt); + + return ret; } /* packet queue handling */ static int packet_queue_init(PacketQueue *q) { memset(q, 0, sizeof(PacketQueue)); + q->pkt_list = av_fifo_alloc(sizeof(MyAVPacketList)); + if (!q->pkt_list) + return AVERROR(ENOMEM); q->mutex = SDL_CreateMutex(); if (!q->mutex) { av_log(NULL, AV_LOG_FATAL, "SDL_CreateMutex(): %s\n", SDL_GetError()); @@ -498,16 +530,13 @@ static int packet_queue_init(PacketQueue *q) static void packet_queue_flush(PacketQueue *q) { - MyAVPacketList *pkt, *pkt1; + MyAVPacketList pkt1; SDL_LockMutex(q->mutex); - for (pkt = q->first_pkt; pkt; pkt = pkt1) { - pkt1 = pkt->next; - av_packet_unref(&pkt->pkt); - av_freep(&pkt); + while (av_fifo_size(q->pkt_list) >= sizeof(pkt1)) { + av_fifo_generic_read(q->pkt_list, &pkt1, sizeof(pkt1), NULL); + av_packet_free(&pkt1.pkt); } - q->last_pkt = NULL; - q->first_pkt = NULL; q->nb_packets = 0; q->size = 0; q->duration = 0; @@ -517,6 +546,7 @@ static void packet_queue_flush(PacketQueue *q) static void packet_queue_destroy(PacketQueue *q) { packet_queue_flush(q); + av_fifo_freep(&q->pkt_list); SDL_DestroyMutex(q->mutex); SDL_DestroyCond(q->cond); } @@ -532,18 +562,29 @@ static void packet_queue_abort(PacketQueue *q) SDL_UnlockMutex(q->mutex); } -static void packet_queue_start(PacketQueue *q) +static int packet_queue_start(PacketQueue *q) { + AVPacket *pkt; + int ret; + + pkt = av_packet_alloc(); + if (!pkt) + return -1; + pkt->data = &flush_pkt; SDL_LockMutex(q->mutex); q->abort_request = 0; - packet_queue_put_private(q, &flush_pkt); + ret = packet_queue_put_private(q, pkt); SDL_UnlockMutex(q->mutex); + if (ret < 0) + av_packet_free(&pkt); + + return ret; } /* return < 0 if aborted, 0 if no packet and > 0 if packet. */ static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block, int *serial) { - MyAVPacketList *pkt1; + MyAVPacketList pkt1; int ret; SDL_LockMutex(q->mutex); @@ -554,18 +595,15 @@ static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block, int *seria break; } - pkt1 = q->first_pkt; - if (pkt1) { - q->first_pkt = pkt1->next; - if (!q->first_pkt) - q->last_pkt = NULL; + if (av_fifo_size(q->pkt_list) >= sizeof(pkt1)) { + av_fifo_generic_read(q->pkt_list, &pkt1, sizeof(pkt1), NULL); q->nb_packets--; - q->size -= pkt1->pkt.size + sizeof(*pkt1); - q->duration -= pkt1->pkt.duration; - *pkt = pkt1->pkt; + q->size -= pkt1.pkt->size + sizeof(pkt1); + q->duration -= pkt1.pkt->duration; + av_packet_move_ref(pkt, pkt1.pkt); if (serial) - *serial = pkt1->serial; - av_free(pkt1); + *serial = pkt1.serial; + av_packet_free(&pkt1.pkt); ret = 1; break; } else if (!block) { @@ -579,21 +617,28 @@ static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block, int *seria return ret; } -static void decoder_init(Decoder *d, AVCodecContext *avctx, PacketQueue *queue, SDL_cond *empty_queue_cond) { +static int decoder_init(Decoder *d, AVCodecContext *avctx, PacketQueue *queue, SDL_cond *empty_queue_cond) { memset(d, 0, sizeof(Decoder)); + d->pkt = av_packet_alloc(); + d->tmp_pkt = av_packet_alloc(); + if (!d->pkt || !d->tmp_pkt) { + av_packet_free(&d->pkt); + av_packet_free(&d->tmp_pkt); + return AVERROR(ENOMEM); + } d->avctx = avctx; d->queue = queue; d->empty_queue_cond = empty_queue_cond; d->start_pts = AV_NOPTS_VALUE; d->pkt_serial = -1; + return 0; } static int decoder_decode_frame(Decoder *d, AVFrame *frame, AVSubtitle *sub) { + AVPacket *pkt = d->tmp_pkt; int ret = AVERROR(EAGAIN); for (;;) { - AVPacket pkt; - if (d->queue->serial == d->pkt_serial) { do { if (d->queue->abort_request) @@ -639,18 +684,18 @@ static int decoder_decode_frame(Decoder *d, AVFrame *frame, AVSubtitle *sub) { if (d->queue->nb_packets == 0) SDL_CondSignal(d->empty_queue_cond); if (d->packet_pending) { - av_packet_move_ref(&pkt, &d->pkt); + av_packet_move_ref(pkt, d->pkt); d->packet_pending = 0; } else { - if (packet_queue_get(d->queue, &pkt, 1, &d->pkt_serial) < 0) + if (packet_queue_get(d->queue, pkt, 1, &d->pkt_serial) < 0) return -1; } if (d->queue->serial == d->pkt_serial) break; - av_packet_unref(&pkt); + av_packet_unref(pkt); } while (1); - if (pkt.data == flush_pkt.data) { + if (pkt->data == &flush_pkt) { avcodec_flush_buffers(d->avctx); d->finished = 0; d->next_pts = d->start_pts; @@ -658,30 +703,31 @@ static int decoder_decode_frame(Decoder *d, AVFrame *frame, AVSubtitle *sub) { } else { if (d->avctx->codec_type == AVMEDIA_TYPE_SUBTITLE) { int got_frame = 0; - ret = avcodec_decode_subtitle2(d->avctx, sub, &got_frame, &pkt); + ret = avcodec_decode_subtitle2(d->avctx, sub, &got_frame, pkt); if (ret < 0) { ret = AVERROR(EAGAIN); } else { - if (got_frame && !pkt.data) { + if (got_frame && !pkt->data) { d->packet_pending = 1; - av_packet_move_ref(&d->pkt, &pkt); + av_packet_move_ref(d->pkt, pkt); } - ret = got_frame ? 0 : (pkt.data ? AVERROR(EAGAIN) : AVERROR_EOF); + ret = got_frame ? 0 : (pkt->data ? AVERROR(EAGAIN) : AVERROR_EOF); } } else { - if (avcodec_send_packet(d->avctx, &pkt) == AVERROR(EAGAIN)) { + if (avcodec_send_packet(d->avctx, pkt) == AVERROR(EAGAIN)) { av_log(d->avctx, AV_LOG_ERROR, "Receive_frame and send_packet both returned EAGAIN, which is an API violation.\n"); d->packet_pending = 1; - av_packet_move_ref(&d->pkt, &pkt); + av_packet_move_ref(d->pkt, pkt); } } - av_packet_unref(&pkt); + av_packet_unref(pkt); } } } static void decoder_destroy(Decoder *d) { - av_packet_unref(&d->pkt); + av_packet_free(&d->pkt); + av_packet_free(&d->tmp_pkt); avcodec_free_context(&d->avctx); } @@ -2685,7 +2731,8 @@ static int stream_component_open(VideoState *is, int stream_index) is->audio_stream = stream_index; is->audio_st = ic->streams[stream_index]; - decoder_init(&is->auddec, avctx, &is->audioq, is->continue_read_thread); + if ((ret = decoder_init(&is->auddec, avctx, &is->audioq, is->continue_read_thread)) < 0) + goto fail; if ((is->ic->iformat->flags & (AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH | AVFMT_NO_BYTE_SEEK)) && !is->ic->iformat->read_seek) { is->auddec.start_pts = is->audio_st->start_time; is->auddec.start_pts_tb = is->audio_st->time_base; @@ -2698,7 +2745,8 @@ static int stream_component_open(VideoState *is, int stream_index) is->video_stream = stream_index; is->video_st = ic->streams[stream_index]; - decoder_init(&is->viddec, avctx, &is->videoq, is->continue_read_thread); + if ((ret = decoder_init(&is->viddec, avctx, &is->videoq, is->continue_read_thread)) < 0) + goto fail; if ((ret = decoder_start(&is->viddec, video_thread, "video_decoder", is)) < 0) goto out; is->queue_attachments_req = 1; @@ -2707,7 +2755,8 @@ static int stream_component_open(VideoState *is, int stream_index) is->subtitle_stream = stream_index; is->subtitle_st = ic->streams[stream_index]; - decoder_init(&is->subdec, avctx, &is->subtitleq, is->continue_read_thread); + if ((ret = decoder_init(&is->subdec, avctx, &is->subtitleq, is->continue_read_thread)) < 0) + goto fail; if ((ret = decoder_start(&is->subdec, subtitle_thread, "subtitle_decoder", is)) < 0) goto out; break; @@ -2760,7 +2809,7 @@ static int read_thread(void *arg) AVFormatContext *ic = NULL; int err, i, ret; int st_index[AVMEDIA_TYPE_NB]; - AVPacket pkt1, *pkt = &pkt1; + AVPacket *pkt = NULL; int64_t stream_start_time; int pkt_in_play_range = 0; AVDictionaryEntry *t; @@ -2777,6 +2826,12 @@ static int read_thread(void *arg) memset(st_index, -1, sizeof(st_index)); is->eof = 0; + pkt = av_packet_alloc(); + if (!pkt) { + av_log(NULL, AV_LOG_FATAL, "Could not allocate packet.\n"); + ret = AVERROR(ENOMEM); + goto fail; + } ic = avformat_alloc_context(); if (!ic) { av_log(NULL, AV_LOG_FATAL, "Could not allocate context.\n"); @@ -2962,15 +3017,15 @@ static int read_thread(void *arg) } else { if (is->audio_stream >= 0) { packet_queue_flush(&is->audioq); - packet_queue_put(&is->audioq, &flush_pkt); + packet_queue_put_flushpacket(&is->audioq); } if (is->subtitle_stream >= 0) { packet_queue_flush(&is->subtitleq); - packet_queue_put(&is->subtitleq, &flush_pkt); + packet_queue_put_flushpacket(&is->subtitleq); } if (is->video_stream >= 0) { packet_queue_flush(&is->videoq); - packet_queue_put(&is->videoq, &flush_pkt); + packet_queue_put_flushpacket(&is->videoq); } if (is->seek_flags & AVSEEK_FLAG_BYTE) { set_clock(&is->extclk, NAN, 0); @@ -2986,10 +3041,7 @@ static int read_thread(void *arg) } if (is->queue_attachments_req) { if (is->video_st && is->video_st->disposition & AV_DISPOSITION_ATTACHED_PIC) { - AVPacket copy; - if ((ret = av_packet_ref(©, &is->video_st->attached_pic)) < 0) - goto fail; - packet_queue_put(&is->videoq, ©); + packet_queue_put(&is->videoq, &is->video_st->attached_pic); packet_queue_put_nullpacket(&is->videoq, is->video_stream); } is->queue_attachments_req = 0; @@ -3066,6 +3118,7 @@ static int read_thread(void *arg) if (ic && !is->ic) avformat_close_input(&ic); + av_packet_free(&pkt); if (ret != 0) { SDL_Event event; @@ -3738,9 +3791,6 @@ int main(int argc, char **argv) SDL_EventState(SDL_SYSWMEVENT, SDL_IGNORE); SDL_EventState(SDL_USEREVENT, SDL_IGNORE); - av_init_packet(&flush_pkt); - flush_pkt.data = (uint8_t *)&flush_pkt; - if (!display_disable) { int flags = SDL_WINDOW_HIDDEN; if (alwaysontop)