1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-11-26 19:01:44 +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/internal.h"
#include "libavutil/intreadwrite.h" #include "libavutil/intreadwrite.h"
#include "avcodec.h" #include "avcodec.h"
#include "decode.h"
#include "internal.h" #include "internal.h"
@ -122,16 +123,7 @@ static int decode_frame(AVCodecContext *avctx, void *data,
} }
if (avctx->bits_per_coded_sample <= 8) { if (avctx->bits_per_coded_sample <= 8) {
buffer_size_t size; frame->palette_has_changed = ff_copy_palette(c->pal, avpkt, avctx);
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);
}
memcpy (frame->data[1], c->pal, AVPALETTE_SIZE); memcpy (frame->data[1], c->pal, AVPALETTE_SIZE);
} }

View File

@ -40,6 +40,7 @@
#include "libavutil/common.h" #include "libavutil/common.h"
#include "libavutil/intreadwrite.h" #include "libavutil/intreadwrite.h"
#include "avcodec.h" #include "avcodec.h"
#include "decode.h"
#include "internal.h" #include "internal.h"
@ -477,14 +478,7 @@ static int cinepak_decode_frame(AVCodecContext *avctx,
return ret; return ret;
if (s->palette_video) { if (s->palette_video) {
buffer_size_t size; s->frame->palette_has_changed = ff_copy_palette(s->pal, avpkt, avctx);
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);
}
} }
if ((ret = cinepak_decode(s)) < 0) { if ((ret = cinepak_decode(s)) < 0) {

View File

@ -2091,3 +2091,17 @@ FF_ENABLE_DEPRECATION_WARNINGS
return 0; 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); 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. * Perform decoder initialization and validation.
* Called when opening the decoder, before the AVCodec.init() call. * Called when opening the decoder, before the AVCodec.init() call.

View File

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

View File

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

View File

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

View File

@ -43,6 +43,7 @@
#define BITSTREAM_READER_LE #define BITSTREAM_READER_LE
#include "avcodec.h" #include "avcodec.h"
#include "bytestream.h" #include "bytestream.h"
#include "decode.h"
#include "get_bits.h" #include "get_bits.h"
#include "hpeldsp.h" #include "hpeldsp.h"
#include "internal.h" #include "internal.h"
@ -1317,14 +1318,7 @@ static int ipvideo_decode_frame(AVCodecContext *avctx,
return ret; return ret;
if (!s->is_16bpp) { if (!s->is_16bpp) {
buffer_size_t size; frame->palette_has_changed = ff_copy_palette(s->pal, avpkt, avctx);
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);
}
} }
switch (frame_format) { switch (frame_format) {

View File

@ -29,6 +29,7 @@
#include "avcodec.h" #include "avcodec.h"
#include "bytestream.h" #include "bytestream.h"
#include "decode.h"
#include "internal.h" #include "internal.h"
#include "libavutil/common.h" #include "libavutil/common.h"
@ -268,8 +269,6 @@ static int decode_frame(AVCodecContext * avctx, void *data, int *got_frame,
int i, ret; int i, ret;
int header; int header;
int blocksize; 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); 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; 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) { if (ctx->setpal) {
ctx->setpal = 0; ctx->setpal = 0;

View File

@ -33,6 +33,7 @@
#include <string.h> #include <string.h>
#include "avcodec.h" #include "avcodec.h"
#include "decode.h"
#include "internal.h" #include "internal.h"
#include "msrledec.h" #include "msrledec.h"
#include "libavutil/imgutils.h" #include "libavutil/imgutils.h"
@ -97,15 +98,8 @@ static int msrle_decode_frame(AVCodecContext *avctx,
return ret; return ret;
if (avctx->bits_per_coded_sample > 1 && avctx->bits_per_coded_sample <= 8) { if (avctx->bits_per_coded_sample > 1 && avctx->bits_per_coded_sample <= 8) {
buffer_size_t size; s->frame->palette_has_changed = ff_copy_palette(s->pal, avpkt, avctx);
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);
}
/* make the palette available */ /* make the palette available */
memcpy(s->frame->data[1], s->pal, AVPALETTE_SIZE); memcpy(s->frame->data[1], s->pal, AVPALETTE_SIZE);
} }

View File

@ -33,6 +33,7 @@
#include "libavutil/internal.h" #include "libavutil/internal.h"
#include "libavutil/intreadwrite.h" #include "libavutil/intreadwrite.h"
#include "avcodec.h" #include "avcodec.h"
#include "decode.h"
#include "internal.h" #include "internal.h"
#define PALETTE_COUNT 256 #define PALETTE_COUNT 256
@ -314,15 +315,7 @@ static int msvideo1_decode_frame(AVCodecContext *avctx,
return ret; return ret;
if (s->mode_8bit) { if (s->mode_8bit) {
buffer_size_t size; s->frame->palette_has_changed = ff_copy_palette(s->pal, avpkt, avctx);
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);
}
} }
if (s->mode_8bit) if (s->mode_8bit)

View File

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

View File

@ -540,15 +540,7 @@ static int qtrle_decode_frame(AVCodecContext *avctx,
} }
if(has_palette) { if(has_palette) {
buffer_size_t size; s->frame->palette_has_changed = ff_copy_palette(s->pal, avpkt, avctx);
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);
}
/* make the palette available on the way out */ /* make the palette available on the way out */
memcpy(s->frame->data[1], s->pal, AVPALETTE_SIZE); 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) { 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; 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) if (!context->palette)
context->palette = av_buffer_alloc(AVPALETTE_SIZE); context->palette = av_buffer_alloc(AVPALETTE_SIZE);
if (!context->palette) { if (!context->palette) {
@ -389,15 +381,14 @@ static int raw_decode(AVCodecContext *avctx, void *data, int *got_frame,
return ret; return ret;
} }
if (pal) { if (ff_copy_palette(context->palette->data, avpkt, avctx)) {
memcpy(context->palette->data, pal, AVPALETTE_SIZE);
frame->palette_has_changed = 1; frame->palette_has_changed = 1;
} else if (context->is_nut_pal8) { } else if (context->is_nut_pal8) {
int vid_size = avctx->width * avctx->height; int vid_size = avctx->width * avctx->height;
int pal_size = avpkt->size - vid_size; int pal_size = avpkt->size - vid_size;
if (avpkt->size > vid_size && pal_size <= AVPALETTE_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); memcpy(context->palette->data, pal, pal_size);
frame->palette_has_changed = 1; frame->palette_has_changed = 1;
} }

View File

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

View File

@ -35,6 +35,7 @@
#include "libavutil/intreadwrite.h" #include "libavutil/intreadwrite.h"
#include "avcodec.h" #include "avcodec.h"
#include "bytestream.h" #include "bytestream.h"
#include "decode.h"
#include "internal.h" #include "internal.h"
#define CPAIR 2 #define CPAIR 2
@ -435,8 +436,6 @@ static int smc_decode_frame(AVCodecContext *avctx,
const uint8_t *buf = avpkt->data; const uint8_t *buf = avpkt->data;
int buf_size = avpkt->size; int buf_size = avpkt->size;
SmcContext *s = avctx->priv_data; 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 ret;
int total_blocks = ((s->avctx->width + 3) / 4) * ((s->avctx->height + 3) / 4); 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) if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0)
return ret; return ret;
if (pal && pal_size == AVPALETTE_SIZE) { s->frame->palette_has_changed = ff_copy_palette(s->pal, avpkt, avctx);
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);
}
smc_decode_stream(s); smc_decode_stream(s);

View File

@ -38,6 +38,7 @@
#include <stdlib.h> #include <stdlib.h>
#include "avcodec.h" #include "avcodec.h"
#include "decode.h"
#include "internal.h" #include "internal.h"
#include "msrledec.h" #include "msrledec.h"
@ -72,15 +73,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
int palette_has_changed = 0; int palette_has_changed = 0;
if (c->avctx->pix_fmt == AV_PIX_FMT_PAL8) { if (c->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
buffer_size_t size; palette_has_changed = ff_copy_palette(c->pal, avpkt, avctx);
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);
}
} }
ret = inflateReset(&c->zstream); ret = inflateReset(&c->zstream);