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/intreadwrite.h"
26 #include "libavutil/opt.h"
31 const AVClass *avclass;
32 struct AVHashContext *hash;
37 static void hash_finish(struct AVFormatContext *s, char *buf)
39 struct HashContext *c = s->priv_data;
40 uint8_t hash[AV_HASH_MAX_SIZE];
41 int i, offset = strlen(buf);
42 int len = av_hash_get_size(c->hash);
43 av_assert0(len > 0 && len <= sizeof(hash));
44 av_hash_final(c->hash, hash);
45 for (i = 0; i < len; i++) {
46 snprintf(buf + offset, 3, "%02"PRIx8, hash[i]);
52 avio_write(s->pb, buf, strlen(buf));
56 #define OFFSET(x) offsetof(struct HashContext, x)
57 #define ENC AV_OPT_FLAG_ENCODING_PARAM
58 #if CONFIG_HASH_MUXER || CONFIG_FRAMEHASH_MUXER
59 static const AVOption hash_options[] = {
60 { "hash", "set hash to use", OFFSET(hash_name), AV_OPT_TYPE_STRING, {.str = "sha256"}, 0, 0, ENC },
61 { "format_version", "file format version", OFFSET(format_version), AV_OPT_TYPE_INT, {.i64 = 2}, 1, 2, ENC },
66 #if CONFIG_MD5_MUXER || CONFIG_FRAMEMD5_MUXER
67 static const AVOption md5_options[] = {
68 { "hash", "set hash to use", OFFSET(hash_name), AV_OPT_TYPE_STRING, {.str = "md5"}, 0, 0, ENC },
69 { "format_version", "file format version", OFFSET(format_version), AV_OPT_TYPE_INT, {.i64 = 2}, 1, 2, ENC },
74 #if CONFIG_HASH_MUXER || CONFIG_MD5_MUXER
75 static int hash_write_header(struct AVFormatContext *s)
77 struct HashContext *c = s->priv_data;
78 int res = av_hash_alloc(&c->hash, c->hash_name);
81 av_hash_init(c->hash);
85 static int hash_write_packet(struct AVFormatContext *s, AVPacket *pkt)
87 struct HashContext *c = s->priv_data;
88 av_hash_update(c->hash, pkt->data, pkt->size);
92 static int hash_write_trailer(struct AVFormatContext *s)
94 struct HashContext *c = s->priv_data;
96 av_strlcpy(buf, av_hash_get_name(c->hash), sizeof(buf) - 200);
97 av_strlcat(buf, "=", sizeof(buf) - 200);
101 av_hash_freep(&c->hash);
106 #if CONFIG_HASH_MUXER
107 static const AVClass hashenc_class = {
108 .class_name = "hash encoder class",
109 .item_name = av_default_item_name,
110 .option = hash_options,
111 .version = LIBAVUTIL_VERSION_INT,
114 AVOutputFormat ff_hash_muxer = {
116 .long_name = NULL_IF_CONFIG_SMALL("Hash testing"),
117 .priv_data_size = sizeof(struct HashContext),
118 .audio_codec = AV_CODEC_ID_PCM_S16LE,
119 .video_codec = AV_CODEC_ID_RAWVIDEO,
120 .write_header = hash_write_header,
121 .write_packet = hash_write_packet,
122 .write_trailer = hash_write_trailer,
123 .flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
125 .priv_class = &hashenc_class,
130 static const AVClass md5enc_class = {
131 .class_name = "MD5 encoder class",
132 .item_name = av_default_item_name,
133 .option = md5_options,
134 .version = LIBAVUTIL_VERSION_INT,
137 AVOutputFormat ff_md5_muxer = {
139 .long_name = NULL_IF_CONFIG_SMALL("MD5 testing"),
140 .priv_data_size = sizeof(struct HashContext),
141 .audio_codec = AV_CODEC_ID_PCM_S16LE,
142 .video_codec = AV_CODEC_ID_RAWVIDEO,
143 .write_header = hash_write_header,
144 .write_packet = hash_write_packet,
145 .write_trailer = hash_write_trailer,
146 .flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
148 .priv_class = &md5enc_class,
152 #if CONFIG_FRAMEHASH_MUXER || CONFIG_FRAMEMD5_MUXER
153 static void framehash_print_extradata(struct AVFormatContext *s)
157 for (i = 0; i < s->nb_streams; i++) {
158 AVStream *st = s->streams[i];
159 AVCodecParameters *par = st->codecpar;
160 if (par->extradata) {
161 struct HashContext *c = s->priv_data;
162 char buf[AV_HASH_MAX_SIZE*2+1];
164 avio_printf(s->pb, "#extradata %d, %31d, ", i, par->extradata_size);
165 av_hash_init(c->hash);
166 av_hash_update(c->hash, par->extradata, par->extradata_size);
167 av_hash_final_hex(c->hash, buf, sizeof(buf));
168 avio_write(s->pb, buf, strlen(buf));
169 avio_printf(s->pb, "\n");
174 static int framehash_write_header(struct AVFormatContext *s)
176 struct HashContext *c = s->priv_data;
177 int res = av_hash_alloc(&c->hash, c->hash_name);
180 avio_printf(s->pb, "#format: frame checksums\n");
181 avio_printf(s->pb, "#version: %d\n", c->format_version);
182 avio_printf(s->pb, "#hash: %s\n", av_hash_get_name(c->hash));
183 framehash_print_extradata(s);
184 ff_framehash_write_header(s);
185 avio_printf(s->pb, "#stream#, dts, pts, duration, size, hash\n");
189 static int framehash_write_packet(struct AVFormatContext *s, AVPacket *pkt)
191 struct HashContext *c = s->priv_data;
192 char buf[AV_HASH_MAX_SIZE*2+128];
194 av_hash_init(c->hash);
195 av_hash_update(c->hash, pkt->data, pkt->size);
197 snprintf(buf, sizeof(buf) - (AV_HASH_MAX_SIZE * 2 + 1), "%d, %10"PRId64", %10"PRId64", %8"PRId64", %8d, ",
198 pkt->stream_index, pkt->dts, pkt->pts, pkt->duration, pkt->size);
200 av_hash_final_hex(c->hash, buf + len, sizeof(buf) - len);
201 avio_write(s->pb, buf, strlen(buf));
203 if (c->format_version > 1 && pkt->side_data_elems) {
205 avio_printf(s->pb, ", S=%d", pkt->side_data_elems);
206 for (i = 0; i < pkt->side_data_elems; i++) {
207 av_hash_init(c->hash);
208 if (HAVE_BIGENDIAN && pkt->side_data[i].type == AV_PKT_DATA_PALETTE) {
209 for (j = 0; j < pkt->side_data[i].size; j += sizeof(uint32_t)) {
210 uint32_t data = AV_RL32(pkt->side_data[i].data + j);
211 av_hash_update(c->hash, (uint8_t *)&data, sizeof(uint32_t));
214 av_hash_update(c->hash, pkt->side_data[i].data, pkt->side_data[i].size);
215 snprintf(buf, sizeof(buf) - (AV_HASH_MAX_SIZE * 2 + 1), ", %8d, ", pkt->side_data[i].size);
217 av_hash_final_hex(c->hash, buf + len, sizeof(buf) - len);
218 avio_write(s->pb, buf, strlen(buf));
222 avio_printf(s->pb, "\n");
227 static int framehash_write_trailer(struct AVFormatContext *s)
229 struct HashContext *c = s->priv_data;
230 av_hash_freep(&c->hash);
235 #if CONFIG_FRAMEHASH_MUXER
236 static const AVClass framehash_class = {
237 .class_name = "frame hash encoder class",
238 .item_name = av_default_item_name,
239 .option = hash_options,
240 .version = LIBAVUTIL_VERSION_INT,
243 AVOutputFormat ff_framehash_muxer = {
245 .long_name = NULL_IF_CONFIG_SMALL("Per-frame hash testing"),
246 .priv_data_size = sizeof(struct HashContext),
247 .audio_codec = AV_CODEC_ID_PCM_S16LE,
248 .video_codec = AV_CODEC_ID_RAWVIDEO,
249 .write_header = framehash_write_header,
250 .write_packet = framehash_write_packet,
251 .write_trailer = framehash_write_trailer,
252 .flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
254 .priv_class = &framehash_class,
258 #if CONFIG_FRAMEMD5_MUXER
259 static const AVClass framemd5_class = {
260 .class_name = "frame hash encoder class",
261 .item_name = av_default_item_name,
262 .option = md5_options,
263 .version = LIBAVUTIL_VERSION_INT,
266 AVOutputFormat ff_framemd5_muxer = {
268 .long_name = NULL_IF_CONFIG_SMALL("Per-frame MD5 testing"),
269 .priv_data_size = sizeof(struct HashContext),
270 .audio_codec = AV_CODEC_ID_PCM_S16LE,
271 .video_codec = AV_CODEC_ID_RAWVIDEO,
272 .write_header = framehash_write_header,
273 .write_packet = framehash_write_packet,
274 .write_trailer = framehash_write_trailer,
275 .flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
277 .priv_class = &framemd5_class,