2 * Hash/MD5 encoder (for codec/format testing)
3 * Copyright (c) 2009 Reimar Döffinger, based on crcenc (c) 2002 Fabrice Bellard
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/avassert.h"
23 #include "libavutil/avstring.h"
24 #include "libavutil/hash.h"
25 #include "libavutil/opt.h"
30 const AVClass *avclass;
31 struct AVHashContext *hash;
36 static void hash_finish(struct AVFormatContext *s, char *buf)
38 struct HashContext *c = s->priv_data;
39 uint8_t hash[AV_HASH_MAX_SIZE];
40 int i, offset = strlen(buf);
41 int len = av_hash_get_size(c->hash);
42 av_assert0(len > 0 && len <= sizeof(hash));
43 av_hash_final(c->hash, hash);
44 for (i = 0; i < len; i++) {
45 snprintf(buf + offset, 3, "%02"PRIx8, hash[i]);
51 avio_write(s->pb, buf, strlen(buf));
55 #define OFFSET(x) offsetof(struct HashContext, x)
56 #define ENC AV_OPT_FLAG_ENCODING_PARAM
57 #if CONFIG_HASH_MUXER || CONFIG_FRAMEHASH_MUXER
58 static const AVOption hash_options[] = {
59 { "hash", "set hash to use", OFFSET(hash_name), AV_OPT_TYPE_STRING, {.str = "sha256"}, 0, 0, ENC },
60 { "format_version", "file format version", OFFSET(format_version), AV_OPT_TYPE_INT, {.i64 = 1}, 1, 2, ENC },
65 #if CONFIG_MD5_MUXER || CONFIG_FRAMEMD5_MUXER
66 static const AVOption md5_options[] = {
67 { "hash", "set hash to use", OFFSET(hash_name), AV_OPT_TYPE_STRING, {.str = "md5"}, 0, 0, ENC },
68 { "format_version", "file format version", OFFSET(format_version), AV_OPT_TYPE_INT, {.i64 = 1}, 1, 2, ENC },
73 #if CONFIG_HASH_MUXER || CONFIG_MD5_MUXER
74 static int hash_write_header(struct AVFormatContext *s)
76 struct HashContext *c = s->priv_data;
77 int res = av_hash_alloc(&c->hash, c->hash_name);
80 av_hash_init(c->hash);
84 static int hash_write_packet(struct AVFormatContext *s, AVPacket *pkt)
86 struct HashContext *c = s->priv_data;
87 av_hash_update(c->hash, pkt->data, pkt->size);
91 static int hash_write_trailer(struct AVFormatContext *s)
93 struct HashContext *c = s->priv_data;
95 av_strlcpy(buf, av_hash_get_name(c->hash), sizeof(buf) - 200);
96 av_strlcat(buf, "=", sizeof(buf) - 200);
100 av_hash_freep(&c->hash);
105 #if CONFIG_HASH_MUXER
106 static const AVClass hashenc_class = {
107 .class_name = "hash encoder class",
108 .item_name = av_default_item_name,
109 .option = hash_options,
110 .version = LIBAVUTIL_VERSION_INT,
113 AVOutputFormat ff_hash_muxer = {
115 .long_name = NULL_IF_CONFIG_SMALL("Hash testing"),
116 .priv_data_size = sizeof(struct HashContext),
117 .audio_codec = AV_CODEC_ID_PCM_S16LE,
118 .video_codec = AV_CODEC_ID_RAWVIDEO,
119 .write_header = hash_write_header,
120 .write_packet = hash_write_packet,
121 .write_trailer = hash_write_trailer,
122 .flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
124 .priv_class = &hashenc_class,
129 static const AVClass md5enc_class = {
130 .class_name = "MD5 encoder class",
131 .item_name = av_default_item_name,
132 .option = md5_options,
133 .version = LIBAVUTIL_VERSION_INT,
136 AVOutputFormat ff_md5_muxer = {
138 .long_name = NULL_IF_CONFIG_SMALL("MD5 testing"),
139 .priv_data_size = sizeof(struct HashContext),
140 .audio_codec = AV_CODEC_ID_PCM_S16LE,
141 .video_codec = AV_CODEC_ID_RAWVIDEO,
142 .write_header = hash_write_header,
143 .write_packet = hash_write_packet,
144 .write_trailer = hash_write_trailer,
145 .flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
147 .priv_class = &md5enc_class,
151 #if CONFIG_FRAMEHASH_MUXER || CONFIG_FRAMEMD5_MUXER
152 static int framehash_write_header(struct AVFormatContext *s)
154 struct HashContext *c = s->priv_data;
155 int res = av_hash_alloc(&c->hash, c->hash_name);
158 avio_printf(s->pb, "#format: frame checksums\n");
159 avio_printf(s->pb, "#version: %d\n", c->format_version);
160 avio_printf(s->pb, "#hash: %s\n", av_hash_get_name(c->hash));
161 ff_framehash_write_header(s, c->format_version);
162 avio_printf(s->pb, "#stream#, dts, pts, duration, size, hash\n");
166 static int framehash_write_packet(struct AVFormatContext *s, AVPacket *pkt)
168 struct HashContext *c = s->priv_data;
170 av_hash_init(c->hash);
171 av_hash_update(c->hash, pkt->data, pkt->size);
173 snprintf(buf, sizeof(buf) - 64, "%d, %10"PRId64", %10"PRId64", %8"PRId64", %8d, ",
174 pkt->stream_index, pkt->dts, pkt->pts, pkt->duration, pkt->size);
179 static int framehash_write_trailer(struct AVFormatContext *s)
181 struct HashContext *c = s->priv_data;
182 av_hash_freep(&c->hash);
187 #if CONFIG_FRAMEHASH_MUXER
188 static const AVClass framehash_class = {
189 .class_name = "frame hash encoder class",
190 .item_name = av_default_item_name,
191 .option = hash_options,
192 .version = LIBAVUTIL_VERSION_INT,
195 AVOutputFormat ff_framehash_muxer = {
197 .long_name = NULL_IF_CONFIG_SMALL("Per-frame hash testing"),
198 .priv_data_size = sizeof(struct HashContext),
199 .audio_codec = AV_CODEC_ID_PCM_S16LE,
200 .video_codec = AV_CODEC_ID_RAWVIDEO,
201 .write_header = framehash_write_header,
202 .write_packet = framehash_write_packet,
203 .write_trailer = framehash_write_trailer,
204 .flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
206 .priv_class = &framehash_class,
210 #if CONFIG_FRAMEMD5_MUXER
211 static const AVClass framemd5_class = {
212 .class_name = "frame hash encoder class",
213 .item_name = av_default_item_name,
214 .option = md5_options,
215 .version = LIBAVUTIL_VERSION_INT,
218 AVOutputFormat ff_framemd5_muxer = {
220 .long_name = NULL_IF_CONFIG_SMALL("Per-frame MD5 testing"),
221 .priv_data_size = sizeof(struct HashContext),
222 .audio_codec = AV_CODEC_ID_PCM_S16LE,
223 .video_codec = AV_CODEC_ID_RAWVIDEO,
224 .write_header = framehash_write_header,
225 .write_packet = framehash_write_packet,
226 .write_trailer = framehash_write_trailer,
227 .flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
229 .priv_class = &framemd5_class,