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 **hashes;
37 #define OFFSET(x) offsetof(struct HashContext, x)
38 #define ENC AV_OPT_FLAG_ENCODING_PARAM
39 #define HASH_OPT(defaulttype) \
40 { "hash", "set hash to use", OFFSET(hash_name), AV_OPT_TYPE_STRING, {.str = defaulttype}, 0, 0, ENC }
41 #define FORMAT_VERSION_OPT \
42 { "format_version", "file format version", OFFSET(format_version), AV_OPT_TYPE_INT, {.i64 = 2}, 1, 2, ENC }
45 static const AVOption hash_options[] = {
51 #if CONFIG_FRAMEHASH_MUXER
52 static const AVOption framehash_options[] = {
60 static const AVOption md5_options[] = {
66 #if CONFIG_FRAMEMD5_MUXER
67 static const AVOption framemd5_options[] = {
74 #if CONFIG_HASH_MUXER || CONFIG_MD5_MUXER
75 static int hash_init(struct AVFormatContext *s)
78 struct HashContext *c = s->priv_data;
79 c->hashes = av_mallocz_array(1, sizeof(c->hashes));
81 return AVERROR(ENOMEM);
82 res = av_hash_alloc(&c->hashes[0], c->hash_name);
85 av_hash_init(c->hashes[0]);
89 static int hash_write_packet(struct AVFormatContext *s, AVPacket *pkt)
91 struct HashContext *c = s->priv_data;
92 av_hash_update(c->hashes[0], pkt->data, pkt->size);
96 static int hash_write_trailer(struct AVFormatContext *s)
98 struct HashContext *c = s->priv_data;
99 char buf[AV_HASH_MAX_SIZE*2+128];
100 snprintf(buf, sizeof(buf) - 200, "%s=", av_hash_get_name(c->hashes[0]));
102 av_hash_final_hex(c->hashes[0], buf + strlen(buf), sizeof(buf) - strlen(buf));
103 av_strlcatf(buf, sizeof(buf), "\n");
104 avio_write(s->pb, buf, strlen(buf));
110 static void hash_free(struct AVFormatContext *s)
112 struct HashContext *c = s->priv_data;
114 av_hash_freep(&c->hashes[0]);
115 av_freep(&c->hashes);
119 #if CONFIG_HASH_MUXER
120 static const AVClass hashenc_class = {
121 .class_name = "hash muxer",
122 .item_name = av_default_item_name,
123 .option = hash_options,
124 .version = LIBAVUTIL_VERSION_INT,
127 AVOutputFormat ff_hash_muxer = {
129 .long_name = NULL_IF_CONFIG_SMALL("Hash testing"),
130 .priv_data_size = sizeof(struct HashContext),
131 .audio_codec = AV_CODEC_ID_PCM_S16LE,
132 .video_codec = AV_CODEC_ID_RAWVIDEO,
134 .write_packet = hash_write_packet,
135 .write_trailer = hash_write_trailer,
137 .flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
139 .priv_class = &hashenc_class,
144 static const AVClass md5enc_class = {
145 .class_name = "MD5 muxer",
146 .item_name = av_default_item_name,
147 .option = md5_options,
148 .version = LIBAVUTIL_VERSION_INT,
151 AVOutputFormat ff_md5_muxer = {
153 .long_name = NULL_IF_CONFIG_SMALL("MD5 testing"),
154 .priv_data_size = sizeof(struct HashContext),
155 .audio_codec = AV_CODEC_ID_PCM_S16LE,
156 .video_codec = AV_CODEC_ID_RAWVIDEO,
158 .write_packet = hash_write_packet,
159 .write_trailer = hash_write_trailer,
161 .flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
163 .priv_class = &md5enc_class,
167 #if CONFIG_FRAMEHASH_MUXER || CONFIG_FRAMEMD5_MUXER
168 static void framehash_print_extradata(struct AVFormatContext *s)
172 for (i = 0; i < s->nb_streams; i++) {
173 AVStream *st = s->streams[i];
174 AVCodecParameters *par = st->codecpar;
175 if (par->extradata) {
176 struct HashContext *c = s->priv_data;
177 char buf[AV_HASH_MAX_SIZE*2+1];
179 avio_printf(s->pb, "#extradata %d, %31d, ", i, par->extradata_size);
180 av_hash_init(c->hashes[0]);
181 av_hash_update(c->hashes[0], par->extradata, par->extradata_size);
182 av_hash_final_hex(c->hashes[0], buf, sizeof(buf));
183 avio_write(s->pb, buf, strlen(buf));
184 avio_printf(s->pb, "\n");
189 static int framehash_init(struct AVFormatContext *s)
192 struct HashContext *c = s->priv_data;
193 c->hashes = av_mallocz_array(1, sizeof(c->hashes));
195 return AVERROR(ENOMEM);
196 res = av_hash_alloc(&c->hashes[0], c->hash_name);
202 static int framehash_write_header(struct AVFormatContext *s)
204 struct HashContext *c = s->priv_data;
205 avio_printf(s->pb, "#format: frame checksums\n");
206 avio_printf(s->pb, "#version: %d\n", c->format_version);
207 avio_printf(s->pb, "#hash: %s\n", av_hash_get_name(c->hashes[0]));
208 framehash_print_extradata(s);
209 ff_framehash_write_header(s);
210 avio_printf(s->pb, "#stream#, dts, pts, duration, size, hash\n");
214 static int framehash_write_packet(struct AVFormatContext *s, AVPacket *pkt)
216 struct HashContext *c = s->priv_data;
217 char buf[AV_HASH_MAX_SIZE*2+128];
219 av_hash_init(c->hashes[0]);
220 av_hash_update(c->hashes[0], pkt->data, pkt->size);
222 snprintf(buf, sizeof(buf) - (AV_HASH_MAX_SIZE * 2 + 1), "%d, %10"PRId64", %10"PRId64", %8"PRId64", %8d, ",
223 pkt->stream_index, pkt->dts, pkt->pts, pkt->duration, pkt->size);
225 av_hash_final_hex(c->hashes[0], buf + len, sizeof(buf) - len);
226 avio_write(s->pb, buf, strlen(buf));
228 if (c->format_version > 1 && pkt->side_data_elems) {
230 avio_printf(s->pb, ", S=%d", pkt->side_data_elems);
231 for (i = 0; i < pkt->side_data_elems; i++) {
232 av_hash_init(c->hashes[0]);
233 if (HAVE_BIGENDIAN && pkt->side_data[i].type == AV_PKT_DATA_PALETTE) {
234 for (j = 0; j < pkt->side_data[i].size; j += sizeof(uint32_t)) {
235 uint32_t data = AV_RL32(pkt->side_data[i].data + j);
236 av_hash_update(c->hashes[0], (uint8_t *)&data, sizeof(uint32_t));
239 av_hash_update(c->hashes[0], pkt->side_data[i].data, pkt->side_data[i].size);
240 snprintf(buf, sizeof(buf) - (AV_HASH_MAX_SIZE * 2 + 1), ", %8d, ", pkt->side_data[i].size);
242 av_hash_final_hex(c->hashes[0], buf + len, sizeof(buf) - len);
243 avio_write(s->pb, buf, strlen(buf));
247 avio_printf(s->pb, "\n");
252 static void framehash_free(struct AVFormatContext *s)
254 struct HashContext *c = s->priv_data;
256 av_hash_freep(&c->hashes[0]);
257 av_freep(&c->hashes);
261 #if CONFIG_FRAMEHASH_MUXER
262 static const AVClass framehash_class = {
263 .class_name = "frame hash muxer",
264 .item_name = av_default_item_name,
265 .option = framehash_options,
266 .version = LIBAVUTIL_VERSION_INT,
269 AVOutputFormat ff_framehash_muxer = {
271 .long_name = NULL_IF_CONFIG_SMALL("Per-frame hash testing"),
272 .priv_data_size = sizeof(struct HashContext),
273 .audio_codec = AV_CODEC_ID_PCM_S16LE,
274 .video_codec = AV_CODEC_ID_RAWVIDEO,
275 .init = framehash_init,
276 .write_header = framehash_write_header,
277 .write_packet = framehash_write_packet,
278 .deinit = framehash_free,
279 .flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
281 .priv_class = &framehash_class,
285 #if CONFIG_FRAMEMD5_MUXER
286 static const AVClass framemd5_class = {
287 .class_name = "frame MD5 muxer",
288 .item_name = av_default_item_name,
289 .option = framemd5_options,
290 .version = LIBAVUTIL_VERSION_INT,
293 AVOutputFormat ff_framemd5_muxer = {
295 .long_name = NULL_IF_CONFIG_SMALL("Per-frame MD5 testing"),
296 .priv_data_size = sizeof(struct HashContext),
297 .audio_codec = AV_CODEC_ID_PCM_S16LE,
298 .video_codec = AV_CODEC_ID_RAWVIDEO,
299 .init = framehash_init,
300 .write_header = framehash_write_header,
301 .write_packet = framehash_write_packet,
302 .deinit = framehash_free,
303 .flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
305 .priv_class = &framemd5_class,