FFmpeg
Loading...
Searching...
No Matches
hashenc.c
Go to the documentation of this file.
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"
27#include "libavutil/mem.h"
28#include "libavutil/opt.h"
29#include "avformat.h"
30#include "internal.h"
31#include "mux.h"
32
43
44#define OFFSET(x) offsetof(struct HashContext, x)
45#define ENC AV_OPT_FLAG_ENCODING_PARAM
46#define HASH_OPT(defaulttype) \
47 { "hash", "set hash to use", OFFSET(hash_name), AV_OPT_TYPE_STRING, {.str = defaulttype}, 0, 0, ENC }
48#define FORMAT_VERSION_OPT \
49 { "format_version", "file format version", OFFSET(format_version), AV_OPT_TYPE_INT, {.i64 = 2}, 1, 2, ENC }
50
51#if CONFIG_FRAMEHASH_MUXER | CONFIG_STREAMHASH_MUXER | CONFIG_HASH_MUXER
52static const AVOption hash_options[] = {
54 HASH_OPT("sha256"),
55 { NULL },
56};
57#endif
58
59#if CONFIG_MD5_MUXER
60static const AVOption md5_options[] = {
61 HASH_OPT("md5"),
62 { NULL },
63};
64#endif
65
66#if CONFIG_FRAMEMD5_MUXER
67static const AVOption framemd5_options[] = {
68 HASH_OPT("md5"),
70 { NULL },
71};
72#endif
73
74#if CONFIG_HASH_MUXER || CONFIG_MD5_MUXER
75static int hash_init(struct AVFormatContext *s)
76{
77 int res;
78 struct HashContext *c = s->priv_data;
79 c->per_stream = 0;
80 c->hashes = av_mallocz(sizeof(*c->hashes));
81 if (!c->hashes)
82 return AVERROR(ENOMEM);
83 res = av_hash_alloc(&c->hashes->hash, c->hash_name);
84 if (res < 0)
85 return res;
86 av_hash_init(c->hashes->hash);
87 return 0;
88}
89
90static int hash_write_packet(struct AVFormatContext *s, AVPacket *pkt)
91{
92 struct HashContext *c = s->priv_data;
93 av_hash_update(c->hashes->hash, pkt->data, pkt->size);
94 return 0;
95}
96#endif
97
98#if CONFIG_STREAMHASH_MUXER || CONFIG_FRAMEHASH_MUXER || CONFIG_FRAMEMD5_MUXER
99static void hash_print_extradata(struct AVFormatContext *s)
100{
101 int i;
102
103 for (i = 0; i < s->nb_streams; i++) {
104 const AVStream *st = s->streams[i];
105 const AVCodecParameters *par = st->codecpar;
106 if (par->extradata) {
107 struct HashContext *c = s->priv_data;
108 char buf[AV_HASH_MAX_SIZE*2+1];
109
110 av_hash_init(c->hashes->hash);
111 av_hash_update(c->hashes->hash, par->extradata, par->extradata_size);
112 av_hash_final_hex(c->hashes->hash, buf, sizeof(buf));
113 avio_printf(s->pb, "#extradata %d, %31d, %s\n", i, par->extradata_size, buf);
114 }
115 }
116}
117#endif
118
120 const AVPacketSideData *sd, int side_data_elems)
121{
122 struct HashContext *c = s->priv_data;
123 char buf[AV_HASH_MAX_SIZE*2+128];
124
125 avio_printf(s->pb, ", S=%d", side_data_elems);
126 for (int i = 0; i < side_data_elems; i++) {
127 av_hash_init(c->hashes->hash);
128 if (HAVE_BIGENDIAN && sd[i].type == AV_PKT_DATA_PALETTE) {
129 for (size_t j = 0; j < sd[i].size; j += sizeof(uint32_t)) {
130 uint32_t data = AV_RL32(sd[i].data + j);
131 av_hash_update(c->hashes->hash, (uint8_t *)&data, sizeof(uint32_t));
132 }
133 } else
134 av_hash_update(c->hashes->hash, sd[i].data, sd[i].size);
135 snprintf(buf, sizeof(buf) - (AV_HASH_MAX_SIZE * 2 + 1),
136 ", %8zu, ", sd[i].size);
137 size_t len = strlen(buf);
138 av_hash_final_hex(c->hashes->hash, buf + len, sizeof(buf) - len);
139 avio_write(s->pb, buf, strlen(buf));
140 }
141}
142
143#if CONFIG_HASH_MUXER || CONFIG_MD5_MUXER || CONFIG_STREAMHASH_MUXER
144static char get_media_type_char(enum AVMediaType type)
145{
146 switch (type) {
147 case AVMEDIA_TYPE_VIDEO: return 'v';
148 case AVMEDIA_TYPE_AUDIO: return 'a';
149 case AVMEDIA_TYPE_DATA: return 'd';
150 case AVMEDIA_TYPE_SUBTITLE: return 's';
151 case AVMEDIA_TYPE_ATTACHMENT: return 't';
152 default: return '?';
153 }
154}
155
156static int hash_write_trailer(struct AVFormatContext *s)
157{
158 struct HashContext *c = s->priv_data;
159 int num_hashes = c->per_stream ? s->nb_streams : 1;
160 for (int i = 0; i < num_hashes; i++) {
161 char buf[AV_HASH_MAX_SIZE*2+128];
162 if (c->per_stream) {
163 if (c->format_version < 2) {
164 AVStream *st = s->streams[i];
165 snprintf(buf, sizeof(buf) - 200, "%d,%c,%s=", i, get_media_type_char(st->codecpar->codec_type),
166 av_hash_get_name(c->hashes[i].hash));
167 } else
168 snprintf(buf, sizeof(buf) - 200, "%d, %11"PRId64", ", i, c->hashes[i].stream_size);
169 } else {
170 snprintf(buf, sizeof(buf) - 200, "%s=", av_hash_get_name(c->hashes[i].hash));
171 }
172 av_hash_final_hex(c->hashes[i].hash, buf + strlen(buf), sizeof(buf) - strlen(buf));
173 avio_write(s->pb, buf, strlen(buf));
174
175 if (c->per_stream && c->format_version >= 2) {
176 const AVStream *st = s->streams[i];
179 }
180
181 avio_printf(s->pb, "\n");
182 }
183
184 return 0;
185}
186#endif
187
188static void hash_free(struct AVFormatContext *s)
189{
190 struct HashContext *c = s->priv_data;
191 if (c->hashes) {
192 int num_hashes = c->per_stream ? s->nb_streams : 1;
193 for (int i = 0; i < num_hashes; i++) {
194 av_hash_freep(&c->hashes[i].hash);
195 }
196 }
197 av_freep(&c->hashes);
198}
199
200#if CONFIG_HASH_MUXER
201static const AVClass hash_hashenc_class = {
202 .class_name = "hash muxer",
203 .item_name = av_default_item_name,
204 .option = &hash_options[1],
205 .version = LIBAVUTIL_VERSION_INT,
206};
207
209 .p.name = "hash",
210 .p.long_name = NULL_IF_CONFIG_SMALL("Hash testing"),
211 .priv_data_size = sizeof(struct HashContext),
212 .p.audio_codec = AV_CODEC_ID_PCM_S16LE,
213 .p.video_codec = AV_CODEC_ID_RAWVIDEO,
214 .init = hash_init,
215 .write_packet = hash_write_packet,
216 .write_trailer = hash_write_trailer,
217 .deinit = hash_free,
220 .p.priv_class = &hash_hashenc_class,
221};
222#endif
223
224#if CONFIG_MD5_MUXER
225static const AVClass md5enc_class = {
226 .class_name = "MD5 muxer",
227 .item_name = av_default_item_name,
228 .option = md5_options,
229 .version = LIBAVUTIL_VERSION_INT,
230};
231
233 .p.name = "md5",
234 .p.long_name = NULL_IF_CONFIG_SMALL("MD5 testing"),
235 .priv_data_size = sizeof(struct HashContext),
236 .p.audio_codec = AV_CODEC_ID_PCM_S16LE,
237 .p.video_codec = AV_CODEC_ID_RAWVIDEO,
238 .init = hash_init,
239 .write_packet = hash_write_packet,
240 .write_trailer = hash_write_trailer,
241 .deinit = hash_free,
244 .p.priv_class = &md5enc_class,
245};
246#endif
247
248#if CONFIG_STREAMHASH_MUXER
249static int streamhash_init(struct AVFormatContext *s)
250{
251 int res, i;
252 struct HashContext *c = s->priv_data;
253 c->per_stream = 1;
254 c->hashes = av_calloc(s->nb_streams, sizeof(*c->hashes));
255 if (!c->hashes)
256 return AVERROR(ENOMEM);
257 for (i = 0; i < s->nb_streams; i++) {
258 res = av_hash_alloc(&c->hashes[i].hash, c->hash_name);
259 if (res < 0) {
260 return res;
261 }
262 av_hash_init(c->hashes[i].hash);
263 }
264 return 0;
265}
266
267static int streamhash_write_header(struct AVFormatContext *s)
268{
269 struct HashContext *c = s->priv_data;
270 if (c->format_version < 2)
271 return 0;
272 avio_printf(s->pb, "#format: stream checksums\n"
273 "#version: %d\n"
274 "#hash: %s\n", c->format_version, av_hash_get_name(c->hashes->hash));
275 hash_print_extradata(s);
277 avio_printf(s->pb, "#stream#, size, hash\n");
278 av_hash_init(c->hashes->hash);
279 return 0;
280}
281
282static int streamhash_write_packet(struct AVFormatContext *s, AVPacket *pkt)
283{
284 struct HashContext *c = s->priv_data;
285 av_hash_update(c->hashes[pkt->stream_index].hash, pkt->data, pkt->size);
286 c->hashes[pkt->stream_index].stream_size += pkt->size;
287 return 0;
288}
289
290static const AVClass streamhashenc_class = {
291 .class_name = "stream hash muxer",
292 .item_name = av_default_item_name,
293 .option = hash_options,
294 .version = LIBAVUTIL_VERSION_INT,
295};
296
298 .p.name = "streamhash",
299 .p.long_name = NULL_IF_CONFIG_SMALL("Per-stream hash testing"),
300 .priv_data_size = sizeof(struct HashContext),
301 .p.audio_codec = AV_CODEC_ID_PCM_S16LE,
302 .p.video_codec = AV_CODEC_ID_RAWVIDEO,
303 .init = streamhash_init,
304 .write_header = streamhash_write_header,
305 .write_packet = streamhash_write_packet,
306 .write_trailer = hash_write_trailer,
307 .deinit = hash_free,
310 .p.priv_class = &streamhashenc_class,
311};
312#endif
313
314#if CONFIG_FRAMEHASH_MUXER || CONFIG_FRAMEMD5_MUXER
315static int framehash_init(struct AVFormatContext *s)
316{
317 int res;
318 struct HashContext *c = s->priv_data;
319 c->per_stream = 0;
320 c->hashes = av_mallocz(sizeof(*c->hashes));
321 if (!c->hashes)
322 return AVERROR(ENOMEM);
323 res = av_hash_alloc(&c->hashes->hash, c->hash_name);
324 if (res < 0)
325 return res;
326 return 0;
327}
328
329static int framehash_write_header(struct AVFormatContext *s)
330{
331 struct HashContext *c = s->priv_data;
332 avio_printf(s->pb, "#format: frame checksums\n");
333 avio_printf(s->pb, "#version: %d\n", c->format_version);
334 avio_printf(s->pb, "#hash: %s\n", av_hash_get_name(c->hashes->hash));
335 hash_print_extradata(s);
337 avio_printf(s->pb, "#stream#, dts, pts, duration, size, hash\n");
338 return 0;
339}
340
341static int framehash_write_packet(struct AVFormatContext *s, AVPacket *pkt)
342{
343 struct HashContext *c = s->priv_data;
344 char buf[AV_HASH_MAX_SIZE*2+128];
345 int len;
346 av_hash_init(c->hashes->hash);
347 av_hash_update(c->hashes->hash, pkt->data, pkt->size);
348
349 snprintf(buf, sizeof(buf) - (AV_HASH_MAX_SIZE * 2 + 1), "%d, %10"PRId64", %10"PRId64", %8"PRId64", %8d, ",
350 pkt->stream_index, pkt->dts, pkt->pts, pkt->duration, pkt->size);
351 len = strlen(buf);
352 av_hash_final_hex(c->hashes->hash, buf + len, sizeof(buf) - len);
353 avio_write(s->pb, buf, strlen(buf));
354
355 if (c->format_version > 1 && pkt->side_data_elems) {
356 hash_print_sidedata(s, pkt->side_data, pkt->side_data_elems);
357 }
358
359 avio_printf(s->pb, "\n");
360 return 0;
361}
362#endif
363
364#if CONFIG_FRAMEHASH_MUXER
365static const AVClass framehash_class = {
366 .class_name = "frame hash muxer",
367 .item_name = av_default_item_name,
368 .option = hash_options,
369 .version = LIBAVUTIL_VERSION_INT,
370};
371
373 .p.name = "framehash",
374 .p.long_name = NULL_IF_CONFIG_SMALL("Per-frame hash testing"),
375 .priv_data_size = sizeof(struct HashContext),
376 .p.audio_codec = AV_CODEC_ID_PCM_S16LE,
377 .p.video_codec = AV_CODEC_ID_RAWVIDEO,
378 .init = framehash_init,
379 .write_header = framehash_write_header,
380 .write_packet = framehash_write_packet,
381 .deinit = hash_free,
384 .p.priv_class = &framehash_class,
385};
386#endif
387
388#if CONFIG_FRAMEMD5_MUXER
389static const AVClass framemd5_class = {
390 .class_name = "frame MD5 muxer",
391 .item_name = av_default_item_name,
392 .option = framemd5_options,
393 .version = LIBAVUTIL_VERSION_INT,
394};
395
397 .p.name = "framemd5",
398 .p.long_name = NULL_IF_CONFIG_SMALL("Per-frame MD5 testing"),
399 .priv_data_size = sizeof(struct HashContext),
400 .p.audio_codec = AV_CODEC_ID_PCM_S16LE,
401 .p.video_codec = AV_CODEC_ID_RAWVIDEO,
402 .init = framehash_init,
403 .write_header = framehash_write_header,
404 .write_packet = framehash_write_packet,
405 .deinit = hash_free,
408 .p.priv_class = &framemd5_class,
409};
410#endif
const FFOutputFormat ff_framemd5_muxer
const FFOutputFormat ff_hash_muxer
const FFOutputFormat ff_md5_muxer
const FFOutputFormat ff_streamhash_muxer
const FFOutputFormat ff_framehash_muxer
Main libavformat public API header.
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition avformat.h:501
#define AVFMT_TS_NEGATIVE
Format allows muxing negative timestamps.
Definition avformat.h:510
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition avformat.h:507
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2
Writes a formatted string to the context.
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition aviobuf.c:206
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
int ff_framehash_write_header(AVFormatContext *s)
Set the timebase for each stream from the corresponding codec timebase and print it.
Definition framehash.c:25
@ AV_CODEC_ID_RAWVIDEO
Definition codec_id.h:63
@ AV_CODEC_ID_PCM_S16LE
Definition codec_id.h:330
@ AV_PKT_DATA_PALETTE
An AV_PKT_DATA_PALETTE side data packet contains exactly AVPALETTE_SIZE bytes worth of palette.
Definition packet.h:47
#define AVERROR(e)
Definition error.h:45
void av_hash_freep(AVHashContext **ctx)
Free hash context and set hash context pointer to NULL.
Definition hash.c:248
void av_hash_final_hex(struct AVHashContext *ctx, uint8_t *dst, int size)
Finalize a hash context and store the hexadecimal representation of the actual hash value as a string...
Definition hash.c:225
void av_hash_init(AVHashContext *ctx)
Initialize or reset a hash context.
Definition hash.c:151
void av_hash_update(AVHashContext *ctx, const uint8_t *src, size_t len)
Update a hash context with additional data.
Definition hash.c:172
int av_hash_alloc(AVHashContext **ctx, const char *name)
Allocate a hash context for the algorithm specified by name.
Definition hash.c:114
#define AV_HASH_MAX_SIZE
Maximum value that av_hash_get_size() will currently return.
Definition hash.h:156
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
AVMediaType
Definition avutil.h:198
@ AVMEDIA_TYPE_ATTACHMENT
Opaque data information usually sparse.
Definition avutil.h:204
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_SUBTITLE
Definition avutil.h:203
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
@ AVMEDIA_TYPE_DATA
Opaque data information usually continuous.
Definition avutil.h:202
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
const char * av_hash_get_name(const AVHashContext *ctx)
Definition hash.c:104
Generic hashing API.
#define FORMAT_VERSION_OPT
Definition hashenc.c:48
#define HASH_OPT(defaulttype)
Definition hashenc.c:46
static void hash_print_sidedata(struct AVFormatContext *s, const AVPacketSideData *sd, int side_data_elems)
Definition hashenc.c:119
static void hash_free(struct AVFormatContext *s)
Definition hashenc.c:188
cl_device_type type
#define AV_RL32(p)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
const char data[16]
Definition mxf.c:149
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
AVOptions.
static char get_media_type_char(enum AVMediaType type)
Definition opt_common.c:669
#define snprintf
Definition snprintf.h:34
Describe the class of an AVClass context structure.
Definition log.h:76
This struct describes the properties of an encoded stream.
Definition codec_par.h:49
int extradata_size
Size of the extradata content in bytes.
Definition codec_par.h:75
int nb_coded_side_data
Amount of entries in coded_side_data.
Definition codec_par.h:88
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition codec_par.h:71
AVPacketSideData * coded_side_data
Additional data associated with the entire stream.
Definition codec_par.h:83
Format I/O context.
Definition avformat.h:1333
AVOption.
Definition opt.h:428
This structure stores auxiliary information for decoding, presenting, or otherwise processing the cod...
Definition packet.h:424
uint8_t * data
Definition packet.h:425
This structure stores compressed data.
Definition packet.h:580
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
char * hash_name
Definition hashenc.c:39
int format_version
Definition hashenc.c:41
const AVClass * avclass
Definition hashenc.c:34
struct HashContext::@342233306123153340317170317037132003264217041033 * hashes
int64_t stream_size
Definition hashenc.c:37
int per_stream
Definition hashenc.c:40
struct AVHashContext * hash
Definition hashenc.c:36
#define av_mallocz(s)
#define av_freep(p)
int size
int len
static double c[64]