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 #define OFFSET(x) offsetof(struct HashContext, x)
38 #define ENC AV_OPT_FLAG_ENCODING_PARAM
39 #if CONFIG_HASH_MUXER || CONFIG_FRAMEHASH_MUXER
40 static const AVOption hash_options[] = {
41 { "hash", "set hash to use", OFFSET(hash_name), AV_OPT_TYPE_STRING, {.str = "sha256"}, 0, 0, ENC },
42 { "format_version", "file format version", OFFSET(format_version), AV_OPT_TYPE_INT, {.i64 = 2}, 1, 2, ENC },
47 #if CONFIG_MD5_MUXER || CONFIG_FRAMEMD5_MUXER
48 static const AVOption md5_options[] = {
49 { "hash", "set hash to use", OFFSET(hash_name), AV_OPT_TYPE_STRING, {.str = "md5"}, 0, 0, ENC },
50 { "format_version", "file format version", OFFSET(format_version), AV_OPT_TYPE_INT, {.i64 = 2}, 1, 2, ENC },
55 #if CONFIG_HASH_MUXER || CONFIG_MD5_MUXER
56 static int hash_write_header(struct AVFormatContext *s)
58 struct HashContext *c = s->priv_data;
59 int res = av_hash_alloc(&c->hash, c->hash_name);
62 av_hash_init(c->hash);
66 static int hash_write_packet(struct AVFormatContext *s, AVPacket *pkt)
68 struct HashContext *c = s->priv_data;
69 av_hash_update(c->hash, pkt->data, pkt->size);
73 static int hash_write_trailer(struct AVFormatContext *s)
75 struct HashContext *c = s->priv_data;
76 char buf[AV_HASH_MAX_SIZE*2+128];
77 snprintf(buf, sizeof(buf) - 200, "%s=", av_hash_get_name(c->hash));
79 av_hash_final_hex(c->hash, buf + strlen(buf), sizeof(buf) - strlen(buf));
80 av_strlcatf(buf, sizeof(buf), "\n");
81 avio_write(s->pb, buf, strlen(buf));
84 av_hash_freep(&c->hash);
90 static const AVClass hashenc_class = {
91 .class_name = "hash encoder class",
92 .item_name = av_default_item_name,
93 .option = hash_options,
94 .version = LIBAVUTIL_VERSION_INT,
97 AVOutputFormat ff_hash_muxer = {
99 .long_name = NULL_IF_CONFIG_SMALL("Hash testing"),
100 .priv_data_size = sizeof(struct HashContext),
101 .audio_codec = AV_CODEC_ID_PCM_S16LE,
102 .video_codec = AV_CODEC_ID_RAWVIDEO,
103 .write_header = hash_write_header,
104 .write_packet = hash_write_packet,
105 .write_trailer = hash_write_trailer,
106 .flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
108 .priv_class = &hashenc_class,
113 static const AVClass md5enc_class = {
114 .class_name = "MD5 encoder class",
115 .item_name = av_default_item_name,
116 .option = md5_options,
117 .version = LIBAVUTIL_VERSION_INT,
120 AVOutputFormat ff_md5_muxer = {
122 .long_name = NULL_IF_CONFIG_SMALL("MD5 testing"),
123 .priv_data_size = sizeof(struct HashContext),
124 .audio_codec = AV_CODEC_ID_PCM_S16LE,
125 .video_codec = AV_CODEC_ID_RAWVIDEO,
126 .write_header = hash_write_header,
127 .write_packet = hash_write_packet,
128 .write_trailer = hash_write_trailer,
129 .flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
131 .priv_class = &md5enc_class,
135 #if CONFIG_FRAMEHASH_MUXER || CONFIG_FRAMEMD5_MUXER
136 static void framehash_print_extradata(struct AVFormatContext *s)
140 for (i = 0; i < s->nb_streams; i++) {
141 AVStream *st = s->streams[i];
142 AVCodecParameters *par = st->codecpar;
143 if (par->extradata) {
144 struct HashContext *c = s->priv_data;
145 char buf[AV_HASH_MAX_SIZE*2+1];
147 avio_printf(s->pb, "#extradata %d, %31d, ", i, par->extradata_size);
148 av_hash_init(c->hash);
149 av_hash_update(c->hash, par->extradata, par->extradata_size);
150 av_hash_final_hex(c->hash, buf, sizeof(buf));
151 avio_write(s->pb, buf, strlen(buf));
152 avio_printf(s->pb, "\n");
157 static int framehash_write_header(struct AVFormatContext *s)
159 struct HashContext *c = s->priv_data;
160 int res = av_hash_alloc(&c->hash, c->hash_name);
163 avio_printf(s->pb, "#format: frame checksums\n");
164 avio_printf(s->pb, "#version: %d\n", c->format_version);
165 avio_printf(s->pb, "#hash: %s\n", av_hash_get_name(c->hash));
166 framehash_print_extradata(s);
167 ff_framehash_write_header(s);
168 avio_printf(s->pb, "#stream#, dts, pts, duration, size, hash\n");
172 static int framehash_write_packet(struct AVFormatContext *s, AVPacket *pkt)
174 struct HashContext *c = s->priv_data;
175 char buf[AV_HASH_MAX_SIZE*2+128];
177 av_hash_init(c->hash);
178 av_hash_update(c->hash, pkt->data, pkt->size);
180 snprintf(buf, sizeof(buf) - (AV_HASH_MAX_SIZE * 2 + 1), "%d, %10"PRId64", %10"PRId64", %8"PRId64", %8d, ",
181 pkt->stream_index, pkt->dts, pkt->pts, pkt->duration, pkt->size);
183 av_hash_final_hex(c->hash, buf + len, sizeof(buf) - len);
184 avio_write(s->pb, buf, strlen(buf));
186 if (c->format_version > 1 && pkt->side_data_elems) {
188 avio_printf(s->pb, ", S=%d", pkt->side_data_elems);
189 for (i = 0; i < pkt->side_data_elems; i++) {
190 av_hash_init(c->hash);
191 if (HAVE_BIGENDIAN && pkt->side_data[i].type == AV_PKT_DATA_PALETTE) {
192 for (j = 0; j < pkt->side_data[i].size; j += sizeof(uint32_t)) {
193 uint32_t data = AV_RL32(pkt->side_data[i].data + j);
194 av_hash_update(c->hash, (uint8_t *)&data, sizeof(uint32_t));
197 av_hash_update(c->hash, pkt->side_data[i].data, pkt->side_data[i].size);
198 snprintf(buf, sizeof(buf) - (AV_HASH_MAX_SIZE * 2 + 1), ", %8d, ", pkt->side_data[i].size);
200 av_hash_final_hex(c->hash, buf + len, sizeof(buf) - len);
201 avio_write(s->pb, buf, strlen(buf));
205 avio_printf(s->pb, "\n");
210 static int framehash_write_trailer(struct AVFormatContext *s)
212 struct HashContext *c = s->priv_data;
213 av_hash_freep(&c->hash);
218 #if CONFIG_FRAMEHASH_MUXER
219 static const AVClass framehash_class = {
220 .class_name = "frame hash encoder class",
221 .item_name = av_default_item_name,
222 .option = hash_options,
223 .version = LIBAVUTIL_VERSION_INT,
226 AVOutputFormat ff_framehash_muxer = {
228 .long_name = NULL_IF_CONFIG_SMALL("Per-frame hash testing"),
229 .priv_data_size = sizeof(struct HashContext),
230 .audio_codec = AV_CODEC_ID_PCM_S16LE,
231 .video_codec = AV_CODEC_ID_RAWVIDEO,
232 .write_header = framehash_write_header,
233 .write_packet = framehash_write_packet,
234 .write_trailer = framehash_write_trailer,
235 .flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
237 .priv_class = &framehash_class,
241 #if CONFIG_FRAMEMD5_MUXER
242 static const AVClass framemd5_class = {
243 .class_name = "frame hash encoder class",
244 .item_name = av_default_item_name,
245 .option = md5_options,
246 .version = LIBAVUTIL_VERSION_INT,
249 AVOutputFormat ff_framemd5_muxer = {
251 .long_name = NULL_IF_CONFIG_SMALL("Per-frame MD5 testing"),
252 .priv_data_size = sizeof(struct HashContext),
253 .audio_codec = AV_CODEC_ID_PCM_S16LE,
254 .video_codec = AV_CODEC_ID_RAWVIDEO,
255 .write_header = framehash_write_header,
256 .write_packet = framehash_write_packet,
257 .write_trailer = framehash_write_trailer,
258 .flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
260 .priv_class = &framemd5_class,