1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-11-21 10:55:51 +02:00

avcodec: Factor updating palette out

Because the properties of frames returned from ff_get/reget_buffer
are not reset at all, lots of returned frames had palette_has_changed
wrongly set to 1. This has been changed, too.

Reviewed-by: James Almer <jamrial@gmail.com>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
This commit is contained in:
Andreas Rheinhardt 2021-03-17 22:43:32 +01:00
parent ab5803553b
commit 7b10083933
17 changed files with 53 additions and 122 deletions

View File

@ -37,6 +37,7 @@
#include "libavutil/internal.h"
#include "libavutil/intreadwrite.h"
#include "avcodec.h"
#include "decode.h"
#include "internal.h"
@ -122,16 +123,7 @@ static int decode_frame(AVCodecContext *avctx, void *data,
}
if (avctx->bits_per_coded_sample <= 8) {
buffer_size_t size;
const uint8_t *pal = av_packet_get_side_data(avpkt,
AV_PKT_DATA_PALETTE,
&size);
if (pal && size == AVPALETTE_SIZE) {
frame->palette_has_changed = 1;
memcpy(c->pal, pal, AVPALETTE_SIZE);
} else if (pal) {
av_log(avctx, AV_LOG_ERROR, "Palette size %d is wrong\n", size);
}
frame->palette_has_changed = ff_copy_palette(c->pal, avpkt, avctx);
memcpy (frame->data[1], c->pal, AVPALETTE_SIZE);
}

View File

@ -40,6 +40,7 @@
#include "libavutil/common.h"
#include "libavutil/intreadwrite.h"
#include "avcodec.h"
#include "decode.h"
#include "internal.h"
@ -477,14 +478,7 @@ static int cinepak_decode_frame(AVCodecContext *avctx,
return ret;
if (s->palette_video) {
buffer_size_t size;
const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &size);
if (pal && size == AVPALETTE_SIZE) {
s->frame->palette_has_changed = 1;
memcpy(s->pal, pal, AVPALETTE_SIZE);
} else if (pal) {
av_log(avctx, AV_LOG_ERROR, "Palette size %d is wrong\n", size);
}
s->frame->palette_has_changed = ff_copy_palette(s->pal, avpkt, avctx);
}
if ((ret = cinepak_decode(s)) < 0) {

View File

@ -2091,3 +2091,17 @@ FF_ENABLE_DEPRECATION_WARNINGS
return 0;
}
int ff_copy_palette(void *dst, const AVPacket *src, void *logctx)
{
buffer_size_t size;
const void *pal = av_packet_get_side_data(src, AV_PKT_DATA_PALETTE, &size);
if (pal && size == AVPALETTE_SIZE) {
memcpy(dst, pal, AVPALETTE_SIZE);
return 1;
} else if (pal) {
av_log(logctx, AV_LOG_ERROR, "Palette size %d is wrong\n", size);
}
return 0;
}

View File

