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 if (sd[i].type != AV_PKT_DATA_IAMF_MIX_GAIN_PARAM &&
136 av_hash_update(c->hashes->hash, sd[i].data, sd[i].size);
137 snprintf(buf, sizeof(buf) - (AV_HASH_MAX_SIZE * 2 + 1),
138 ", %8zu, ", sd[i].size);
139 size_t len = strlen(buf);
140 av_hash_final_hex(c->hashes->hash, buf + len, sizeof(buf) - len);
141 avio_write(s->pb, buf, strlen(buf));
142 }
143}
144
145#if CONFIG_HASH_MUXER || CONFIG_MD5_MUXER || CONFIG_STREAMHASH_MUXER
146static char get_media_type_char(enum AVMediaType type)
147{
148 switch (type) {
149 case AVMEDIA_TYPE_VIDEO: return 'v';
150 case AVMEDIA_TYPE_AUDIO: return 'a';
151 case AVMEDIA_TYPE_DATA: return 'd';
152 case AVMEDIA_TYPE_SUBTITLE: return 's';
153 case AVMEDIA_TYPE_ATTACHMENT: return 't';
154 default: return '?';
155 }
156}
157
158static int hash_write_trailer(struct AVFormatContext *s)
159{
160 struct HashContext *c = s->priv_data;
161 int num_hashes = c->per_stream ? s->nb_streams : 1;
162 for (int i = 0; i < num_hashes; i++) {
163 char buf[AV_HASH_MAX_SIZE*2+128];
164 if (c->per_stream) {
165 if (c->format_version < 2) {
166 AVStream *st = s->streams[i];
167 snprintf(buf, sizeof(buf) - 200, "%d,%c,%s=", i, get_media_type_char(st->codecpar->codec_type),
168 av_hash_get_name(c->hashes[i].hash));
169 } else
170 snprintf(buf, sizeof(buf) - 200, "%d, %11"PRId64", ", i, c->hashes[i].stream_size);
171 } else {
172 snprintf(buf, sizeof(buf) - 200, "%s=", av_hash_get_name(c->hashes[i].hash));
173 }
174 av_hash_final_hex(c->hashes[i].hash, buf + strlen(buf), sizeof(buf) - strlen(buf));
175 avio_write(s->pb, buf, strlen(buf));
176
177 if (c->per_stream && c->format_version >= 2) {
178 const AVStream *st = s->streams[i];
181 }
182
183 avio_printf(s->pb, "\n");
184 }
185
186 return 0;
187}
188#endif
189
190static void hash_free(struct AVFormatContext *s)
191{
192 struct HashContext *c = s->priv_data;
193 if (c->hashes) {
194 int num_hashes = c->per_stream ? s->nb_streams : 1;
195 for (int i = 0; i < num_hashes; i++) {
196 av_hash_freep(&c->hashes[i].hash);
197 }
198 }
199 av_freep(&c->hashes);
200}
201
202#if CONFIG_HASH_MUXER
203static const AVClass hash_hashenc_class = {
204 .class_name = "hash muxer",
205 .item_name = av_default_item_name,
206 .option = &hash_options[1],
207 .version = LIBAVUTIL_VERSION_INT,
208};
209
211 .p.name = "hash",
212 .p.long_name = NULL_IF_CONFIG_SMALL("Hash testing"),
213 .priv_data_size = sizeof(struct HashContext),
214 .p.audio_codec = AV_CODEC_ID_PCM_S16LE,
215 .p.video_codec = AV_CODEC_ID_RAWVIDEO,
216 .init = hash_init,
217 .write_packet = hash_write_packet,
218 .write_trailer = hash_write_trailer,
219 .deinit = hash_free,
222 .p.priv_class = &hash_hashenc_class,
223};
224#endif
225
226#if CONFIG_MD5_MUXER
227static const AVClass md5enc_class = {
228 .class_name = "MD5 muxer",
229 .item_name = av_default_item_name,
230 .option = md5_options,
231 .version = LIBAVUTIL_VERSION_INT,
232};
233
235 .p.name = "md5",
236 .p.long_name = NULL_IF_CONFIG_SMALL("MD5 testing"),
237 .priv_data_size = sizeof(struct HashContext),
238 .p.audio_codec = AV_CODEC_ID_PCM_S16LE,
239 .p.video_codec = AV_CODEC_ID_RAWVIDEO,
240 .init = hash_init,
241 .write_packet = hash_write_packet,
242 .write_trailer = hash_write_trailer,
243 .deinit = hash_free,
246 .p.priv_class = &md5enc_class,
247};
248#endif
249
250#if CONFIG_STREAMHASH_MUXER
251static int streamhash_init(struct AVFormatContext *s)
252{
253 int res, i;
254 struct HashContext *c = s->priv_data;
255 c->per_stream = 1;
256 c->hashes = av_calloc(s->nb_streams, sizeof(*c->hashes));
257 if (!c->hashes)
258 return AVERROR(ENOMEM);
259 for (i = 0; i < s->nb_streams; i++) {
260 res = av_hash_alloc(&c->hashes[i].hash, c->hash_name);
261 if (res < 0) {
262 return res;
263 }
264 av_hash_init(c->hashes[i].hash);
265 }
266 return 0;
267}
268
269static int streamhash_write_header(struct AVFormatContext *s)
270{
271 struct HashContext *c = s->priv_data;
272 if (c->format_version < 2)
273 return 0;
274 avio_printf(s->pb, "#format: stream checksums\n"
275 "#version: %d\n"
276 "#hash: %s\n", c->format_version, av_hash_get_name(c->hashes->hash));
277 hash_print_extradata(s);
279 avio_printf(s->pb, "#stream#, size, hash\n");
280 av_hash_init(c->hashes->hash);
281 return 0;
282}
283
284static int streamhash_write_packet(struct AVFormatContext *s, AVPacket *pkt)
285{
286 struct HashContext *c = s->priv_data;
287 av_hash_update(c->hashes[pkt->stream_index].hash, pkt->data, pkt->size);
288 c->hashes[pkt->stream_index].stream_size += pkt->size;
289 return 0;
290}
291
292static const AVClass streamhashenc_class = {
293 .class_name = "stream hash muxer",
294 .item_name = av_default_item_name,
295 .option = hash_options,
296 .version = LIBAVUTIL_VERSION_INT,
297};
298
300 .p.name = "streamhash",
301 .p.long_name = NULL_IF_CONFIG_SMALL("Per-stream hash testing"),
302 .priv_data_size = sizeof(struct HashContext),
303 .p.audio_codec = AV_CODEC_ID_PCM_S16LE,
304 .p.video_codec = AV_CODEC_ID_RAWVIDEO,
305 .init = streamhash_init,
306 .write_header = streamhash_write_header,
307 .write_packet = streamhash_write_packet,
308 .write_trailer = hash_write_trailer,
309 .deinit = hash_free,
312 .p.priv_class = &streamhashenc_class,
313};
314#endif
315
316#if CONFIG_FRAMEHASH_MUXER || CONFIG_FRAMEMD5_MUXER
317static int framehash_init(struct AVFormatContext *s)
318{
319 int res;
320 struct HashContext *c = s->priv_data;
321 c->per_stream = 0;
322 c->hashes = av_mallocz(sizeof(*c->hashes));
323 if (!c->hashes)
324 return AVERROR(ENOMEM);
325 res = av_hash_alloc(&c->hashes->hash, c->hash_name);
326 if (res < 0)
327 return res;
328 return 0;
329}
330
331static int framehash_write_header(struct AVFormatContext *s)
332{
333 struct HashContext *c = s->priv_data;
334 avio_printf(s->pb, "#format: frame checksums\n");
335 avio_printf(s->pb, "#version: %d\n", c->format_version);
336 avio_printf(s->pb, "#hash: %s\n", av_hash_get_name(c->hashes->hash));
337 hash_print_extradata(s);
339 avio_printf(s->pb, "#stream#, dts, pts, duration, size, hash\n");
340 return 0;
341}
342
343static int framehash_write_packet(struct AVFormatContext *s, AVPacket *pkt)
344{
345 struct HashContext *c = s->priv_data;
346 char buf[AV_HASH_MAX_SIZE*2+128];
347 int len;
348 av_hash_init(c->hashes->hash);
349 av_hash_update(c->hashes->hash, pkt->data, pkt->size);
350
351 snprintf(buf, sizeof(buf) - (AV_HASH_MAX_SIZE * 2 + 1), "%d, %10"PRId64", %10"PRId64", %8"PRId64", %8d, ",
352 pkt->stream_index, pkt->dts, pkt->pts, pkt->duration, pkt->size);
353 len = strlen(buf);
354 av_hash_final_hex(c->hashes->hash, buf + len, sizeof(buf) - len);
355 avio_write(s->pb, buf, strlen(buf));
356
357 if (c->format_version > 1 && pkt->side_data_elems) {
358 hash_print_sidedata(s, pkt->side_data, pkt->side_data_elems);
359 }
360
361 avio_printf(s->pb, "\n");
362 return 0;
363}
364#endif
365
366#if CONFIG_FRAMEHASH_MUXER
367static const AVClass framehash_class = {
368 .class_name = "frame hash muxer",
369 .item_name = av_default_item_name,
370 .option = hash_options,
371 .version = LIBAVUTIL_VERSION_INT,
372};
373
375 .p.name = "framehash",
376 .p.long_name = NULL_IF_CONFIG_SMALL("Per-frame hash testing"),
377 .priv_data_size = sizeof(struct HashContext),
378 .p.audio_codec = AV_CODEC_ID_PCM_S16LE,
379 .p.video_codec = AV_CODEC_ID_RAWVIDEO,
380 .init = framehash_init,
381 .write_header = framehash_write_header,
382 .write_packet = framehash_write_packet,
383 .deinit = hash_free,
386 .p.priv_class = &framehash_class,
387};
388#endif
389
390#if CONFIG_FRAMEMD5_MUXER
391static const AVClass framemd5_class = {
392 .class_name = "frame MD5 muxer",
393 .item_name = av_default_item_name,
394 .option = framemd5_options,
395 .version = LIBAVUTIL_VERSION_INT,
396};
397
399 .p.name = "framemd5",
400 .p.long_name = NULL_IF_CONFIG_SMALL("Per-frame MD5 testing"),
401 .priv_data_size = sizeof(struct HashContext),
402 .p.audio_codec = AV_CODEC_ID_PCM_S16LE,
403 .p.video_codec = AV_CODEC_ID_RAWVIDEO,
404 .init = framehash_init,
405 .write_header = framehash_write_header,
406 .write_packet = framehash_write_packet,
407 .deinit = hash_free,
410 .p.priv_class = &framemd5_class,
411};
412#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:503
#define AVFMT_TS_NEGATIVE
Format allows muxing negative timestamps.
Definition avformat.h:512
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition avformat.h:509
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:332
@ AV_PKT_DATA_IAMF_RECON_GAIN_INFO_PARAM
IAMF Recon Gain Info Parameter Data associated with the audio frame.
Definition packet.h:320
@ AV_PKT_DATA_IAMF_DEMIXING_INFO_PARAM
IAMF Demixing Info Parameter Data associated with the audio frame.
Definition packet.h:312
@ AV_PKT_DATA_PALETTE
An AV_PKT_DATA_PALETTE side data packet contains exactly AVPALETTE_SIZE bytes worth of palette.
Definition packet.h:47
@ AV_PKT_DATA_IAMF_MIX_GAIN_PARAM
IAMF Mix Gain Parameter Data associated with the audio frame.
Definition packet.h:304
#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:190
#define AV_RL32(p)
uint32_t type
Definition jpegmpfenc.c:80
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:97
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:370
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
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:1400
AVOption.
Definition opt.h:428
This structure stores auxiliary information for decoding, presenting, or otherwise processing the cod...
Definition packet.h:431
uint8_t * data
Definition packet.h:432
This structure stores compressed data.
Definition packet.h:587
Stream structure.
Definition avformat.h:768
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:791
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]