@@ -813,7 +813,7 @@ static int decode_i_frame(FourXContext *f, const uint8_t *buf, int length)
prestream_size);
if (!f->bitstream_buffer)
return AVERROR(ENOMEM);
- f->bbdsp.bswap_buf(f->bitstream_buffer, (const uint32_t *) prestream,
+ f->bbdsp.bswap_buf(f->bitstream_buffer, prestream,
prestream_size / 4);
init_get_bits(&f->pre_gb, f->bitstream_buffer, 8 * prestream_size);
@@ -1524,7 +1524,7 @@ static int ac3_decode_frame(AVCodecContext *avctx, AVFrame *frame,
// seems to be byte-swapped AC-3
int cnt = FFMIN(buf_size, AC3_FRAME_BUFFER_SIZE) >> 1;
s->bdsp.bswap16_buf((uint16_t *) s->input_buffer,
- (const uint16_t *) buf, cnt);
+ buf, cnt);
} else
memcpy(s->input_buffer, buf, FFMIN(buf_size, AC3_FRAME_BUFFER_SIZE));
@@ -1900,7 +1900,7 @@ static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
*dest++ = av_bswap16(src[sample]);
} else {
ctx->bdsp.bswap_buf((uint32_t *) ctx->crc_buffer,
- (uint32_t *) frame->data[0],
+ frame->data[0],
ctx->cur_frame_length * channels);
}
crc_source = ctx->crc_buffer;
@@ -1499,7 +1499,7 @@ static int ape_decode_frame(AVCodecContext *avctx, AVFrame *frame,
av_fast_padded_malloc(&s->data, &s->data_size, buf_size);
if (!s->data)
return AVERROR(ENOMEM);
- s->bdsp.bswap_buf((uint32_t *) s->data, (const uint32_t *) buf,
+ s->bdsp.bswap_buf((uint32_t *) s->data, buf,
buf_size >> 2);
memset(s->data + (buf_size & ~3), 0, buf_size & 3);
s->ptr = s->data;
@@ -254,7 +254,7 @@ static int decode_frame(AVCodecContext *avctx, AVFrame *p,
return AVERROR(ENOMEM);
c->bbdsp.bswap_buf((uint32_t *) a->bitstream_buffer,
- (const uint32_t *) buf, buf_size / 4);
+ buf, buf_size / 4);
ret = init_get_bits8(&a->gb, a->bitstream_buffer, buf_size);
} else {
ret = init_get_bits8_le(&a->gb, buf, buf_size);
@@ -310,7 +310,7 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
if (avctx->codec_id == AV_CODEC_ID_ASV1) {
c->bbdsp.bswap_buf((uint32_t *) pkt->data,
- (uint32_t *) pkt->data, size);
+ pkt->data, size);
}
pkt->size = size * 4;
@@ -20,30 +20,35 @@
#include "libavutil/attributes.h"
#include "libavutil/bswap.h"
+#include "libavutil/intreadwrite.h"
#include "bswapdsp.h"
-static void bswap_buf(uint32_t *dst, const uint32_t *src, int w)
+static void bswap_buf(uint32_t *dst, const void *src_, int w)
{
- int i;
+ const unsigned char *src = src_;
+ uint32_t *const end = dst + w, *end2 = dst + (w & ~7);
- for (i = 0; i + 8 <= w; i += 8) {
- dst[i + 0] = av_bswap32(src[i + 0]);
- dst[i + 1] = av_bswap32(src[i + 1]);
- dst[i + 2] = av_bswap32(src[i + 2]);
- dst[i + 3] = av_bswap32(src[i + 3]);
- dst[i + 4] = av_bswap32(src[i + 4]);
- dst[i + 5] = av_bswap32(src[i + 5]);
- dst[i + 6] = av_bswap32(src[i + 6]);
- dst[i + 7] = av_bswap32(src[i + 7]);
+ for (; dst < end2; dst += 8, src += 8 * 4) {
+ dst[0] = av_bswap32(AV_RN32(src));
+ dst[1] = av_bswap32(AV_RN32(src + 4));
+ dst[2] = av_bswap32(AV_RN32(src + 8));
+ dst[3] = av_bswap32(AV_RN32(src + 12));
+ dst[4] = av_bswap32(AV_RN32(src + 16));
+ dst[5] = av_bswap32(AV_RN32(src + 20));
+ dst[6] = av_bswap32(AV_RN32(src + 24));
+ dst[7] = av_bswap32(AV_RN32(src + 28));
}
- for (; i < w; i++)
- dst[i + 0] = av_bswap32(src[i + 0]);
+ for (; dst < end; dst++, src += 4)
+ dst[0] = av_bswap32(AV_RN32(src));
}
-static void bswap16_buf(uint16_t *dst, const uint16_t *src, int len)
+static void bswap16_buf(uint16_t *dst, const void *src_, int len)
{
- while (len--)
- *dst++ = av_bswap16(*src++);
+ const unsigned char *src = src_;
+ while (len--) {
+ *dst++ = av_bswap16(AV_RN16(src));
+ src += 2;
+ }
}
av_cold void ff_bswapdsp_init(BswapDSPContext *c)
@@ -22,8 +22,8 @@
#include <stdint.h>
typedef struct BswapDSPContext {
- void (*bswap_buf)(uint32_t *dst, const uint32_t *src, int w);
- void (*bswap16_buf)(uint16_t *dst, const uint16_t *src, int len);
+ void (*bswap_buf)(uint32_t *dst, const void *src, int w);
+ void (*bswap16_buf)(uint16_t *dst, const void *src, int len);
} BswapDSPContext;
void ff_bswapdsp_init(BswapDSPContext *c);
@@ -397,7 +397,7 @@ static int cllc_decode_frame(AVCodecContext *avctx, AVFrame *pic,
}
/* bswap16 the buffer since CLLC's bitreader works in 16-bit words */
- ctx->bdsp.bswap16_buf((uint16_t *) ctx->swapped_buf, (uint16_t *) src,
+ ctx->bdsp.bswap16_buf((uint16_t *) ctx->swapped_buf, src,
data_size / 2);
if ((ret = init_get_bits8(&gb, ctx->swapped_buf, data_size)) < 0)
@@ -304,7 +304,7 @@ static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
bytestream2_get_bytes_left(&gb));
if (!s->bitstream_buf)
return AVERROR(ENOMEM);
- s->bbdsp.bswap16_buf(s->bitstream_buf, (const uint16_t *)(buf + bytestream2_tell(&gb)),
+ s->bbdsp.bswap16_buf(s->bitstream_buf, buf + bytestream2_tell(&gb),
bytestream2_get_bytes_left(&gb) / 2);
memset((uint8_t*)s->bitstream_buf + bytestream2_get_bytes_left(&gb), 0, AV_INPUT_BUFFER_PADDING_SIZE);
init_get_bits(&s->gb, s->bitstream_buf, 8*(bytestream2_get_bytes_left(&gb)));
@@ -148,8 +148,7 @@ static int tqi_decode_frame(AVCodecContext *avctx, AVFrame *frame,
buf_end - buf);
if (!t->bitstream_buf)
return AVERROR(ENOMEM);
- t->bsdsp.bswap_buf(t->bitstream_buf, (const uint32_t *) buf,
- (buf_end - buf) / 4);
+ t->bsdsp.bswap_buf(t->bitstream_buf, buf, (buf_end - buf) / 4);
init_get_bits(&t->gb, t->bitstream_buf, 8 * (buf_end - buf));
t->last_dc[0] =
@@ -1587,7 +1587,7 @@ static int update_md5_sum(FlacEncodeContext *s, const void *samples)
buf = (const uint8_t *)samples;
#if HAVE_BIGENDIAN
s->bdsp.bswap16_buf((uint16_t *) s->md5_buffer,
- (const uint16_t *) samples, buf_size / 2);
+ samples, buf_size / 2);
buf = s->md5_buffer;
#endif
} else if (s->avctx->bits_per_raw_sample <= 24) {
@@ -106,8 +106,7 @@ static int fraps2_decode_plane(FrapsContext *s, uint8_t *dst, int stride, int w,
/* we have built Huffman table and are ready to decode plane */
/* convert bits so they may be used by standard bitreader */
- s->bdsp.bswap_buf((uint32_t *) s->tmpbuf,
- (const uint32_t *) src, size >> 2);
+ s->bdsp.bswap_buf((uint32_t *) s->tmpbuf, src, size >> 2);
if ((ret = init_get_bits8(&gb, s->tmpbuf, size)) < 0)
return ret;
@@ -3274,8 +3274,7 @@ static int verify_md5(HEVCContext *s, AVFrame *frame)
const uint8_t *src = frame->data[i] + j * frame->linesize[i];
#if HAVE_BIGENDIAN
if (pixel_shift) {
- s->bdsp.bswap16_buf((uint16_t *) s->checksum_buf,
- (const uint16_t *) src, w);
+ s->bdsp.bswap16_buf((uint16_t *) s->checksum_buf, src, w);
src = s->checksum_buf;
}
#endif
@@ -1240,7 +1240,7 @@ static int decode_frame(AVCodecContext *avctx, AVFrame *p,
return AVERROR(ENOMEM);
s->bdsp.bswap_buf((uint32_t *) s->bitstream_buffer,
- (const uint32_t *) buf, buf_size / 4);
+ buf, buf_size / 4);
if ((ret = ff_thread_get_buffer(avctx, p, 0)) < 0)
return ret;
@@ -1018,7 +1018,7 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
avctx->stats_out[0] = '\0';
if (!(s->avctx->flags2 & AV_CODEC_FLAG2_NO_OUTPUT)) {
flush_put_bits(&s->pb);
- s->bdsp.bswap_buf((uint32_t *) pkt->data, (uint32_t *) pkt->data, size);
+ s->bdsp.bswap_buf((uint32_t *) pkt->data, pkt->data, size);
}
s->picture_number++;
@@ -986,7 +986,7 @@ static int imc_decode_frame(AVCodecContext *avctx, AVFrame *frame,
for (i = 0; i < avctx->ch_layout.nb_channels; i++) {
q->out_samples = (float *)frame->extended_data[i];
- q->bdsp.bswap16_buf(buf16, (const uint16_t *) buf, IMC_BLOCK_SIZE / 2);
+ q->bdsp.bswap16_buf(buf16, buf, IMC_BLOCK_SIZE / 2);
init_get_bits(&q->gb, (const uint8_t*)buf16, IMC_BLOCK_SIZE * 8);
@@ -369,8 +369,7 @@ static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
return AVERROR(ENOMEM);
s->bdsp.bswap_buf((uint32_t *)s->bitstream,
- (uint32_t *)avpkt->data,
- (avpkt->size + 3) >> 2);
+ avpkt->data, (avpkt->size + 3) >> 2);
if ((ret = init_get_bits8(gb, s->bitstream, FFALIGN(avpkt->size, 4))) < 0)
return ret;
@@ -182,7 +182,7 @@ static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
av_fast_padded_malloc(&a->bitstream_buffer, &a->bitstream_buffer_size, buf_size);
if (!a->bitstream_buffer)
return AVERROR(ENOMEM);
- a->bbdsp.bswap16_buf((uint16_t *)a->bitstream_buffer, (uint16_t *)buf, (buf_size + 1) / 2);
+ a->bbdsp.bswap16_buf((uint16_t *)a->bitstream_buffer, buf, (buf_size + 1) / 2);
if ((ret = init_get_bits8(&a->gb, a->bitstream_buffer, buf_size)) < 0)
return ret;
@@ -412,7 +412,7 @@ static int mimic_decode_frame(AVCodecContext *avctx, AVFrame *rframe,
return AVERROR(ENOMEM);
ctx->bbdsp.bswap_buf(ctx->swap_buf,
- (const uint32_t *) (buf + MIMIC_HEADER_SIZE),
+ buf + MIMIC_HEADER_SIZE,
swap_buf_size >> 2);
init_get_bits(&ctx->gb, ctx->swap_buf, swap_buf_size << 3);
@@ -1226,8 +1226,7 @@ static int mobiclip_decode(AVCodecContext *avctx, AVFrame *rframe,
return ret;
s->bdsp.bswap16_buf((uint16_t *)s->bitstream,
- (uint16_t *)pkt->data,
- (pkt->size + 1) >> 1);
+ pkt->data, (pkt->size + 1) >> 1);
ret = init_get_bits8(gb, s->bitstream, FFALIGN(pkt->size, 2));
if (ret < 0)
@@ -294,8 +294,7 @@ static int mp_decode_frame(AVCodecContext *avctx, AVFrame *rframe,
av_fast_padded_malloc(&mp->bswapbuf, &mp->bswapbuf_size, buf_size);
if (!mp->bswapbuf)
return AVERROR(ENOMEM);
- mp->bdsp.bswap_buf((uint32_t *) mp->bswapbuf, (const uint32_t *) buf,
- buf_size / 4);
+ mp->bdsp.bswap_buf((uint32_t *) mp->bswapbuf, buf, buf_size / 4);
if (buf_size & 3)
memcpy(mp->bswapbuf + (buf_size & ~3), buf + (buf_size & ~3), buf_size & 3);
init_get_bits(&gb, mp->bswapbuf, buf_size * 8);
@@ -319,9 +319,9 @@ static int raw_decode(AVCodecContext *avctx, AVFrame *frame,
if (!context->bitstream_buf)
return AVERROR(ENOMEM);
if (swap == 16)
- context->bbdsp.bswap16_buf(context->bitstream_buf, (const uint16_t*)buf, buf_size / 2);
+ context->bbdsp.bswap16_buf(context->bitstream_buf, buf, buf_size / 2);
else if (swap == 32)
- context->bbdsp.bswap_buf(context->bitstream_buf, (const uint32_t*)buf, buf_size / 4);
+ context->bbdsp.bswap_buf(context->bitstream_buf, buf, buf_size / 4);
else
return AVERROR_INVALIDDATA;
buf = context->bitstream_buf;
@@ -25,9 +25,9 @@
#include "libavutil/cpu.h"
#include "libavcodec/bswapdsp.h"
-void ff_bswap32_buf_rvb(uint32_t *dst, const uint32_t *src, int len);
-void ff_bswap32_buf_rvv(uint32_t *dst, const uint32_t *src, int len);
-void ff_bswap16_buf_rvv(uint16_t *dst, const uint16_t *src, int len);
+void ff_bswap32_buf_rvb(uint32_t *dst, const void *src, int len);
+void ff_bswap32_buf_rvv(uint32_t *dst, const void *src, int len);
+void ff_bswap16_buf_rvv(uint16_t *dst, const void *src, int len);
av_cold void ff_bswapdsp_init_riscv(BswapDSPContext *c)
{
@@ -758,7 +758,7 @@ static int shorten_decode_frame(AVCodecContext *avctx, AVFrame *frame,
}
if (s->swap && s->internal_ftype != TYPE_U8)
s->bdsp.bswap16_buf(((uint16_t **)frame->extended_data)[chan],
- ((uint16_t **)frame->extended_data)[chan],
+ frame->extended_data[chan],
s->blocksize);
}
@@ -907,8 +907,7 @@ static int decode_frame(AVCodecContext *avctx, AVFrame *rframe,
if ((ret = ff_reget_buffer(avctx, p, 0)) < 0)
return ret;
- l->bdsp.bswap_buf((uint32_t *) l->buffer, (const uint32_t *) buf,
- buf_size >> 2);
+ l->bdsp.bswap_buf((uint32_t *) l->buffer, buf, buf_size >> 2);
if ((ret = tm2_read_header(l, l->buffer)) < 0) {
return ret;
@@ -82,7 +82,7 @@ static void truespeech_read_frame(TSContext *dec, const uint8_t *input)
{
GetBitContext gb;
- dec->bdsp.bswap_buf((uint32_t *) dec->buffer, (const uint32_t *) input, 8);
+ dec->bdsp.bswap_buf((uint32_t *) dec->buffer, input, 8);
init_get_bits(&gb, dec->buffer, 32 * 8);
dec->vector[7] = ts_codebook[7][get_bits(&gb, 3)];
@@ -151,7 +151,7 @@ static int decode_plane10(UtvideoContext *c, int plane_no,
memset(c->slice_bits + slice_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
c->bdsp.bswap_buf((uint32_t *) c->slice_bits,
- (uint32_t *)(src + slice_data_start + c->slices * 4),
+ src + slice_data_start + c->slices * 4,
(slice_data_end - slice_data_start + 3) >> 2);
init_get_bits(&gb, c->slice_bits, slice_size * 8);
@@ -491,8 +491,7 @@ static int encode_plane(AVCodecContext *avctx, const uint8_t *src,
/* Byteswap the written huffman codes */
c->bdsp.bswap_buf((uint32_t *) c->slice_bits,
- (uint32_t *) c->slice_bits,
- slice_len >> 2);
+ c->slice_bits, slice_len >> 2);
/* Write the offset to the stream */
bytestream2_put_le32(pb, offset);
@@ -99,7 +99,7 @@ SECTION .text
add r0, 16
%endmacro
-; void ff_bswap_buf(uint32_t *dst, const uint32_t *src, int w);
+; void ff_bswap_buf(uint32_t *dst, const void *src, int w);
%macro BSWAP32_BUF 0
%if cpuflag(ssse3)||cpuflag(avx2)
cglobal bswap32_buf, 3,4,3
@@ -23,9 +23,9 @@
#include "libavutil/x86/cpu.h"
#include "libavcodec/bswapdsp.h"
-void ff_bswap32_buf_sse2(uint32_t *dst, const uint32_t *src, int w);
-void ff_bswap32_buf_ssse3(uint32_t *dst, const uint32_t *src, int w);
-void ff_bswap32_buf_avx2(uint32_t *dst, const uint32_t *src, int w);
+void ff_bswap32_buf_sse2(uint32_t *dst, const void *src, int w);
+void ff_bswap32_buf_ssse3(uint32_t *dst, const void *src, int w);
+void ff_bswap32_buf_avx2(uint32_t *dst, const void *src, int w);
av_cold void ff_bswapdsp_init_x86(BswapDSPContext *c)
{
@@ -312,7 +312,7 @@ static int decode_frame(AVCodecContext *avctx, AVFrame *p,
memcpy(s->buffer, avpkt->data + toffset, boffset - toffset);
memset(s->buffer + boffset - toffset, 0, AV_INPUT_BUFFER_PADDING_SIZE);
s->bdsp.bswap_buf((uint32_t *) s->buffer,
- (uint32_t *) s->buffer,
+ s->buffer,
(boffset - toffset + 3) >> 2);
if ((ret = init_get_bits8(&gb, s->buffer, boffset - toffset)) < 0)
return ret;
src typically comes from AVPackets and AVPacket.data has no alignment requirement. This means that the casts to uint32_t* might be undefined behaviour (they are if the pointer is no suitably aligned) and that the C versions of the conversion functions presume too much alignment; I don't know whether the same is true for the riscv asm. Fix this by using const void* for src. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com> --- Would be nice if someone could actually check whether riscv presumes alignment. libavcodec/4xm.c | 2 +- libavcodec/ac3dec.c | 2 +- libavcodec/alsdec.c | 2 +- libavcodec/apedec.c | 2 +- libavcodec/asvdec.c | 2 +- libavcodec/asvenc.c | 2 +- libavcodec/bswapdsp.c | 37 ++++++++++++++++++-------------- libavcodec/bswapdsp.h | 4 ++-- libavcodec/cllc.c | 2 +- libavcodec/eamad.c | 2 +- libavcodec/eatqi.c | 3 +-- libavcodec/flacenc.c | 2 +- libavcodec/fraps.c | 3 +-- libavcodec/hevcdec.c | 3 +-- libavcodec/huffyuvdec.c | 2 +- libavcodec/huffyuvenc.c | 2 +- libavcodec/imc.c | 2 +- libavcodec/imm4.c | 3 +-- libavcodec/mdec.c | 2 +- libavcodec/mimic.c | 2 +- libavcodec/mobiclip.c | 3 +-- libavcodec/motionpixels.c | 3 +-- libavcodec/rawdec.c | 4 ++-- libavcodec/riscv/bswapdsp_init.c | 6 +++--- libavcodec/shorten.c | 2 +- libavcodec/truemotion2.c | 3 +-- libavcodec/truespeech.c | 2 +- libavcodec/utvideodec.c | 2 +- libavcodec/utvideoenc.c | 3 +-- libavcodec/x86/bswapdsp.asm | 2 +- libavcodec/x86/bswapdsp_init.c | 6 +++--- libavcodec/ylc.c | 2 +- 32 files changed, 58 insertions(+), 61 deletions(-)