From patchwork Sun Mar 21 17:08:59 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 26522 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 EF50D44B3D0 for ; Sun, 21 Mar 2021 19:15:43 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id BA76068A67E; Sun, 21 Mar 2021 19:15:43 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qk1-f181.google.com (mail-qk1-f181.google.com [209.85.222.181]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9E1F268A57A for ; Sun, 21 Mar 2021 19:15:36 +0200 (EET) Received: by mail-qk1-f181.google.com with SMTP id q26so3057175qkm.6 for ; Sun, 21 Mar 2021 10:15:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=RpOjWtFrMgnpZYAc/AzkLPxWDxb9CWBNIvoY9IsdJv4=; b=WYqAFAAlGzdUBOmTUPfFlg9XLrf1WpNI24UaYFXcy+oKWZ2RTjEtmHx6wrB7MQIElw bo1PfhCYSpa0+C+Ff9++wz28WiCdumzrS3T6X12sXMMEbk1ze70ifhNsn9RjElXWTi7S XXATsI7qNvkZQ3N0BgsqB+wwZczxJad2fhKybVczXUS69f39bZF30QbzO52njk/c9reR F+nDbtyrRAgzUg/RLC5M4Gi33vdZ5fzkJv4p3WMXjLV1c8P1Sd0csN9AdXB4UMyI1g5U zV14SkfFf94vPZOsLoLN+78sRgGVYrR6GGMybUb7CCTPniCXTWjlpMGa099M0LMFSs3S 18CA== 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:mime-version :content-transfer-encoding; bh=RpOjWtFrMgnpZYAc/AzkLPxWDxb9CWBNIvoY9IsdJv4=; b=StSxKrcfd5RC2JcJvEC6tL1WZ9XXzSJumOAjdjJg7jEkYqivLUlugydNSoxeYtNC4L oGvzhz7fxIEC5cVREoVq6a17IePFcwx5SbbqH5bOl84I/OrAjAS6GP5jXZg9r4jAwmMR I73zU/t1y09V840yqCtZa5FyIgjF6w2fWMKpQ+AY7us9gfmyYLiddA/+gxjlvG7fSsYi zl7euozxp+FTZGJYR54NkGPNDUMp1liVUXx1rnr9QDNZ+MxhjTQ2X42JGu40vkpoZVYd obxFLzGExoJMTKD+NEroMJZDiMv0B9fI4PXkgmFlBCd1Q4Sk8+qMV9xNnz02gZE/mZIP oQTQ== X-Gm-Message-State: AOAM531k6KQ2lheWpmt1Z0nL2zHvt76FH8jzltQsEl/xx4Aw2ue3JEg1 EscsnK343Gf1aCZaVEdtpxlnfQwnwic= X-Google-Smtp-Source: ABdhPJxN3Paukd/RNT4ZdaSo3n0px3Lktb/mF5dTqSjiVjB2ym4T31/qzaJzV8TNtfdIvi4ZEmOIbw== X-Received: by 2002:a37:a81:: with SMTP id 123mr7424472qkk.144.1616346573309; Sun, 21 Mar 2021 10:09:33 -0700 (PDT) Received: from localhost.localdomain ([191.84.255.89]) by smtp.gmail.com with ESMTPSA id o7sm9047610qki.63.2021.03.21.10.09.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Mar 2021 10:09:32 -0700 (PDT) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Sun, 21 Mar 2021 14:08:59 -0300 Message-Id: <20210321170900.2045-1-jamrial@gmail.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/2] avformat/utils: add an internal getter function to access AVStream.attached_pic 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" It can be removed once the field becomes a pointer. Signed-off-by: James Almer --- libavformat/apetag.c | 9 ++++----- libavformat/asfdec_f.c | 13 +++++++------ libavformat/asfdec_o.c | 13 +++++++------ libavformat/flac_picture.c | 14 ++++++++------ libavformat/hls.c | 8 +++++--- libavformat/id3v2.c | 14 ++++++++------ libavformat/internal.h | 2 ++ libavformat/matroskadec.c | 2 +- libavformat/mov.c | 19 +++++++++++-------- libavformat/options.c | 12 +++++++++++- libavformat/utils.c | 18 ++++++++++++++++-- libavformat/wtvdec.c | 8 +++++--- 12 files changed, 85 insertions(+), 47 deletions(-) diff --git a/libavformat/apetag.c b/libavformat/apetag.c index 454c6c688b..fce0d49c01 100644 --- a/libavformat/apetag.c +++ b/libavformat/apetag.c @@ -79,10 +79,10 @@ static int ape_tag_read_field(AVFormatContext *s) av_dict_set(&st->metadata, key, filename, 0); if ((id = ff_guess_image2_codec(filename)) != AV_CODEC_ID_NONE) { - AVPacket pkt; + AVPacket *attached_pic = ff_stream_get_attached_pic(st); int ret; - ret = av_get_packet(s->pb, &pkt, size); + ret = av_get_packet(s->pb, attached_pic, size); if (ret < 0) { av_log(s, AV_LOG_ERROR, "Error reading cover art.\n"); return ret; @@ -92,9 +92,8 @@ static int ape_tag_read_field(AVFormatContext *s) st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; st->codecpar->codec_id = id; - st->attached_pic = pkt; - st->attached_pic.stream_index = st->index; - st->attached_pic.flags |= AV_PKT_FLAG_KEY; + attached_pic->stream_index = st->index; + attached_pic->flags |= AV_PKT_FLAG_KEY; } else { if ((ret = ff_get_extradata(s, st->codecpar, s->pb, size)) < 0) return ret; diff --git a/libavformat/asfdec_f.c b/libavformat/asfdec_f.c index 1484b544d9..05b06beb4f 100644 --- a/libavformat/asfdec_f.c +++ b/libavformat/asfdec_f.c @@ -223,7 +223,7 @@ static int get_value(AVIOContext *pb, int type, int type2_size) * but in reality this is only loosely similar */ static int asf_read_picture(AVFormatContext *s, int len) { - AVPacket pkt = { 0 }; + AVPacket *attached_pic, *pkt = s->internal->parse_pkt; const CodecMime *mime = ff_id3v2_mime_tags; enum AVCodecID id = AV_CODEC_ID_NONE; char mimetype[64]; @@ -277,7 +277,7 @@ static int asf_read_picture(AVFormatContext *s, int len) return AVERROR(ENOMEM); len -= avio_get_str16le(s->pb, len - picsize, desc, desc_len); - ret = av_get_packet(s->pb, &pkt, picsize); + ret = av_get_packet(s->pb, pkt, picsize); if (ret < 0) goto fail; @@ -289,9 +289,10 @@ static int asf_read_picture(AVFormatContext *s, int len) st->disposition |= AV_DISPOSITION_ATTACHED_PIC; st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; st->codecpar->codec_id = id; - st->attached_pic = pkt; - st->attached_pic.stream_index = st->index; - st->attached_pic.flags |= AV_PKT_FLAG_KEY; + attached_pic = ff_stream_get_attached_pic(st); + av_packet_move_ref(attached_pic, pkt); + attached_pic->stream_index = st->index; + attached_pic->flags |= AV_PKT_FLAG_KEY; if (*desc) av_dict_set(&st->metadata, "title", desc, AV_DICT_DONT_STRDUP_VAL); @@ -304,7 +305,7 @@ static int asf_read_picture(AVFormatContext *s, int len) fail: av_freep(&desc); - av_packet_unref(&pkt); + av_packet_unref(pkt); return ret; } diff --git a/libavformat/asfdec_o.c b/libavformat/asfdec_o.c index 34ae541934..ac032f98fc 100644 --- a/libavformat/asfdec_o.c +++ b/libavformat/asfdec_o.c @@ -360,7 +360,7 @@ static int asf_set_metadata(AVFormatContext *s, const uint8_t *name, * but in reality this is only loosely similar */ static int asf_read_picture(AVFormatContext *s, int len) { - AVPacket pkt = { 0 }; + AVPacket *attached_pic, *pkt = s->internal->parse_pkt; const CodecMime *mime = ff_id3v2_mime_tags; enum AVCodecID id = AV_CODEC_ID_NONE; char mimetype[64]; @@ -414,7 +414,7 @@ static int asf_read_picture(AVFormatContext *s, int len) return AVERROR(ENOMEM); len -= avio_get_str16le(s->pb, len - picsize, desc, desc_len); - ret = av_get_packet(s->pb, &pkt, picsize); + ret = av_get_packet(s->pb, pkt, picsize); if (ret < 0) goto fail; @@ -427,9 +427,10 @@ static int asf_read_picture(AVFormatContext *s, int len) st->disposition |= AV_DISPOSITION_ATTACHED_PIC; st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; st->codecpar->codec_id = id; - st->attached_pic = pkt; - st->attached_pic.stream_index = st->index; - st->attached_pic.flags |= AV_PKT_FLAG_KEY; + attached_pic = ff_stream_get_attached_pic(st); + av_packet_move_ref(attached_pic, pkt); + attached_pic->stream_index = st->index; + attached_pic->flags |= AV_PKT_FLAG_KEY; if (*desc) { if (av_dict_set(&st->metadata, "title", desc, AV_DICT_DONT_STRDUP_VAL) < 0) @@ -444,7 +445,7 @@ static int asf_read_picture(AVFormatContext *s, int len) fail: av_freep(&desc); - av_packet_unref(&pkt); + av_packet_unref(pkt); return ret; } diff --git a/libavformat/flac_picture.c b/libavformat/flac_picture.c index f15cfa877a..52a2b0f1fb 100644 --- a/libavformat/flac_picture.c +++ b/libavformat/flac_picture.c @@ -37,6 +37,7 @@ int ff_flac_parse_picture(AVFormatContext *s, uint8_t *buf, int buf_size, int tr uint8_t mimetype[64], *desc = NULL; GetByteContext g; AVStream *st; + AVPacket *attached_pic; int width, height, ret = 0; unsigned int type; uint32_t len, left, trunclen = 0; @@ -165,12 +166,13 @@ int ff_flac_parse_picture(AVFormatContext *s, uint8_t *buf, int buf_size, int tr RETURN_ERROR(AVERROR(ENOMEM)); } - av_packet_unref(&st->attached_pic); - st->attached_pic.buf = data; - st->attached_pic.data = data->data; - st->attached_pic.size = len; - st->attached_pic.stream_index = st->index; - st->attached_pic.flags |= AV_PKT_FLAG_KEY; + attached_pic = ff_stream_get_attached_pic(st); + av_packet_unref(attached_pic); + attached_pic->buf = data; + attached_pic->data = data->data; + attached_pic->size = len; + attached_pic->stream_index = st->index; + attached_pic->flags |= AV_PKT_FLAG_KEY; st->disposition |= AV_DISPOSITION_ATTACHED_PIC; st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; diff --git a/libavformat/hls.c b/libavformat/hls.c index 597bea7f25..d75b129e45 100644 --- a/libavformat/hls.c +++ b/libavformat/hls.c @@ -1068,15 +1068,17 @@ static int id3_has_changed_values(struct playlist *pls, AVDictionary *metadata, } /* check if apic appeared */ - if (apic && (pls->ctx->nb_streams != 2 || !pls->ctx->streams[1]->attached_pic.data)) + if (apic && (pls->ctx->nb_streams != 2 || + !ff_stream_get_attached_pic(pls->ctx->streams[1])->data)) return 1; if (apic) { - int size = pls->ctx->streams[1]->attached_pic.size; + AVPacket *attached_pic = ff_stream_get_attached_pic(pls->ctx->streams[1]); + int size = attached_pic->size; if (size != apic->buf->size - AV_INPUT_BUFFER_PADDING_SIZE) return 1; - if (memcmp(apic->buf->data, pls->ctx->streams[1]->attached_pic.data, size) != 0) + if (memcmp(apic->buf->data, attached_pic->data, size) != 0) return 1; } diff --git a/libavformat/id3v2.c b/libavformat/id3v2.c index f33b7ba93a..c3df59af4f 100644 --- a/libavformat/id3v2.c +++ b/libavformat/id3v2.c @@ -1141,6 +1141,7 @@ int ff_id3v2_parse_apic(AVFormatContext *s, ID3v2ExtraMeta *extra_meta) for (cur = extra_meta; cur; cur = cur->next) { ID3v2ExtraMetaAPIC *apic; + AVPacket *attached_pic; AVStream *st; if (strcmp(cur->tag, "APIC")) @@ -1162,12 +1163,13 @@ int ff_id3v2_parse_apic(AVFormatContext *s, ID3v2ExtraMeta *extra_meta) av_dict_set(&st->metadata, "comment", apic->type, 0); - av_packet_unref(&st->attached_pic); - st->attached_pic.buf = apic->buf; - st->attached_pic.data = apic->buf->data; - st->attached_pic.size = apic->buf->size - AV_INPUT_BUFFER_PADDING_SIZE; - st->attached_pic.stream_index = st->index; - st->attached_pic.flags |= AV_PKT_FLAG_KEY; + attached_pic = ff_stream_get_attached_pic(st); + av_packet_unref(attached_pic); + attached_pic->buf = apic->buf; + attached_pic->data = apic->buf->data; + attached_pic->size = apic->buf->size - AV_INPUT_BUFFER_PADDING_SIZE; + attached_pic->stream_index = st->index; + attached_pic->flags |= AV_PKT_FLAG_KEY; apic->buf = NULL; } diff --git a/libavformat/internal.h b/libavformat/internal.h index 3c6b2921c1..aad2e80cc0 100644 --- a/libavformat/internal.h +++ b/libavformat/internal.h @@ -892,6 +892,8 @@ int ff_unlock_avformat(void); */ void ff_format_set_url(AVFormatContext *s, char *url); +AVPacket *ff_stream_get_attached_pic(AVStream *st); + void avpriv_register_devices(const AVOutputFormat * const o[], const AVInputFormat * const i[]); #endif /* AVFORMAT_INTERNAL_H */ diff --git a/libavformat/matroskadec.c b/libavformat/matroskadec.c index 9acfdf5b32..c05fb35be7 100644 --- a/libavformat/matroskadec.c +++ b/libavformat/matroskadec.c @@ -3007,7 +3007,7 @@ static int matroska_read_header(AVFormatContext *s) attachments[j].stream = st; if (st->codecpar->codec_id != AV_CODEC_ID_NONE) { - AVPacket *pkt = &st->attached_pic; + AVPacket *pkt = ff_stream_get_attached_pic(st); st->disposition |= AV_DISPOSITION_ATTACHED_PIC; st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; diff --git a/libavformat/mov.c b/libavformat/mov.c index f9c4dbe5d4..e8de594e25 100644 --- a/libavformat/mov.c +++ b/libavformat/mov.c @@ -182,6 +182,7 @@ static int mov_read_mac_string(MOVContext *c, AVIOContext *pb, int len, static int mov_read_covr(MOVContext *c, AVIOContext *pb, int type, int len) { AVStream *st; + AVPacket *attached_pic; MOVStreamContext *sc; enum AVCodecID id; int ret; @@ -204,12 +205,13 @@ static int mov_read_covr(MOVContext *c, AVIOContext *pb, int type, int len) return AVERROR(ENOMEM); st->priv_data = sc; - ret = av_get_packet(pb, &st->attached_pic, len); + attached_pic = ff_stream_get_attached_pic(st); + ret = av_get_packet(pb, attached_pic, len); if (ret < 0) return ret; - if (st->attached_pic.size >= 8 && id != AV_CODEC_ID_BMP) { - if (AV_RB64(st->attached_pic.data) == 0x89504e470d0a1a0a) { + if (attached_pic->size >= 8 && id != AV_CODEC_ID_BMP) { + if (AV_RB64(attached_pic->data) == 0x89504e470d0a1a0a) { id = AV_CODEC_ID_PNG; } else { id = AV_CODEC_ID_MJPEG; @@ -218,8 +220,8 @@ static int mov_read_covr(MOVContext *c, AVIOContext *pb, int type, int len) st->disposition |= AV_DISPOSITION_ATTACHED_PIC; - st->attached_pic.stream_index = st->index; - st->attached_pic.flags |= AV_PKT_FLAG_KEY; + attached_pic->stream_index = st->index; + attached_pic->flags |= AV_PKT_FLAG_KEY; st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; st->codecpar->codec_id = id; @@ -7224,16 +7226,17 @@ static void mov_read_chapters(AVFormatContext *s) if (st->internal->nb_index_entries) { // Retrieve the first frame, if possible AVIndexEntry *sample = &st->internal->index_entries[0]; + AVPacket *attached_pic = ff_stream_get_attached_pic(st); if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) { av_log(s, AV_LOG_ERROR, "Failed to retrieve first frame\n"); goto finish; } - if (av_get_packet(sc->pb, &st->attached_pic, sample->size) < 0) + if (av_get_packet(sc->pb, attached_pic, sample->size) < 0) goto finish; - st->attached_pic.stream_index = st->index; - st->attached_pic.flags |= AV_PKT_FLAG_KEY; + attached_pic->stream_index = st->index; + attached_pic->flags |= AV_PKT_FLAG_KEY; } } else { st->codecpar->codec_type = AVMEDIA_TYPE_DATA; diff --git a/libavformat/options.c b/libavformat/options.c index 07403b533e..bd4056caa1 100644 --- a/libavformat/options.c +++ b/libavformat/options.c @@ -220,11 +220,21 @@ AVFormatContext *avformat_alloc_context(void) av_free(ic); return NULL; } +#if !FF_API_INIT_PACKET + ic->attached_pic = av_packet_alloc(); +#endif internal->pkt = av_packet_alloc(); internal->parse_pkt = av_packet_alloc(); - if (!internal->pkt || !internal->parse_pkt) { + if (!internal->pkt || !internal->parse_pkt +#if !FF_API_INIT_PACKET + || !ic->attached_pic +#endif + ) { av_packet_free(&internal->pkt); av_packet_free(&internal->parse_pkt); +#if !FF_API_INIT_PACKET + av_packet_free(&ic->attached_pic); +#endif av_free(internal); av_free(ic); return NULL; diff --git a/libavformat/utils.c b/libavformat/utils.c index ee947c195d..3db12af1b6 100644 --- a/libavformat/utils.c +++ b/libavformat/utils.c @@ -457,7 +457,8 @@ int avformat_queue_attached_pictures(AVFormatContext *s) for (i = 0; i < s->nb_streams; i++) if (s->streams[i]->disposition & AV_DISPOSITION_ATTACHED_PIC && s->streams[i]->discard < AVDISCARD_ALL) { - if (s->streams[i]->attached_pic.size <= 0) { + AVPacket *attached_pic = ff_stream_get_attached_pic(s->streams[i]); + if (attached_pic->size <= 0) { av_log(s, AV_LOG_WARNING, "Attached picture on stream %d has invalid size, " "ignoring\n", i); @@ -466,7 +467,7 @@ int avformat_queue_attached_pictures(AVFormatContext *s) ret = avpriv_packet_list_put(&s->internal->raw_packet_buffer, &s->internal->raw_packet_buffer_end, - &s->streams[i]->attached_pic, + attached_pic, av_packet_ref, 0); if (ret < 0) return ret; @@ -4382,8 +4383,12 @@ static void free_stream(AVStream **pst) if (st->parser) av_parser_close(st->parser); +#if FF_API_INIT_PACKET if (st->attached_pic.data) av_packet_unref(&st->attached_pic); +#else + av_packet_free(st->attached_pic); +#endif if (st->internal) { avcodec_free_context(&st->internal->avctx); @@ -5855,3 +5860,12 @@ FF_DISABLE_DEPRECATION_WARNINGS FF_ENABLE_DEPRECATION_WARNINGS #endif } + +AVPacket *ff_stream_get_attached_pic(AVStream *st) +{ +#if FF_API_INIT_PACKET + return &st->attached_pic; +#else + return st->attached_pic; +#endif +} diff --git a/libavformat/wtvdec.c b/libavformat/wtvdec.c index 7def9d2348..9613cfbd41 100644 --- a/libavformat/wtvdec.c +++ b/libavformat/wtvdec.c @@ -434,6 +434,7 @@ static void get_attachment(AVFormatContext *s, AVIOContext *pb, int length) char description[1024]; unsigned int filesize; AVStream *st; + AVPacket *attached_pic; int ret; int64_t pos = avio_tell(pb); @@ -454,11 +455,12 @@ static void get_attachment(AVFormatContext *s, AVIOContext *pb, int length) st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; st->codecpar->codec_id = AV_CODEC_ID_MJPEG; st->id = -1; - ret = av_get_packet(pb, &st->attached_pic, filesize); + attached_pic = ff_stream_get_attached_pic(st); + ret = av_get_packet(pb, attached_pic, filesize); if (ret < 0) goto done; - st->attached_pic.stream_index = st->index; - st->attached_pic.flags |= AV_PKT_FLAG_KEY; + attached_pic->stream_index = st->index; + attached_pic->flags |= AV_PKT_FLAG_KEY; st->disposition |= AV_DISPOSITION_ATTACHED_PIC; done: avio_seek(pb, pos + length, SEEK_SET);