History

avcodec/h274: fix hash verification on BE

Wu Jianhua committed 1 year ago

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

Showing the changes to libavcodec (1 of 1 file). Show all

Unified · Split

libavcodec/h274.c+57−15
@@ -27,6 +27,7 @@ · 26 unmodified lines
27 27
28#include "libavutil/avassert.h"28#include "libavutil/avassert.h"
29#include "libavutil/bswap.h"29#include "libavutil/bswap.h"
30#include "libavcodec/bswapdsp.h"
30#include "libavutil/crc.h"31#include "libavutil/crc.h"
31#include "libavutil/imgutils.h"32#include "libavutil/imgutils.h"
32#include "libavutil/md5.h"33#include "libavutil/md5.h"
@@ -795,15 +796,41 @@ static const int8_t R64T[64][64] = { · 762 unmodified lines
795 }796 }
796};797};
797 798
798static int verify_plane_md5(struct AVMD5 *ctx,799struct H274HashContext {
799 const uint8_t *src, const int w, const int h, const int stride,800 int type;
800 const uint8_t *expected)801 struct AVMD5 *ctx;
802
803#if HAVE_BIGENDIAN
804 BswapDSPContext bdsp;
805 uint8_t *buf;
806 int buf_size;
807#endif
808};
809
810static 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
821static 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)
801{824{
802#define MD5_SIZE 16825#define MD5_SIZE 16
826 struct AVMD5 *ctx = s->ctx;
803 uint8_t md5[MD5_SIZE];827 uint8_t md5[MD5_SIZE];
828
804 av_md5_init(ctx);829 av_md5_init(ctx);
805 for (int j = 0; j < h; j++) {830 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);
807 src += stride;834 src += stride;
808 }835 }
809 av_md5_final(ctx, md5);836 av_md5_final(ctx, md5);
@@ -814,15 +841,16 @@ static int verify_plane_md5(struct AVMD5 *ctx, · 4 unmodified lines
814 return 0;841 return 0;
815}842}
816 843
817static int verify_plane_crc(const uint8_t *src, const int w, const int h, const int stride,844static int verify_plane_crc(H274HashContext *s, const uint8_t *_src, const int w, const int h, const int stride,
818 uint16_t expected)845 const int ps, uint16_t expected)
819{846{
820 uint32_t crc = 0x0F1D; // CRC-16-CCITT-AUG847 uint32_t crc = 0x0F1D; // CRC-16-CCITT-AUG
821 const AVCRC *ctx = av_crc_get_table(AV_CRC_16_CCITT);848 const AVCRC *ctx = av_crc_get_table(AV_CRC_16_CCITT);
822 849
823 expected = av_le2ne32(expected);
824 for (int j = 0; j < h; j++) {850 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);
826 src += stride;854 src += stride;
827 }855 }
828 crc = av_bswap16(crc);856 crc = av_bswap16(crc);
@@ -863,11 +891,6 @@ enum { · 34 unmodified lines
863 HASH_LAST = HASH_CHECKSUM,891 HASH_LAST = HASH_CHECKSUM,
864};892};
865 893
866struct H274HashContext {
867 int type;
868 struct AVMD5 *ctx;
869};
870
871void ff_h274_hash_freep(H274HashContext **ctx)894void ff_h274_hash_freep(H274HashContext **ctx)
872{895{
873 if (*ctx) {896 if (*ctx) {
@@ -875,6 +898,9 @@ void ff_h274_hash_freep(H274HashContext **ctx) · 1 unmodified line
875 if (c->ctx)898 if (c->ctx)
876 av_free(c->ctx);899 av_free(c->ctx);
877 av_freep(ctx);900 av_freep(ctx);
901#if HAVE_BIGENDIAN
902 av_freep(&c->buf);
903#endif
878 }904 }
879}905}
880 906
@@ -906,6 +932,10 @@ int ff_h274_hash_init(H274HashContext **ctx, const int type) · 25 unmodified lines
906 return AVERROR(ENOMEM);932 return AVERROR(ENOMEM);
907 }933 }
908 934
935#if HAVE_BIGENDIAN
936 ff_bswapdsp_init(&c->bdsp);
937#endif
938
909 return 0;939 return 0;
910}940}
911 941
@@ -932,10 +962,22 @@ int ff_h274_hash_verify(H274HashContext *c, const H274SEIPictureHash *hash, · 20 unmodified lines
932 const uint8_t *src = frame->data[i];962 const uint8_t *src = frame->data[i];
933 const int stride = frame->linesize[i];963 const int stride = frame->linesize[i];
934 964
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
935 if (c->type == HASH_MD5SUM)977 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]);
937 else if (c->type == HASH_CRC)979 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]);
939 else if (c->type == HASH_CHECKSUM)981 else if (c->type == HASH_CHECKSUM)
940 err = verify_plane_checksum(src, w, h, stride, ps, hash->checksum[i]);982 err = verify_plane_checksum(src, w, h, stride, ps, hash->checksum[i]);
941 if (err < 0)983 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