Skip to content

Instantly share code, notes, and snippets.

@yannick
Created March 14, 2010 20:06
Show Gist options
  • Save yannick/332192 to your computer and use it in GitHub Desktop.
Save yannick/332192 to your computer and use it in GitHub Desktop.
From c20276eddba27e17006385ed8c65974c1764642d Mon Sep 17 00:00:00 2001
From: Yannick Kechlin <[email protected]
Date: Sun, 14 Mar 2010 17:03:24 +0100
Subject: [PATCH] sha1 checksum for media data analogous to crc
Signed-off-by: Yannick Kechlin <[email protected]>
---
libavformat/Makefile | 1 +
libavformat/allformats.c | 1 +
libavformat/shaenc.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 85 insertions(+), 0 deletions(-)
create mode 100755 libavformat/shaenc.c
diff --git a/libavformat/Makefile b/libavformat/Makefile
index dcf4ed8..dd29756 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -212,6 +212,7 @@ OBJS-$(CONFIG_RTP_MUXER) += rtp.o \
avc.o
OBJS-$(CONFIG_RTSP_DEMUXER) += rtsp.o
OBJS-$(CONFIG_RTSP_MUXER) += rtsp.o rtspenc.o
+OBJS-$(CONFIG_SHA_MUXER) += shaenc.o
OBJS-$(CONFIG_SDP_DEMUXER) += rtsp.o \
rdt.o \
rtp.o \
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index 090d134..2a464fb 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -173,6 +173,7 @@ void av_register_all(void)
REGISTER_DEMUXER (RPL, rpl);
REGISTER_MUXER (RTP, rtp);
REGISTER_MUXDEMUX (RTSP, rtsp);
+ REGISTER_MUXER (SHA, sha);
REGISTER_DEMUXER (SDP, sdp);
#if CONFIG_SDP_DEMUXER
av_register_rtp_dynamic_payload_handlers();
diff --git a/libavformat/shaenc.c b/libavformat/shaenc.c
new file mode 100755
index 0000000..b14d4b2
--- /dev/null
+++ b/libavformat/shaenc.c
@@ -0,0 +1,83 @@
+/*
+ * CRC encoder (for codec/format testing)
+ * Copyright (c) 2002 Fabrice Bellard
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "libavutil/sha.h"
+#include "avformat.h"
+
+typedef struct CRCState {
+ uint32_t crcval;
+} CRCState;
+
+struct AVSHA *avshactx;
+
+
+static int sha_write_header(struct AVFormatContext *s)
+{
+
+ avshactx = av_mallocz(av_sha_size);
+ av_sha_init(avshactx, 160);
+
+ CRCState *crc = s->priv_data;
+
+ /* init CRC */
+ crc->crcval = 1;
+
+ return 0;
+}
+
+static int sha_write_packet(struct AVFormatContext *s, AVPacket *pkt)
+{
+ CRCState *crc = s->priv_data;
+ av_sha_update(avshactx,pkt->data, pkt->size);
+ return 0;
+}
+
+static int sha_write_trailer(struct AVFormatContext *s)
+{
+ CRCState *crc = s->priv_data;
+ char buf[64];
+ unsigned char avsha1[20];
+
+ av_sha_final(avshactx, &avsha1);
+
+ snprintf(buf, sizeof(buf), "\nSHA=");
+ int i;
+ for (i = 0; i < 20; i++) {
+ snprintf(buf+5+(i*2), sizeof(buf), "%02x", avsha1[i]);
+ }
+// snprintf(buf+46, sizeof(buf), "\n");
+ put_buffer(s->pb, buf, strlen(buf));
+ put_flush_packet(s->pb);
+ return 0;
+}
+
+AVOutputFormat sha_muxer = {
+ "sha",
+ NULL_IF_CONFIG_SMALL("SHA testing format"),
+ NULL,
+ "",
+ sizeof(CRCState),
+ CODEC_ID_PCM_S16LE,
+ CODEC_ID_RAWVIDEO,
+ sha_write_header,
+ sha_write_packet,
+ sha_write_trailer,
+};
--
1.7.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment