1 /*
2 * Hash/MD5 encoder (for codec/format testing)
3 * Copyright (c) 2009 Reimar Döffinger, based on crcenc (c) 2002 Fabrice Bellard
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include "config_components.h"
23
24 #include "libavutil/avstring.h"
25 #include "libavutil/hash.h"
26 #include "libavutil/intreadwrite.h"
27 #include "libavutil/opt.h"
28 #include "avformat.h"
29 #include "internal.h"
30
31 struct HashContext {
32 const AVClass *avclass;
33 struct AVHashContext **hashes;
34 char *hash_name;
35 int per_stream;
36 int format_version;
37 };
38
39 #define OFFSET(x) offsetof(struct HashContext, x)
40 #define ENC AV_OPT_FLAG_ENCODING_PARAM
41 #define HASH_OPT(defaulttype) \
42 { "hash", "set hash to use", OFFSET(hash_name), AV_OPT_TYPE_STRING, {.str = defaulttype}, 0, 0, ENC }
43 #define FORMAT_VERSION_OPT \
44 { "format_version", "file format version", OFFSET(format_version), AV_OPT_TYPE_INT, {.i64 = 2}, 1, 2, ENC }
45
46 #if CONFIG_HASH_MUXER || CONFIG_STREAMHASH_MUXER
47 static const AVOption hash_streamhash_options[] = {
48 HASH_OPT("sha256"),
49 { NULL },
50 };
51
52 static const AVClass hash_streamhashenc_class = {
53 .class_name = "(stream) hash muxer",
54 .item_name = av_default_item_name,
55 .option = hash_streamhash_options,
56 .version = LIBAVUTIL_VERSION_INT,
57 };
58 #endif
59
60 #if CONFIG_FRAMEHASH_MUXER
61 static const AVOption framehash_options[] = {
62 HASH_OPT("sha256"),
63 FORMAT_VERSION_OPT,
64 { NULL },
65 };
66 #endif
67
68 #if CONFIG_MD5_MUXER
69 static const AVOption md5_options[] = {
70 HASH_OPT("md5"),
71 { NULL },
72 };
73 #endif
74
75 #if CONFIG_FRAMEMD5_MUXER
76 static const AVOption framemd5_options[] = {
77 HASH_OPT("md5"),
78 FORMAT_VERSION_OPT,
79 { NULL },
80 };
81 #endif
82
83 #if CONFIG_HASH_MUXER || CONFIG_MD5_MUXER
hash_init(struct AVFormatContext * s)84 static int hash_init(struct AVFormatContext *s)
85 {
86 int res;
87 struct HashContext *c = s->priv_data;
88 c->per_stream = 0;
89 c->hashes = av_mallocz(sizeof(*c->hashes));
90 if (!c->hashes)
91 return AVERROR(ENOMEM);
92 res = av_hash_alloc(&c->hashes[0], c->hash_name);
93 if (res < 0)
94 return res;
95 av_hash_init(c->hashes[0]);
96 return 0;
97 }
98 #endif
99
100 #if CONFIG_STREAMHASH_MUXER
streamhash_init(struct AVFormatContext * s)101 static int streamhash_init(struct AVFormatContext *s)
102 {
103 int res, i;
104 struct HashContext *c = s->priv_data;
105 c->per_stream = 1;
106 c->hashes = av_calloc(s->nb_streams, sizeof(*c->hashes));
107 if (!c->hashes)
108 return AVERROR(ENOMEM);
109 for (i = 0; i < s->nb_streams; i++) {
110 res = av_hash_alloc(&c->hashes[i], c->hash_name);
111 if (res < 0) {
112 return res;
113 }
114 av_hash_init(c->hashes[i]);
115 }
116 return 0;
117 }
118 #endif
119
120 #if CONFIG_HASH_MUXER || CONFIG_MD5_MUXER || CONFIG_STREAMHASH_MUXER
get_media_type_char(enum AVMediaType type)121 static char get_media_type_char(enum AVMediaType type)
122 {
123 switch (type) {
124 case AVMEDIA_TYPE_VIDEO: return 'v';
125 case AVMEDIA_TYPE_AUDIO: return 'a';
126 case AVMEDIA_TYPE_DATA: return 'd';
127 case AVMEDIA_TYPE_SUBTITLE: return 's';
128 case AVMEDIA_TYPE_ATTACHMENT: return 't';
129 default: return '?';
130 }
131 }
132
hash_write_packet(struct AVFormatContext * s,AVPacket * pkt)133 static int hash_write_packet(struct AVFormatContext *s, AVPacket *pkt)
134 {
135 struct HashContext *c = s->priv_data;
136 av_hash_update(c->hashes[c->per_stream ? pkt->stream_index : 0], pkt->data, pkt->size);
137 return 0;
138 }
139
hash_write_trailer(struct AVFormatContext * s)140 static int hash_write_trailer(struct AVFormatContext *s)
141 {
142 struct HashContext *c = s->priv_data;
143 int num_hashes = c->per_stream ? s->nb_streams : 1;
144 for (int i = 0; i < num_hashes; i++) {
145 char buf[AV_HASH_MAX_SIZE*2+128];
146 if (c->per_stream) {
147 AVStream *st = s->streams[i];
148 snprintf(buf, sizeof(buf) - 200, "%d,%c,%s=", i, get_media_type_char(st->codecpar->codec_type),
149 av_hash_get_name(c->hashes[i]));
150 } else {
151 snprintf(buf, sizeof(buf) - 200, "%s=", av_hash_get_name(c->hashes[i]));
152 }
153 av_hash_final_hex(c->hashes[i], buf + strlen(buf), sizeof(buf) - strlen(buf));
154 av_strlcatf(buf, sizeof(buf), "\n");
155 avio_write(s->pb, buf, strlen(buf));
156 }
157
158 return 0;
159 }
160 #endif
161
hash_free(struct AVFormatContext * s)162 static void hash_free(struct AVFormatContext *s)
163 {
164 struct HashContext *c = s->priv_data;
165 if (c->hashes) {
166 int num_hashes = c->per_stream ? s->nb_streams : 1;
167 for (int i = 0; i < num_hashes; i++) {
168 av_hash_freep(&c->hashes[i]);
169 }
170 }
171 av_freep(&c->hashes);
172 }
173
174 #if CONFIG_HASH_MUXER
175 const AVOutputFormat ff_hash_muxer = {
176 .name = "hash",
177 .long_name = NULL_IF_CONFIG_SMALL("Hash testing"),
178 .priv_data_size = sizeof(struct HashContext),
179 .audio_codec = AV_CODEC_ID_PCM_S16LE,
180 .video_codec = AV_CODEC_ID_RAWVIDEO,
181 .init = hash_init,
182 .write_packet = hash_write_packet,
183 .write_trailer = hash_write_trailer,
184 .deinit = hash_free,
185 .flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
186 AVFMT_TS_NEGATIVE,
187 .priv_class = &hash_streamhashenc_class,
188 };
189 #endif
190
191 #if CONFIG_MD5_MUXER
192 static const AVClass md5enc_class = {
193 .class_name = "MD5 muxer",
194 .item_name = av_default_item_name,
195 .option = md5_options,
196 .version = LIBAVUTIL_VERSION_INT,
197 };
198
199 const AVOutputFormat ff_md5_muxer = {
200 .name = "md5",
201 .long_name = NULL_IF_CONFIG_SMALL("MD5 testing"),
202 .priv_data_size = sizeof(struct HashContext),
203 .audio_codec = AV_CODEC_ID_PCM_S16LE,
204 .video_codec = AV_CODEC_ID_RAWVIDEO,
205 .init = hash_init,
206 .write_packet = hash_write_packet,
207 .write_trailer = hash_write_trailer,
208 .deinit = hash_free,
209 .flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
210 AVFMT_TS_NEGATIVE,
211 .priv_class = &md5enc_class,
212 };
213 #endif
214
215 #if CONFIG_STREAMHASH_MUXER
216 const AVOutputFormat ff_streamhash_muxer = {
217 .name = "streamhash",
218 .long_name = NULL_IF_CONFIG_SMALL("Per-stream hash testing"),
219 .priv_data_size = sizeof(struct HashContext),
220 .audio_codec = AV_CODEC_ID_PCM_S16LE,
221 .video_codec = AV_CODEC_ID_RAWVIDEO,
222 .init = streamhash_init,
223 .write_packet = hash_write_packet,
224 .write_trailer = hash_write_trailer,
225 .deinit = hash_free,
226 .flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
227 AVFMT_TS_NEGATIVE,
228 .priv_class = &hash_streamhashenc_class,
229 };
230 #endif
231
232 #if CONFIG_FRAMEHASH_MUXER || CONFIG_FRAMEMD5_MUXER
framehash_print_extradata(struct AVFormatContext * s)233 static void framehash_print_extradata(struct AVFormatContext *s)
234 {
235 int i;
236
237 for (i = 0; i < s->nb_streams; i++) {
238 AVStream *st = s->streams[i];
239 AVCodecParameters *par = st->codecpar;
240 if (par->extradata) {
241 struct HashContext *c = s->priv_data;
242 char buf[AV_HASH_MAX_SIZE*2+1];
243
244 avio_printf(s->pb, "#extradata %d, %31d, ", i, par->extradata_size);
245 av_hash_init(c->hashes[0]);
246 av_hash_update(c->hashes[0], par->extradata, par->extradata_size);
247 av_hash_final_hex(c->hashes[0], buf, sizeof(buf));
248 avio_write(s->pb, buf, strlen(buf));
249 avio_printf(s->pb, "\n");
250 }
251 }
252 }
253
framehash_init(struct AVFormatContext * s)254 static int framehash_init(struct AVFormatContext *s)
255 {
256 int res;
257 struct HashContext *c = s->priv_data;
258 c->per_stream = 0;
259 c->hashes = av_mallocz(sizeof(*c->hashes));
260 if (!c->hashes)
261 return AVERROR(ENOMEM);
262 res = av_hash_alloc(&c->hashes[0], c->hash_name);
263 if (res < 0)
264 return res;
265 return 0;
266 }
267
framehash_write_header(struct AVFormatContext * s)268 static int framehash_write_header(struct AVFormatContext *s)
269 {
270 struct HashContext *c = s->priv_data;
271 avio_printf(s->pb, "#format: frame checksums\n");
272 avio_printf(s->pb, "#version: %d\n", c->format_version);
273 avio_printf(s->pb, "#hash: %s\n", av_hash_get_name(c->hashes[0]));
274 framehash_print_extradata(s);
275 ff_framehash_write_header(s);
276 avio_printf(s->pb, "#stream#, dts, pts, duration, size, hash\n");
277 return 0;
278 }
279
framehash_write_packet(struct AVFormatContext * s,AVPacket * pkt)280 static int framehash_write_packet(struct AVFormatContext *s, AVPacket *pkt)
281 {
282 struct HashContext *c = s->priv_data;
283 char buf[AV_HASH_MAX_SIZE*2+128];
284 int len;
285 av_hash_init(c->hashes[0]);
286 av_hash_update(c->hashes[0], pkt->data, pkt->size);
287
288 snprintf(buf, sizeof(buf) - (AV_HASH_MAX_SIZE * 2 + 1), "%d, %10"PRId64", %10"PRId64", %8"PRId64", %8d, ",
289 pkt->stream_index, pkt->dts, pkt->pts, pkt->duration, pkt->size);
290 len = strlen(buf);
291 av_hash_final_hex(c->hashes[0], buf + len, sizeof(buf) - len);
292 avio_write(s->pb, buf, strlen(buf));
293
294 if (c->format_version > 1 && pkt->side_data_elems) {
295 int i;
296 avio_printf(s->pb, ", S=%d", pkt->side_data_elems);
297 for (i = 0; i < pkt->side_data_elems; i++) {
298 av_hash_init(c->hashes[0]);
299 if (HAVE_BIGENDIAN && pkt->side_data[i].type == AV_PKT_DATA_PALETTE) {
300 for (size_t j = 0; j < pkt->side_data[i].size; j += sizeof(uint32_t)) {
301 uint32_t data = AV_RL32(pkt->side_data[i].data + j);
302 av_hash_update(c->hashes[0], (uint8_t *)&data, sizeof(uint32_t));
303 }
304 } else
305 av_hash_update(c->hashes[0], pkt->side_data[i].data, pkt->side_data[i].size);
306 snprintf(buf, sizeof(buf) - (AV_HASH_MAX_SIZE * 2 + 1),
307 ", %8"SIZE_SPECIFIER", ", pkt->side_data[i].size);
308 len = strlen(buf);
309 av_hash_final_hex(c->hashes[0], buf + len, sizeof(buf) - len);
310 avio_write(s->pb, buf, strlen(buf));
311 }
312 }
313
314 avio_printf(s->pb, "\n");
315 return 0;
316 }
317 #endif
318
319 #if CONFIG_FRAMEHASH_MUXER
320 static const AVClass framehash_class = {
321 .class_name = "frame hash muxer",
322 .item_name = av_default_item_name,
323 .option = framehash_options,
324 .version = LIBAVUTIL_VERSION_INT,
325 };
326
327 const AVOutputFormat ff_framehash_muxer = {
328 .name = "framehash",
329 .long_name = NULL_IF_CONFIG_SMALL("Per-frame hash testing"),
330 .priv_data_size = sizeof(struct HashContext),
331 .audio_codec = AV_CODEC_ID_PCM_S16LE,
332 .video_codec = AV_CODEC_ID_RAWVIDEO,
333 .init = framehash_init,
334 .write_header = framehash_write_header,
335 .write_packet = framehash_write_packet,
336 .deinit = hash_free,
337 .flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
338 AVFMT_TS_NEGATIVE,
339 .priv_class = &framehash_class,
340 };
341 #endif
342
343 #if CONFIG_FRAMEMD5_MUXER
344 static const AVClass framemd5_class = {
345 .class_name = "frame MD5 muxer",
346 .item_name = av_default_item_name,
347 .option = framemd5_options,
348 .version = LIBAVUTIL_VERSION_INT,
349 };
350
351 const AVOutputFormat ff_framemd5_muxer = {
352 .name = "framemd5",
353 .long_name = NULL_IF_CONFIG_SMALL("Per-frame MD5 testing"),
354 .priv_data_size = sizeof(struct HashContext),
355 .audio_codec = AV_CODEC_ID_PCM_S16LE,
356 .video_codec = AV_CODEC_ID_RAWVIDEO,
357 .init = framehash_init,
358 .write_header = framehash_write_header,
359 .write_packet = framehash_write_packet,
360 .deinit = hash_free,
361 .flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
362 AVFMT_TS_NEGATIVE,
363 .priv_class = &framemd5_class,
364 };
365 #endif
366