History

avcodec/h274: fix hash verification on BE

Wu Jianhua committed 1 year ago

Commit 8c9a2f41 parent 77b9b941 file · +57 −15Browse files

Unified · Split

libavcodec/h274.c+57−15
@@ -27,6 +27,7 @@ · 26 unmodified lines
2727
2828 #include "libavutil/avassert.h"
2929 #include "libavutil/bswap.h"
30+#include "libavcodec/bswapdsp.h"
3031 #include "libavutil/crc.h"
3132 #include "libavutil/imgutils.h"
3233 #include "libavutil/md5.h"
@@ -795,15 +796,41 @@ static const int8_t R64T[64][64] = { · 762 unmodified lines
795796 }
796797 };
797798
798−static int verify_plane_md5(struct AVMD5 *ctx,
799− const uint8_t *src, const int w, const int h, const int stride,
800− const uint8_t *expected)
799+struct H274HashContext {
800+ int type;
801+ struct AVMD5 *ctx;
802+
803+#if HAVE_BIGENDIAN
804+ BswapDSPContext bdsp;
805+ uint8_t *buf;
806+ int buf_size;
807+#endif
808+};
809+
810+static av_always_inline void bswap16_buf_if_be(H274HashContext *s, const int ps, const uint8_t **src, const int w)
811+{
812+#if HAVE_BIGENDIAN
813+ if (ps) {
814+ s->bdsp.bswap16_buf((uint16_t *)s->buf,
815+ (const uint16_t *)*src, w);
816+ *src = s->buf;
817+ }
818+#endif
819+}
820+
821+static int verify_plane_md5(H274HashContext *s,
822+ const uint8_t *_src, const int w, const int h, const int stride,
823+ const int ps, const uint8_t *expected)
801824 {
802825 #define MD5_SIZE 16
826+ struct AVMD5 *ctx = s->ctx;
803827 uint8_t md5[MD5_SIZE];
828+
804829 av_md5_init(ctx);
805830 for (int j = 0; j < h; j++) {
806− av_md5_update(ctx, src, w);
831+ const uint8_t *src = &_src[j * stride];
832+ bswap16_buf_if_be(s, ps, &src, w);
833+ av_md5_update(ctx, src, w << ps);
807834 src += stride;
808835 }
809836 av_md5_final(ctx, md5);
@@ -814,15 +841,16 @@ static int verify_plane_md5(struct AVMD5 *ctx, · 4 unmodified lines
814841 return 0;
815842 }
816843
817−static int verify_plane_crc(const uint8_t *src, const int w, const int h, const int stride,
818− uint16_t expected)
844+static int verify_plane_crc(H274HashContext *s, const uint8_t *_src, const int w, const int h, const int stride,
845+ const int ps, uint16_t expected)
819846 {
820847 uint32_t crc = 0x0F1D; // CRC-16-CCITT-AUG
821848 const AVCRC *ctx = av_crc_get_table(AV_CRC_16_CCITT);
822849
823− expected = av_le2ne32(expected);
824850 for (int j = 0; j < h; j++) {
825− crc = av_crc(ctx, crc, src, w);
851+ const uint8_t *src = &_src[j * stride];
852+ bswap16_buf_if_be(s, ps, &src, w);
853+ crc = av_crc(ctx, crc, src, w << ps);
826854 src += stride;
827855 }
828856 crc = av_bswap16(crc);
@@ -863,11 +891,6 @@ enum { · 34 unmodified lines
863891 HASH_LAST = HASH_CHECKSUM,
864892 };
865893
866−struct H274HashContext {
867− int type;
868− struct AVMD5 *ctx;
869−};
870−
871894 void ff_h274_hash_freep(H274HashContext **ctx)
872895 {
873896 if (*ctx) {
@@ -875,6 +898,9 @@ void ff_h274_hash_freep(H274HashContext **ctx) · 1 unmodified line
875898 if (c->ctx)
876899 av_free(c->ctx);
877900 av_freep(ctx);
901+#if HAVE_BIGENDIAN
902+ av_freep(&c->buf);
903+#endif
878904 }
879905 }
880906
@@ -906,6 +932,10 @@ int ff_h274_hash_init(H274HashContext **ctx, const int type) · 25 unmodified lines
906932 return AVERROR(ENOMEM);
907933 }
908934
935+#if HAVE_BIGENDIAN
936+ ff_bswapdsp_init(&c->bdsp);
937+#endif
938+
909939 return 0;
910940 }
911941
@@ -932,10 +962,22 @@ int ff_h274_hash_verify(H274HashContext *c, const H274SEIPictureHash *hash, · 20 unmodified lines
932962 const uint8_t *src = frame->data[i];
933963 const int stride = frame->linesize[i];
934964
965+#if HAVE_BIGENDIAN
966+ if (c->type != HASH_CHECKSUM) {
967+ if (ps) {
968+ av_fast_malloc(&c->buf, &c->buf_size,
969+ FFMAX3(frame->linesize[0], frame->linesize[1],
970+ frame->linesize[2]));
971+ if (!c->buf)
972+ return AVERROR(ENOMEM);
973+ }
974+ }
975+#endif
976+
935977 if (c->type == HASH_MD5SUM)
936− err = verify_plane_md5(c->ctx, src, w << ps, h, stride, hash->md5[i]);
978+ err = verify_plane_md5(c, src, w, h, stride, ps, hash->md5[i]);
937979 else if (c->type == HASH_CRC)
938− err = verify_plane_crc(src, w << ps, h, stride, hash->crc[i]);
980+ err = verify_plane_crc(c, src, w, h, stride, ps, hash->crc[i]);
939981 else if (c->type == HASH_CHECKSUM)
940982 err = verify_plane_checksum(src, w, h, stride, ps, hash->checksum[i]);
941983 if (err < 0)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t