2 * 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 md5_finish(struct AVFormatContext
*s
, char *buf
)
38 struct MD5Context
*c
= s
->priv_data
;
39 uint8_t md5
[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(md5
));
43 av_hash_final(c
->hash
, md5
);
44 for (i
= 0; i
< len
; i
++) {
45 snprintf(buf
+ offset
, 3, "%02"PRIx8
, md5
[i
]);
51 avio_write(s
->pb
, buf
, strlen(buf
));
55 #define OFFSET(x) offsetof(struct MD5Context, x)
56 #define ENC AV_OPT_FLAG_ENCODING_PARAM
57 static const AVOption hash_options
[] = {
58 { "hash", "set hash to use", OFFSET(hash_name
), AV_OPT_TYPE_STRING
, {.str
= "md5"}, 0, 0, ENC
},
59 { "format_version", "file format version", OFFSET(format_version
), AV_OPT_TYPE_INT
, {.i64
= 1}, 1, 1, ENC
},
63 static const AVClass md5enc_class
= {
64 .class_name
= "hash encoder class",
65 .item_name
= av_default_item_name
,
66 .option
= hash_options
,
67 .version
= LIBAVUTIL_VERSION_INT
,
71 static int write_header(struct AVFormatContext
*s
)
73 struct MD5Context
*c
= s
->priv_data
;
74 int res
= av_hash_alloc(&c
->hash
, c
->hash_name
);
77 av_hash_init(c
->hash
);
81 static int write_packet(struct AVFormatContext
*s
, AVPacket
*pkt
)
83 struct MD5Context
*c
= s
->priv_data
;
84 av_hash_update(c
->hash
, pkt
->data
, pkt
->size
);
88 static int write_trailer(struct AVFormatContext
*s
)
90 struct MD5Context
*c
= s
->priv_data
;
92 av_strlcpy(buf
, av_hash_get_name(c
->hash
), sizeof(buf
) - 200);
93 av_strlcat(buf
, "=", sizeof(buf
) - 200);
97 av_hash_freep(&c
->hash
);
101 AVOutputFormat ff_md5_muxer
= {
103 .long_name
= NULL_IF_CONFIG_SMALL("MD5 testing"),
104 .priv_data_size
= sizeof(struct MD5Context
),
105 .audio_codec
= AV_CODEC_ID_PCM_S16LE
,
106 .video_codec
= AV_CODEC_ID_RAWVIDEO
,
107 .write_header
= write_header
,
108 .write_packet
= write_packet
,
109 .write_trailer
= write_trailer
,
110 .flags
= AVFMT_NOTIMESTAMPS
,
111 .priv_class
= &md5enc_class
,
115 #if CONFIG_FRAMEMD5_MUXER
116 static int framemd5_write_header(struct AVFormatContext
*s
)
118 struct MD5Context
*c
= s
->priv_data
;
119 int res
= av_hash_alloc(&c
->hash
, c
->hash_name
);
122 avio_printf(s
->pb
, "#format: frame checksums\n");
123 avio_printf(s
->pb
, "#version: %d\n", c
->format_version
);
124 avio_printf(s
->pb
, "#hash: %s\n", av_hash_get_name(c
->hash
));
125 ff_framehash_write_header(s
);
126 avio_printf(s
->pb
, "#stream#, dts, pts, duration, size, hash\n");
130 static int framemd5_write_packet(struct AVFormatContext
*s
, AVPacket
*pkt
)
132 struct MD5Context
*c
= s
->priv_data
;
134 av_hash_init(c
->hash
);
135 av_hash_update(c
->hash
, pkt
->data
, pkt
->size
);
137 snprintf(buf
, sizeof(buf
) - 64, "%d, %10"PRId64
", %10"PRId64
", %8d, %8d, ",
138 pkt
->stream_index
, pkt
->dts
, pkt
->pts
, pkt
->duration
, pkt
->size
);
143 static int framemd5_write_trailer(struct AVFormatContext
*s
)
145 struct MD5Context
*c
= s
->priv_data
;
146 av_hash_freep(&c
->hash
);
150 static const AVClass framemd5_class
= {
151 .class_name
= "frame hash encoder class",
152 .item_name
= av_default_item_name
,
153 .option
= hash_options
,
154 .version
= LIBAVUTIL_VERSION_INT
,
157 AVOutputFormat ff_framemd5_muxer
= {
159 .long_name
= NULL_IF_CONFIG_SMALL("Per-frame MD5 testing"),
160 .priv_data_size
= sizeof(struct MD5Context
),
161 .audio_codec
= AV_CODEC_ID_PCM_S16LE
,
162 .video_codec
= AV_CODEC_ID_RAWVIDEO
,
163 .write_header
= framemd5_write_header
,
164 .write_packet
= framemd5_write_packet
,
165 .write_trailer
= framemd5_write_trailer
,
166 .flags
= AVFMT_VARIABLE_FPS
| AVFMT_TS_NONSTRICT
|
168 .priv_class
= &framemd5_class
,