From patchwork Sun Jan 10 01:20:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marton Balint X-Patchwork-Id: 24875 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 BDE5344AFAF for ; Sun, 10 Jan 2021 03:20:57 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 906DB68A115; Sun, 10 Jan 2021 03:20:57 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from iq.passwd.hu (iq.passwd.hu [217.27.212.140]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 394D06899C9 for ; Sun, 10 Jan 2021 03:20:51 +0200 (EET) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 063E9E4ECC; Sun, 10 Jan 2021 02:20:51 +0100 (CET) X-Virus-Scanned: amavisd-new at passwd.hu Received: from iq.passwd.hu ([127.0.0.1]) by localhost (iq.passwd.hu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 4otkL8_6GbvZ; Sun, 10 Jan 2021 02:20:48 +0100 (CET) Received: from bluegene.passwd.hu (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 37A41E12F9; Sun, 10 Jan 2021 02:20:48 +0100 (CET) From: Marton Balint To: ffmpeg-devel@ffmpeg.org Date: Sun, 10 Jan 2021 02:20:42 +0100 Message-Id: <20210110012045.20448-1-cus@passwd.hu> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/4] avformat/swfenc: fix generation of FileAttributes tag 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 Cc: Marton Balint Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Marton Balint --- libavformat/swfenc.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libavformat/swfenc.c b/libavformat/swfenc.c index 14be2b72aa..8ee175664a 100644 --- a/libavformat/swfenc.c +++ b/libavformat/swfenc.c @@ -277,10 +277,10 @@ static int swf_write_header(AVFormatContext *s) swf->duration_pos = avio_tell(pb); avio_wl16(pb, (uint16_t)(DUMMY_DURATION * (int64_t)rate / rate_base)); /* frame count */ - /* avm2/swf v9 (also v8?) files require a file attribute tag */ - if (version == 9) { + /* swf v8 and later files require a file attribute tag */ + if (version >= 8) { put_swf_tag(s, TAG_FILEATTRIBUTES); - avio_wl32(pb, 1<<3); /* set ActionScript v3/AVM2 flag */ + avio_wl32(pb, (version >= 9) << 3); /* set ActionScript v3/AVM2 flag */ put_swf_end_tag(s); } From patchwork Sun Jan 10 01:20:43 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marton Balint X-Patchwork-Id: 24876 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 D869944AFAF for ; Sun, 10 Jan 2021 03:21:00 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id BE53E68A7D9; Sun, 10 Jan 2021 03:21:00 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from iq.passwd.hu (iq.passwd.hu [217.27.212.140]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 693FC68A115 for ; Sun, 10 Jan 2021 03:20:52 +0200 (EET) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 52232E12F9; Sun, 10 Jan 2021 02:20:52 +0100 (CET) X-Virus-Scanned: amavisd-new at passwd.hu Received: from iq.passwd.hu ([127.0.0.1]) by localhost (iq.passwd.hu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id DZd7XrerMnHU; Sun, 10 Jan 2021 02:20:50 +0100 (CET) Received: from bluegene.passwd.hu (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 15624E4E60; Sun, 10 Jan 2021 02:20:50 +0100 (CET) From: Marton Balint To: ffmpeg-devel@ffmpeg.org Date: Sun, 10 Jan 2021 02:20:43 +0100 Message-Id: <20210110012045.20448-2-cus@passwd.hu> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210110012045.20448-1-cus@passwd.hu> References: <20210110012045.20448-1-cus@passwd.hu> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/4] avformat/swfenc: add support for muxing png images 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 Cc: Marton Balint Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Marton Balint --- libavformat/swfenc.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/libavformat/swfenc.c b/libavformat/swfenc.c index 8ee175664a..a0b7615629 100644 --- a/libavformat/swfenc.c +++ b/libavformat/swfenc.c @@ -224,11 +224,12 @@ static int swf_write_header(AVFormatContext *s) } if (par->codec_id == AV_CODEC_ID_VP6F || par->codec_id == AV_CODEC_ID_FLV1 || + par->codec_id == AV_CODEC_ID_PNG || par->codec_id == AV_CODEC_ID_MJPEG) { swf->video_st = s->streams[i]; swf->video_par = par; } else { - av_log(s, AV_LOG_ERROR, "SWF muxer only supports VP6, FLV1 and MJPEG\n"); + av_log(s, AV_LOG_ERROR, "SWF muxer only supports VP6, FLV1, PNG and MJPEG\n"); return -1; } } @@ -257,7 +258,7 @@ static int swf_write_header(AVFormatContext *s) if (!strcmp("avm2", s->oformat->name)) version = 9; - else if (swf->video_par && swf->video_par->codec_id == AV_CODEC_ID_VP6F) + else if (swf->video_par && (swf->video_par->codec_id == AV_CODEC_ID_VP6F || swf->video_par->codec_id == AV_CODEC_ID_PNG)) version = 8; /* version 8 and above support VP6 codec */ else if (swf->video_par && swf->video_par->codec_id == AV_CODEC_ID_FLV1) version = 6; /* version 6 and above support FLV1 codec */ @@ -285,7 +286,7 @@ static int swf_write_header(AVFormatContext *s) } /* define a shape with the jpeg inside */ - if (swf->video_par && swf->video_par->codec_id == AV_CODEC_ID_MJPEG) { + if (swf->video_par && (swf->video_par->codec_id == AV_CODEC_ID_MJPEG || swf->video_par->codec_id == AV_CODEC_ID_PNG)) { put_swf_tag(s, TAG_DEFINESHAPE); avio_wl16(pb, SHAPE_ID); /* ID of shape */ @@ -406,7 +407,7 @@ static int swf_write_video(AVFormatContext *s, avio_wl16(pb, swf->video_frame_number++); avio_write(pb, buf, size); put_swf_end_tag(s); - } else if (par->codec_id == AV_CODEC_ID_MJPEG) { + } else if (par->codec_id == AV_CODEC_ID_MJPEG || par->codec_id == AV_CODEC_ID_PNG) { if (swf->swf_frame_number > 0) { /* remove the shape */ put_swf_tag(s, TAG_REMOVEOBJECT); @@ -425,8 +426,9 @@ static int swf_write_video(AVFormatContext *s, avio_wl16(pb, BITMAP_ID); /* ID of the image */ /* a dummy jpeg header seems to be required */ - avio_wb32(pb, 0xffd8ffd9); - /* write the jpeg image */ + if (par->codec_id == AV_CODEC_ID_MJPEG) + avio_wb32(pb, 0xffd8ffd9); + /* write the jpeg/png image */ avio_write(pb, buf, size); put_swf_end_tag(s); From patchwork Sun Jan 10 01:20:44 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marton Balint X-Patchwork-Id: 24879 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 0355744AFAF for ; Sun, 10 Jan 2021 03:21:02 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id DD7A268A755; Sun, 10 Jan 2021 03:21:01 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from iq.passwd.hu (iq.passwd.hu [217.27.212.140]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id EDE056899C9 for ; Sun, 10 Jan 2021 03:20:54 +0200 (EET) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id C771DE4E60; Sun, 10 Jan 2021 02:20:54 +0100 (CET) X-Virus-Scanned: amavisd-new at passwd.hu Received: from iq.passwd.hu ([127.0.0.1]) by localhost (iq.passwd.hu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 6_v4RPPXj2n5; Sun, 10 Jan 2021 02:20:52 +0100 (CET) Received: from bluegene.passwd.hu (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 248E5E4ED0; Sun, 10 Jan 2021 02:20:52 +0100 (CET) From: Marton Balint To: ffmpeg-devel@ffmpeg.org Date: Sun, 10 Jan 2021 02:20:44 +0100 Message-Id: <20210110012045.20448-3-cus@passwd.hu> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210110012045.20448-1-cus@passwd.hu> References: <20210110012045.20448-1-cus@passwd.hu> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 3/4] avformat/swf: add support for reading and writing VP6A and Flash Screen Video codecs 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 Cc: Marton Balint Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Marton Balint --- libavformat/swf.c | 8 +++++--- libavformat/swfdec.c | 10 ++++++++++ libavformat/swfenc.c | 31 ++++++++++++++++++++----------- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/libavformat/swf.c b/libavformat/swf.c index 1aa434a0ea..a3471d917a 100644 --- a/libavformat/swf.c +++ b/libavformat/swf.c @@ -23,7 +23,9 @@ #include "internal.h" const AVCodecTag ff_swf_codec_tags[] = { - { AV_CODEC_ID_FLV1, 0x02 }, - { AV_CODEC_ID_VP6F, 0x04 }, - { AV_CODEC_ID_NONE, 0 }, + { AV_CODEC_ID_FLV1, 0x02 }, + { AV_CODEC_ID_FLASHSV, 0x03 }, + { AV_CODEC_ID_VP6F, 0x04 }, + { AV_CODEC_ID_VP6A, 0x05 }, + { AV_CODEC_ID_NONE, 0 }, }; diff --git a/libavformat/swfdec.c b/libavformat/swfdec.c index 1463f0ad4d..9416187803 100644 --- a/libavformat/swfdec.c +++ b/libavformat/swfdec.c @@ -33,6 +33,7 @@ #include "libavutil/intreadwrite.h" #include "libavcodec/get_bits.h" #include "swf.h" +#include "flv.h" typedef struct SWFDecContext { int samples_per_frame; @@ -307,15 +308,24 @@ static int swf_read_packet(AVFormatContext *s, AVPacket *pkt) for(i=0; inb_streams; i++) { st = s->streams[i]; if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id) { + int pkt_flags = 0; frame = avio_rl16(pb); len -= 2; if (len <= 0) goto skip; + if (st->codecpar->codec_id == AV_CODEC_ID_FLASHSV) { + unsigned flags = avio_r8(pb); + len--; + if (len <= 0) + goto skip; + pkt_flags |= (flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY ? AV_PKT_FLAG_KEY : 0; + } if ((res = av_get_packet(pb, pkt, len)) < 0) return res; pkt->pos = pos; pkt->pts = frame; pkt->stream_index = st->index; + pkt->flags |= pkt_flags; return pkt->size; } } diff --git a/libavformat/swfenc.c b/libavformat/swfenc.c index a0b7615629..7e4e214014 100644 --- a/libavformat/swfenc.c +++ b/libavformat/swfenc.c @@ -24,6 +24,7 @@ #include "libavutil/avassert.h" #include "libavutil/fifo.h" #include "avformat.h" +#include "flv.h" #include "swf.h" #define AUDIO_FIFO_SIZE 65536 @@ -222,14 +223,13 @@ static int swf_write_header(AVFormatContext *s) av_log(s, AV_LOG_ERROR, "SWF muxer only supports 1 video stream\n"); return AVERROR_INVALIDDATA; } - if (par->codec_id == AV_CODEC_ID_VP6F || - par->codec_id == AV_CODEC_ID_FLV1 || + if (ff_codec_get_tag(ff_swf_codec_tags, par->codec_id) || par->codec_id == AV_CODEC_ID_PNG || par->codec_id == AV_CODEC_ID_MJPEG) { swf->video_st = s->streams[i]; swf->video_par = par; } else { - av_log(s, AV_LOG_ERROR, "SWF muxer only supports VP6, FLV1, PNG and MJPEG\n"); + av_log(s, AV_LOG_ERROR, "SWF muxer only supports VP6, FLV, Flash Screen Video, PNG and MJPEG\n"); return -1; } } @@ -258,8 +258,12 @@ static int swf_write_header(AVFormatContext *s) if (!strcmp("avm2", s->oformat->name)) version = 9; - else if (swf->video_par && (swf->video_par->codec_id == AV_CODEC_ID_VP6F || swf->video_par->codec_id == AV_CODEC_ID_PNG)) - version = 8; /* version 8 and above support VP6 codec */ + else if (swf->video_par && (swf->video_par->codec_id == AV_CODEC_ID_VP6A || + swf->video_par->codec_id == AV_CODEC_ID_VP6F || + swf->video_par->codec_id == AV_CODEC_ID_PNG)) + version = 8; /* version 8 and above support VP6 and PNG codec */ + else if (swf->video_par && swf->video_par->codec_id == AV_CODEC_ID_FLASHSV) + version = 7; /* version 7 and above support Flash Screen Video codec */ else if (swf->video_par && swf->video_par->codec_id == AV_CODEC_ID_FLV1) version = 6; /* version 6 and above support FLV1 codec */ else @@ -359,17 +363,17 @@ static int swf_write_header(AVFormatContext *s) } static int swf_write_video(AVFormatContext *s, - AVCodecParameters *par, const uint8_t *buf, int size) + AVCodecParameters *par, const uint8_t *buf, int size, unsigned pkt_flags) { SWFEncContext *swf = s->priv_data; AVIOContext *pb = s->pb; + unsigned codec_tag = ff_codec_get_tag(ff_swf_codec_tags, par->codec_id); /* Flash Player limit */ if (swf->swf_frame_number == 16000) av_log(s, AV_LOG_INFO, "warning: Flash Player limit of 16000 frames reached\n"); - if (par->codec_id == AV_CODEC_ID_VP6F || - par->codec_id == AV_CODEC_ID_FLV1) { + if (codec_tag) { if (swf->video_frame_number == 0) { /* create a new video object */ put_swf_tag(s, TAG_VIDEOSTREAM); @@ -379,7 +383,7 @@ static int swf_write_video(AVFormatContext *s, avio_wl16(pb, par->width); avio_wl16(pb, par->height); avio_w8(pb, 0); - avio_w8(pb,ff_codec_get_tag(ff_swf_codec_tags, par->codec_id)); + avio_w8(pb, codec_tag); put_swf_end_tag(s); /* place the video object for the first time */ @@ -405,6 +409,11 @@ static int swf_write_video(AVFormatContext *s, put_swf_tag(s, TAG_VIDEOFRAME | TAG_LONG); avio_wl16(pb, VIDEO_ID); avio_wl16(pb, swf->video_frame_number++); + if (par->codec_id == AV_CODEC_ID_FLASHSV) { + /* FrameType and CodecId is needed here even if it is not documented correctly in the SWF specs */ + int flags = codec_tag | (pkt_flags & AV_PKT_FLAG_KEY ? FLV_FRAME_KEY : FLV_FRAME_INTER); + avio_w8(pb, flags); + } avio_write(pb, buf, size); put_swf_end_tag(s); } else if (par->codec_id == AV_CODEC_ID_MJPEG || par->codec_id == AV_CODEC_ID_PNG) { @@ -483,7 +492,7 @@ static int swf_write_audio(AVFormatContext *s, /* if audio only stream make sure we add swf frames */ if (!swf->video_par) - swf_write_video(s, par, 0, 0); + swf_write_video(s, par, 0, 0, 0); return 0; } @@ -494,7 +503,7 @@ static int swf_write_packet(AVFormatContext *s, AVPacket *pkt) if (par->codec_type == AVMEDIA_TYPE_AUDIO) return swf_write_audio(s, par, pkt->data, pkt->size); else - return swf_write_video(s, par, pkt->data, pkt->size); + return swf_write_video(s, par, pkt->data, pkt->size, pkt->flags); } static int swf_write_trailer(AVFormatContext *s) From patchwork Sun Jan 10 01:20:45 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marton Balint X-Patchwork-Id: 24880 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 E6DD744AFAF for ; Sun, 10 Jan 2021 03:21:02 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D12ED68A92E; Sun, 10 Jan 2021 03:21:02 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from iq.passwd.hu (iq.passwd.hu [217.27.212.140]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id A53EE68A755 for ; Sun, 10 Jan 2021 03:20:56 +0200 (EET) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 8BADBE4ECC; Sun, 10 Jan 2021 02:20:56 +0100 (CET) X-Virus-Scanned: amavisd-new at passwd.hu Received: from iq.passwd.hu ([127.0.0.1]) by localhost (iq.passwd.hu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id aXxE2Otq_HdQ; Sun, 10 Jan 2021 02:20:54 +0100 (CET) Received: from bluegene.passwd.hu (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id F1D64E4EC6; Sun, 10 Jan 2021 02:20:53 +0100 (CET) From: Marton Balint To: ffmpeg-devel@ffmpeg.org Date: Sun, 10 Jan 2021 02:20:45 +0100 Message-Id: <20210110012045.20448-4-cus@passwd.hu> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210110012045.20448-1-cus@passwd.hu> References: <20210110012045.20448-1-cus@passwd.hu> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 4/4] avcodec/flashsv2enc: factorize updating block dimensions 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 Cc: Marton Balint Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Marton Balint --- libavcodec/flashsv2enc.c | 76 +++++++++++++++++----------------------- 1 file changed, 32 insertions(+), 44 deletions(-) diff --git a/libavcodec/flashsv2enc.c b/libavcodec/flashsv2enc.c index 6603d0ded1..5139b17a28 100644 --- a/libavcodec/flashsv2enc.c +++ b/libavcodec/flashsv2enc.c @@ -174,6 +174,33 @@ static void reset_stats(FlashSV2Context * s) #endif } +static int update_block_dimensions(FlashSV2Context *s, int block_width, int block_height) +{ + s->block_width = block_width; + s->block_height = block_height; + s->rows = (s->image_height + s->block_height - 1) / s->block_height; + s->cols = (s->image_width + s->block_width - 1) / s->block_width; + if (s->rows * s->cols > s->blocks_size / sizeof(Block)) { + s->frame_blocks = av_realloc_array(s->frame_blocks, s->rows, s->cols * sizeof(Block)); + s->key_blocks = av_realloc_array(s->key_blocks, s->cols, s->rows * sizeof(Block)); + if (!s->frame_blocks || !s->key_blocks) { + av_log(s->avctx, AV_LOG_ERROR, "Memory allocation failed.\n"); + return AVERROR(ENOMEM); + } + s->blocks_size = s->rows * s->cols * sizeof(Block); + } + init_blocks(s, s->frame_blocks, s->encbuffer, s->databuffer); + init_blocks(s, s->key_blocks, s->keybuffer, 0); + + av_fast_malloc(&s->blockbuffer, &s->blockbuffer_size, block_width * block_height * 6); + if (!s->blockbuffer) { + av_log(s->avctx, AV_LOG_ERROR, "Could not allocate block buffer.\n"); + return AVERROR(ENOMEM); + } + return 0; +} + + static av_cold int flashsv2_encode_init(AVCodecContext * avctx) { FlashSV2Context *s = avctx->priv_data; @@ -211,39 +238,19 @@ static av_cold int flashsv2_encode_init(AVCodecContext * avctx) s->image_width = avctx->width; s->image_height = avctx->height; - s->block_width = (s->image_width / 12) & ~15; - s->block_height = (s->image_height / 12) & ~15; - - if(!s->block_width) - s->block_width = 1; - if(!s->block_height) - s->block_height = 1; - - s->rows = (s->image_height + s->block_height - 1) / s->block_height; - s->cols = (s->image_width + s->block_width - 1) / s->block_width; - s->frame_size = s->image_width * s->image_height * 3; - s->blocks_size = s->rows * s->cols * sizeof(Block); s->encbuffer = av_mallocz(s->frame_size); s->keybuffer = av_mallocz(s->frame_size); s->databuffer = av_mallocz(s->frame_size * 6); s->current_frame = av_mallocz(s->frame_size); s->key_frame = av_mallocz(s->frame_size); - s->frame_blocks = av_mallocz(s->blocks_size); - s->key_blocks = av_mallocz(s->blocks_size); if (!s->encbuffer || !s->keybuffer || !s->databuffer - || !s->current_frame || !s->key_frame || !s->key_blocks - || !s->frame_blocks) { + || !s->current_frame || !s->key_frame) { av_log(avctx, AV_LOG_ERROR, "Memory allocation failed.\n"); return AVERROR(ENOMEM); } - s->blockbuffer = NULL; - s->blockbuffer_size = 0; - - init_blocks(s, s->frame_blocks, s->encbuffer, s->databuffer); - init_blocks(s, s->key_blocks, s->keybuffer, 0); reset_stats(s); #ifndef FLASHSV2_DUMB s->total_bits = 1; @@ -252,7 +259,7 @@ static av_cold int flashsv2_encode_init(AVCodecContext * avctx) s->use_custom_palette = 0; s->palette_type = -1; // so that the palette will be generated in reconfigure_at_keyframe - return 0; + return update_block_dimensions(s, 64, 64); } static int new_key_frame(FlashSV2Context * s) @@ -800,29 +807,10 @@ static int reconfigure_at_keyframe(FlashSV2Context * s, const uint8_t * image, int block_width = optimum_block_width (s); int block_height = optimum_block_height(s); - s->rows = (s->image_height + block_height - 1) / block_height; - s->cols = (s->image_width + block_width - 1) / block_width; - if (block_width != s->block_width || block_height != s->block_height) { - s->block_width = block_width; - s->block_height = block_height; - if (s->rows * s->cols > s->blocks_size / sizeof(Block)) { - s->frame_blocks = av_realloc_array(s->frame_blocks, s->rows, s->cols * sizeof(Block)); - s->key_blocks = av_realloc_array(s->key_blocks, s->cols, s->rows * sizeof(Block)); - if (!s->frame_blocks || !s->key_blocks) { - av_log(s->avctx, AV_LOG_ERROR, "Memory allocation failed.\n"); - return -1; - } - s->blocks_size = s->rows * s->cols * sizeof(Block); - } - init_blocks(s, s->frame_blocks, s->encbuffer, s->databuffer); - init_blocks(s, s->key_blocks, s->keybuffer, 0); - - av_fast_malloc(&s->blockbuffer, &s->blockbuffer_size, block_width * block_height * 6); - if (!s->blockbuffer) { - av_log(s->avctx, AV_LOG_ERROR, "Could not allocate block buffer.\n"); - return AVERROR(ENOMEM); - } + res = update_block_dimensions(s, block_width, block_height); + if (res < 0) + return res; } s->use15_7 = optimum_use15_7(s);