@ -79,6 +79,15 @@ int ff_decode_get_hw_frames_ctx(AVCodecContext *avctx,
int ff_attach_decode_data(AVFrame *frame);
/**
* Check whether the side-data of src contains a palette of
* size AVPALETTE_SIZE; if so, copy it to dst and return 1;
* else return 0.
* Also emit an error message upon encountering a palette
* with invalid size.
*/
int ff_copy_palette(void *dst, const AVPacket *src, void *logctx);
/**
* Perform decoder initialization and validation.
* Called when opening the decoder, before the AVCodec.init() call.

View File

@ -23,6 +23,7 @@
#include "libavutil/common.h"
#include "avcodec.h"
#include "bytestream.h"
#include "decode.h"
#include "internal.h"
typedef struct GDVContext {
@ -462,8 +463,6 @@ static int gdv_decode_frame(AVCodecContext *avctx, void *data,
PutByteContext *pb = &gdv->pb;
AVFrame *frame = data;
int ret, i;
buffer_size_t pal_size;
const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &pal_size);
int compression;
unsigned flags;
uint8_t *dst;
@ -479,8 +478,7 @@ static int gdv_decode_frame(AVCodecContext *avctx, void *data,
if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
return ret;
if (pal && pal_size == AVPALETTE_SIZE)
memcpy(gdv->pal, pal, AVPALETTE_SIZE);
ff_copy_palette(gdv->pal, avpkt, avctx);
if (compression < 2 && bytestream2_get_bytes_left(gb) < 256*3)
return AVERROR_INVALIDDATA;

View File

@ -49,6 +49,7 @@
#include <string.h>
#include "avcodec.h"
#include "decode.h"
#include "internal.h"
#include "libavutil/internal.h"
@ -214,8 +215,6 @@ static int idcin_decode_frame(AVCodecContext *avctx,
const uint8_t *buf = avpkt->data;
int buf_size = avpkt->size;
IdcinContext *s = avctx->priv_data;
buffer_size_t pal_size;
const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &pal_size);
AVFrame *frame = data;
int ret;
@ -228,12 +227,7 @@ static int idcin_decode_frame(AVCodecContext *avctx,
if (idcin_decode_vlcs(s, frame))
return AVERROR_INVALIDDATA;
if (pal && pal_size == AVPALETTE_SIZE) {
frame->palette_has_changed = 1;
memcpy(s->pal, pal, AVPALETTE_SIZE);
} else if (pal) {
av_log(avctx, AV_LOG_ERROR, "Palette size %d is wrong\n", pal_size);
}
frame->palette_has_changed = ff_copy_palette(s->pal, avpkt, avctx);
/* make the palette available on the way out */
memcpy(frame->data[1], s->pal, AVPALETTE_SIZE);

View File

@ -21,6 +21,7 @@
#include "libavutil/common.h"
#include "avcodec.h"
#include "bytestream.h"
#include "decode.h"
#include "internal.h"
typedef struct SimbiosisIMXContext {
@ -50,16 +51,13 @@ static int imx_decode_frame(AVCodecContext *avctx, void *data,
{
SimbiosisIMXContext *imx = avctx->priv_data;
int ret, x, y;
buffer_size_t pal_size;
const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &pal_size);
AVFrame *frame = imx->frame;
GetByteContext gb;
if ((ret = ff_reget_buffer(avctx, frame, 0)) < 0)
return ret;
if (pal && pal_size == AVPALETTE_SIZE) {
memcpy(imx->pal, pal, pal_size);
if (ff_copy_palette(imx->pal, avpkt, avctx)) {
frame->palette_has_changed = 1;
frame->key_frame = 1;
} else {

View File

@ -43,6 +43,7 @@
#define BITSTREAM_READER_LE
#include "avcodec.h"
#include "bytestream.h"
#include "decode.h"
#include "get_bits.h"
#include "hpeldsp.h"
#include "internal.h"
@ -1317,14 +1318,7 @@ static int ipvideo_decode_frame(AVCodecContext *avctx,
return ret;
if (!s->is_16bpp) {
buffer_size_t size;
const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &size);
if (pal && size == AVPALETTE_SIZE) {
frame->palette_has_changed = 1;
memcpy(s->pal, pal, AVPALETTE_SIZE);
} else if (pal) {
av_log(avctx, AV_LOG_ERROR, "Palette size %d is wrong\n", size);
}
frame->palette_has_changed = ff_copy_palette(s->pal, avpkt, avctx);
}
switch (frame_format) {

View File

@ -29,6 +29,7 @@
#include "avcodec.h"
#include "bytestream.h"
#include "decode.h"
#include "internal.h"
#include "libavutil/common.h"
@ -268,8 +269,6 @@ static int decode_frame(AVCodecContext * avctx, void *data, int *got_frame,
int i, ret;
int header;
int blocksize;
buffer_size_t pal_size;
const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &pal_size);
bytestream2_init(&ctx->g, avpkt->data, avpkt->size);
@ -304,12 +303,8 @@ static int decode_frame(AVCodecContext * avctx, void *data, int *got_frame,
}
}
if (pal && pal_size == AVPALETTE_SIZE) {
if (ff_copy_palette(ctx->pal, avpkt, avctx))
frame->palette_has_changed = 1;
memcpy(ctx->pal, pal, AVPALETTE_SIZE);
} else if (pal) {
av_log(avctx, AV_LOG_ERROR, "Palette size %d is wrong\n", pal_size);
}
if (ctx->setpal) {
ctx->setpal = 0;

View File

@ -33,6 +33,7 @@
#include <string.h>
#include "avcodec.h"
#include "decode.h"
#include "internal.h"
#include "msrledec.h"
#include "libavutil/imgutils.h"
@ -97,15 +98,8 @@ static int msrle_decode_frame(AVCodecContext *avctx,
return ret;
if (avctx->bits_per_coded_sample > 1 && avctx->bits_per_coded_sample <= 8) {
buffer_size_t size;
const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &size);
s->frame->palette_has_changed = ff_copy_palette(s->pal, avpkt, avctx);
if (pal && size == AVPALETTE_SIZE) {
s->frame->palette_has_changed = 1;
memcpy(s->pal, pal, AVPALETTE_SIZE);
} else if (pal) {
av_log(avctx, AV_LOG_ERROR, "Palette size %d is wrong\n", size);
}
/* make the palette available */
memcpy(s->frame->data[1], s->pal, AVPALETTE_SIZE);
}

View File

@ -33,6 +33,7 @@
#include "libavutil/internal.h"
#include "libavutil/intreadwrite.h"
#include "avcodec.h"
#include "decode.h"
#include "internal.h"
#define PALETTE_COUNT 256
@ -314,15 +315,7 @@ static int msvideo1_decode_frame(AVCodecContext *avctx,
return ret;
if (s->mode_8bit) {
buffer_size_t size;
const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &size);
if (pal && size == AVPALETTE_SIZE) {
memcpy(s->pal, pal, AVPALETTE_SIZE);
s->frame->palette_has_changed = 1;
} else if (pal) {
av_log(avctx, AV_LOG_ERROR, "Palette size %d is wrong\n", size);
}
s->frame->palette_has_changed = ff_copy_palette(s->pal, avpkt, avctx);
}
if (s->mode_8bit)

View File

@ -26,6 +26,7 @@
#include "avcodec.h"
#include "bytestream.h"
#include "decode.h"
#include "internal.h"
typedef struct QpegContext{
@ -274,8 +275,6 @@ static int decode_frame(AVCodecContext *avctx,
AVFrame * const ref = a->ref;
uint8_t* outdata;
int delta, intra, ret;
buffer_size_t pal_size;
const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &pal_size);
if (avpkt->size < 0x86) {
av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
@ -300,12 +299,7 @@ static int decode_frame(AVCodecContext *avctx,
}
/* make the palette available on the way out */
if (pal && pal_size == AVPALETTE_SIZE) {
p->palette_has_changed = 1;
memcpy(a->pal, pal, AVPALETTE_SIZE);
} else if (pal) {
av_log(avctx, AV_LOG_ERROR, "Palette size %d is wrong\n", pal_size);
}
p->palette_has_changed = ff_copy_palette(a->pal, avpkt, avctx);
memcpy(p->data[1], a->pal, AVPALETTE_SIZE);
av_frame_unref(ref);

View File

@ -540,15 +540,7 @@ static int qtrle_decode_frame(AVCodecContext *avctx,
}
if(has_palette) {
buffer_size_t size;
const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &size);
if (pal && size == AVPALETTE_SIZE) {
s->frame->palette_has_changed = 1;
memcpy(s->pal, pal, AVPALETTE_SIZE);
} else if (pal) {
av_log(avctx, AV_LOG_ERROR, "Palette size %d is wrong\n", size);
}
s->frame->palette_has_changed = ff_copy_palette(s->pal, avpkt, avctx);
/* make the palette available on the way out */
memcpy(s->frame->data[1], s->pal, AVPALETTE_SIZE);

View File

@ -367,16 +367,8 @@ static int raw_decode(AVCodecContext *avctx, void *data, int *got_frame,
}
if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
buffer_size_t pal_size;
const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE,
&pal_size);
int ret;
if (pal && pal_size != AVPALETTE_SIZE) {
av_log(avctx, AV_LOG_ERROR, "Palette size %d is wrong\n", pal_size);
pal = NULL;
}
if (!context->palette)
context->palette = av_buffer_alloc(AVPALETTE_SIZE);
if (!context->palette) {
@ -389,15 +381,14 @@ static int raw_decode(AVCodecContext *avctx, void *data, int *got_frame,
return ret;
}
if (pal) {
memcpy(context->palette->data, pal, AVPALETTE_SIZE);
if (ff_copy_palette(context->palette->data, avpkt, avctx)) {
frame->palette_has_changed = 1;
} else if (context->is_nut_pal8) {
int vid_size = avctx->width * avctx->height;
int pal_size = avpkt->size - vid_size;
if (avpkt->size > vid_size && pal_size <= AVPALETTE_SIZE) {
pal = avpkt->data + vid_size;
const uint8_t *pal = avpkt->data + vid_size;
memcpy(context->palette->data, pal, pal_size);
frame->palette_has_changed = 1;
}

View File

@ -43,6 +43,7 @@
#include "avcodec.h"
#include "bytestream.h"
#include "decode.h"
#include "internal.h"
#define TILE_SIZE 8
@ -346,17 +347,8 @@ static int rscc_decode_frame(AVCodecContext *avctx, void *data,
/* Palette handling */
if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
buffer_size_t size;
const uint8_t *palette = av_packet_get_side_data(avpkt,
AV_PKT_DATA_PALETTE,
&size);
if (palette && size == AVPALETTE_SIZE) {
frame->palette_has_changed = 1;
memcpy(ctx->palette, palette, AVPALETTE_SIZE);
} else if (palette) {
av_log(avctx, AV_LOG_ERROR, "Palette size %d is wrong\n", size);
}
memcpy (frame->data[1], ctx->palette, AVPALETTE_SIZE);
frame->palette_has_changed = ff_copy_palette(ctx->palette, avpkt, avctx);
memcpy(frame->data[1], ctx->palette, AVPALETTE_SIZE);
}
// We only return a picture when enough of it is undamaged, this avoids copying nearly broken frames around
if (ctx->valid_pixels < ctx->inflated_size)

View File

@ -35,6 +35,7 @@
#include "libavutil/intreadwrite.h"
#include "avcodec.h"
#include "bytestream.h"
#include "decode.h"
#include "internal.h"
#define CPAIR 2
@ -435,8 +436,6 @@ static int smc_decode_frame(AVCodecContext *avctx,
const uint8_t *buf = avpkt->data;
int buf_size = avpkt->size;
SmcContext *s = avctx->priv_data;
buffer_size_t pal_size;
const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &pal_size);
int ret;
int total_blocks = ((s->avctx->width + 3) / 4) * ((s->avctx->height + 3) / 4);
@ -448,12 +447,7 @@ static int smc_decode_frame(AVCodecContext *avctx,
if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0)
return ret;
if (pal && pal_size == AVPALETTE_SIZE) {
s->frame->palette_has_changed = 1;
memcpy(s->pal, pal, AVPALETTE_SIZE);
} else if (pal) {
av_log(avctx, AV_LOG_ERROR, "Palette size %d is wrong\n", pal_size);
}
s->frame->palette_has_changed = ff_copy_palette(s->pal, avpkt, avctx);
smc_decode_stream(s);

View File

@ -38,6 +38,7 @@
#include <stdlib.h>
#include "avcodec.h"
#include "decode.h"
#include "internal.h"
#include "msrledec.h"
@ -72,15 +73,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
int palette_has_changed = 0;
if (c->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
buffer_size_t size;
const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &size);
if (pal && size == AVPALETTE_SIZE) {
palette_has_changed = 1;
memcpy(c->pal, pal, AVPALETTE_SIZE);
} else if (pal) {
av_log(avctx, AV_LOG_ERROR, "Palette size %d is wrong\n", size);
}
palette_has_changed = ff_copy_palette(c->pal, avpkt, avctx);
}
ret = inflateReset(&c->zstream);