FFmpeg
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 "libavutil/avassert.h"
23 #include "libavutil/avstring.h"
24 #include "libavutil/hash.h"
25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/opt.h"
27 #include "avformat.h"
28 #include "internal.h"
29 
30 struct HashContext {
31  const AVClass *avclass;
33  char *hash_name;
36 };
37 
38 #define OFFSET(x) offsetof(struct HashContext, x)
39 #define ENC AV_OPT_FLAG_ENCODING_PARAM
40 #define HASH_OPT(defaulttype) \
41  { "hash", "set hash to use", OFFSET(hash_name), AV_OPT_TYPE_STRING, {.str = defaulttype}, 0, 0, ENC }
42 #define FORMAT_VERSION_OPT \
43  { "format_version", "file format version", OFFSET(format_version), AV_OPT_TYPE_INT, {.i64 = 2}, 1, 2, ENC }
44 
45 #if CONFIG_HASH_MUXER
46 static const AVOption hash_options[] = {
47  HASH_OPT("sha256"),
48  { NULL },
49 };
50 #endif
51 
52 #if CONFIG_FRAMEHASH_MUXER
53 static const AVOption framehash_options[] = {
54  HASH_OPT("sha256"),
56  { NULL },
57 };
58 #endif
59 
60 #if CONFIG_STREAMHASH_MUXER
61 static const AVOption streamhash_options[] = {
62  HASH_OPT("sha256"),
63  { NULL },
64 };
65 #endif
66 
67 #if CONFIG_MD5_MUXER
68 static const AVOption md5_options[] = {
69  HASH_OPT("md5"),
70  { NULL },
71 };
72 #endif
73 
74 #if CONFIG_FRAMEMD5_MUXER
75 static const AVOption framemd5_options[] = {
76  HASH_OPT("md5"),
78  { NULL },
79 };
80 #endif
81 
82 #if CONFIG_HASH_MUXER || CONFIG_MD5_MUXER
83 static int hash_init(struct AVFormatContext *s)
84 {
85  int res;
86  struct HashContext *c = s->priv_data;
87  c->per_stream = 0;
88  c->hashes = av_mallocz_array(1, sizeof(*c->hashes));
89  if (!c->hashes)
90  return AVERROR(ENOMEM);
91  res = av_hash_alloc(&c->hashes[0], c->hash_name);
92  if (res < 0)
93  return res;
94  av_hash_init(c->hashes[0]);
95  return 0;
96 }
97 #endif
98 
99 #if CONFIG_STREAMHASH_MUXER
100 static int streamhash_init(struct AVFormatContext *s)
101 {
102  int res, i;
103  struct HashContext *c = s->priv_data;
104  c->per_stream = 1;
105  c->hashes = av_mallocz_array(s->nb_streams, sizeof(*c->hashes));
106  if (!c->hashes)
107  return AVERROR(ENOMEM);
108  for (i = 0; i < s->nb_streams; i++) {
109  res = av_hash_alloc(&c->hashes[i], c->hash_name);
110  if (res < 0) {
111  return res;
112  }
113  av_hash_init(c->hashes[i]);
114  }
115  return 0;
116 }
117 #endif
118 
119 #if CONFIG_HASH_MUXER || CONFIG_MD5_MUXER || CONFIG_STREAMHASH_MUXER
120 static char get_media_type_char(enum AVMediaType type)
121 {
122  switch (type) {
123  case AVMEDIA_TYPE_VIDEO: return 'v';
124  case AVMEDIA_TYPE_AUDIO: return 'a';
125  case AVMEDIA_TYPE_DATA: return 'd';
126  case AVMEDIA_TYPE_SUBTITLE: return 's';
127  case AVMEDIA_TYPE_ATTACHMENT: return 't';
128  default: return '?';
129  }
130 }
131 
132 static int hash_write_packet(struct AVFormatContext *s, AVPacket *pkt)
133 {
134  struct HashContext *c = s->priv_data;
135  av_hash_update(c->hashes[c->per_stream ? pkt->stream_index : 0], pkt->data, pkt->size);
136  return 0;
137 }
138 
139 static int hash_write_trailer(struct AVFormatContext *s)
140 {
141  struct HashContext *c = s->priv_data;
142  int num_hashes = c->per_stream ? s->nb_streams : 1;
143  for (int i = 0; i < num_hashes; i++) {
144  char buf[AV_HASH_MAX_SIZE*2+128];
145  if (c->per_stream) {
146  AVStream *st = s->streams[i];
147  snprintf(buf, sizeof(buf) - 200, "%d,%c,%s=", i, get_media_type_char(st->codecpar->codec_type),
148  av_hash_get_name(c->hashes[i]));
149  } else {
150  snprintf(buf, sizeof(buf) - 200, "%s=", av_hash_get_name(c->hashes[i]));
151  }
152  av_hash_final_hex(c->hashes[i], buf + strlen(buf), sizeof(buf) - strlen(buf));
153  av_strlcatf(buf, sizeof(buf), "\n");
154  avio_write(s->pb, buf, strlen(buf));
155  }
156 
157  return 0;
158 }
159 
160 static void hash_free(struct AVFormatContext *s)
161 {
162  struct HashContext *c = s->priv_data;
163  if (c->hashes) {
164  int num_hashes = c->per_stream ? s->nb_streams : 1;
165  for (int i = 0; i < num_hashes; i++) {
166  av_hash_freep(&c->hashes[i]);
167  }
168  }
169  av_freep(&c->hashes);
170 }
171 #endif
172 
173 #if CONFIG_HASH_MUXER
174 static const AVClass hashenc_class = {
175  .class_name = "hash muxer",
176  .item_name = av_default_item_name,
177  .option = hash_options,
178  .version = LIBAVUTIL_VERSION_INT,
179 };
180 
182  .name = "hash",
183  .long_name = NULL_IF_CONFIG_SMALL("Hash testing"),
184  .priv_data_size = sizeof(struct HashContext),
185  .audio_codec = AV_CODEC_ID_PCM_S16LE,
186  .video_codec = AV_CODEC_ID_RAWVIDEO,
187  .init = hash_init,
188  .write_packet = hash_write_packet,
189  .write_trailer = hash_write_trailer,
190  .deinit = hash_free,
193  .priv_class = &hashenc_class,
194 };
195 #endif
196 
197 #if CONFIG_MD5_MUXER
198 static const AVClass md5enc_class = {
199  .class_name = "MD5 muxer",
200  .item_name = av_default_item_name,
201  .option = md5_options,
202  .version = LIBAVUTIL_VERSION_INT,
203 };
204 
206  .name = "md5",
207  .long_name = NULL_IF_CONFIG_SMALL("MD5 testing"),
208  .priv_data_size = sizeof(struct HashContext),
209  .audio_codec = AV_CODEC_ID_PCM_S16LE,
210  .video_codec = AV_CODEC_ID_RAWVIDEO,
211  .init = hash_init,
212  .write_packet = hash_write_packet,
213  .write_trailer = hash_write_trailer,
214  .deinit = hash_free,
217  .priv_class = &md5enc_class,
218 };
219 #endif
220 
221 #if CONFIG_STREAMHASH_MUXER
222 static const AVClass streamhashenc_class = {
223  .class_name = "stream hash muxer",
224  .item_name = av_default_item_name,
225  .option = streamhash_options,
226  .version = LIBAVUTIL_VERSION_INT,
227 };
228 
230  .name = "streamhash",
231  .long_name = NULL_IF_CONFIG_SMALL("Per-stream hash testing"),
232  .priv_data_size = sizeof(struct HashContext),
233  .audio_codec = AV_CODEC_ID_PCM_S16LE,
234  .video_codec = AV_CODEC_ID_RAWVIDEO,
235  .init = streamhash_init,
236  .write_packet = hash_write_packet,
237  .write_trailer = hash_write_trailer,
238  .deinit = hash_free,
241  .priv_class = &streamhashenc_class,
242 };
243 #endif
244 
245 #if CONFIG_FRAMEHASH_MUXER || CONFIG_FRAMEMD5_MUXER
246 static void framehash_print_extradata(struct AVFormatContext *s)
247 {
248  int i;
249 
250  for (i = 0; i < s->nb_streams; i++) {
251  AVStream *st = s->streams[i];
252  AVCodecParameters *par = st->codecpar;
253  if (par->extradata) {
254  struct HashContext *c = s->priv_data;
255  char buf[AV_HASH_MAX_SIZE*2+1];
256 
257  avio_printf(s->pb, "#extradata %d, %31d, ", i, par->extradata_size);
258  av_hash_init(c->hashes[0]);
259  av_hash_update(c->hashes[0], par->extradata, par->extradata_size);
260  av_hash_final_hex(c->hashes[0], buf, sizeof(buf));
261  avio_write(s->pb, buf, strlen(buf));
262  avio_printf(s->pb, "\n");
263  }
264  }
265 }
266 
267 static int framehash_init(struct AVFormatContext *s)
268 {
269  int res;
270  struct HashContext *c = s->priv_data;
271  c->per_stream = 0;
272  c->hashes = av_mallocz_array(1, sizeof(*c->hashes));
273  if (!c->hashes)
274  return AVERROR(ENOMEM);
275  res = av_hash_alloc(&c->hashes[0], c->hash_name);
276  if (res < 0)
277  return res;
278  return 0;
279 }
280 
281 static int framehash_write_header(struct AVFormatContext *s)
282 {
283  struct HashContext *c = s->priv_data;
284  avio_printf(s->pb, "#format: frame checksums\n");
285  avio_printf(s->pb, "#version: %d\n", c->format_version);
286  avio_printf(s->pb, "#hash: %s\n", av_hash_get_name(c->hashes[0]));
287  framehash_print_extradata(s);
289  avio_printf(s->pb, "#stream#, dts, pts, duration, size, hash\n");
290  return 0;
291 }
292 
293 static int framehash_write_packet(struct AVFormatContext *s, AVPacket *pkt)
294 {
295  struct HashContext *c = s->priv_data;
296  char buf[AV_HASH_MAX_SIZE*2+128];
297  int len;
298  av_hash_init(c->hashes[0]);
299  av_hash_update(c->hashes[0], pkt->data, pkt->size);
300 
301  snprintf(buf, sizeof(buf) - (AV_HASH_MAX_SIZE * 2 + 1), "%d, %10"PRId64", %10"PRId64", %8"PRId64", %8d, ",
303  len = strlen(buf);
304  av_hash_final_hex(c->hashes[0], buf + len, sizeof(buf) - len);
305  avio_write(s->pb, buf, strlen(buf));
306 
307  if (c->format_version > 1 && pkt->side_data_elems) {
308  int i, j;
309  avio_printf(s->pb, ", S=%d", pkt->side_data_elems);
310  for (i = 0; i < pkt->side_data_elems; i++) {
311  av_hash_init(c->hashes[0]);
312  if (HAVE_BIGENDIAN && pkt->side_data[i].type == AV_PKT_DATA_PALETTE) {
313  for (j = 0; j < pkt->side_data[i].size; j += sizeof(uint32_t)) {
314  uint32_t data = AV_RL32(pkt->side_data[i].data + j);
315  av_hash_update(c->hashes[0], (uint8_t *)&data, sizeof(uint32_t));
316  }
317  } else
318  av_hash_update(c->hashes[0], pkt->side_data[i].data, pkt->side_data[i].size);
319  snprintf(buf, sizeof(buf) - (AV_HASH_MAX_SIZE * 2 + 1), ", %8d, ", pkt->side_data[i].size);
320  len = strlen(buf);
321  av_hash_final_hex(c->hashes[0], buf + len, sizeof(buf) - len);
322  avio_write(s->pb, buf, strlen(buf));
323  }
324  }
325 
326  avio_printf(s->pb, "\n");
327  return 0;
328 }
329 
330 static void framehash_free(struct AVFormatContext *s)
331 {
332  struct HashContext *c = s->priv_data;
333  if (c->hashes)
334  av_hash_freep(&c->hashes[0]);
335  av_freep(&c->hashes);
336 }
337 #endif
338 
339 #if CONFIG_FRAMEHASH_MUXER
340 static const AVClass framehash_class = {
341  .class_name = "frame hash muxer",
342  .item_name = av_default_item_name,
343  .option = framehash_options,
344  .version = LIBAVUTIL_VERSION_INT,
345 };
346 
348  .name = "framehash",
349  .long_name = NULL_IF_CONFIG_SMALL("Per-frame hash testing"),
350  .priv_data_size = sizeof(struct HashContext),
351  .audio_codec = AV_CODEC_ID_PCM_S16LE,
352  .video_codec = AV_CODEC_ID_RAWVIDEO,
353  .init = framehash_init,
354  .write_header = framehash_write_header,
355  .write_packet = framehash_write_packet,
356  .deinit = framehash_free,
359  .priv_class = &framehash_class,
360 };
361 #endif
362 
363 #if CONFIG_FRAMEMD5_MUXER
364 static const AVClass framemd5_class = {
365  .class_name = "frame MD5 muxer",
366  .item_name = av_default_item_name,
367  .option = framemd5_options,
368  .version = LIBAVUTIL_VERSION_INT,
369 };
370 
372  .name = "framemd5",
373  .long_name = NULL_IF_CONFIG_SMALL("Per-frame MD5 testing"),
374  .priv_data_size = sizeof(struct HashContext),
375  .audio_codec = AV_CODEC_ID_PCM_S16LE,
376  .video_codec = AV_CODEC_ID_RAWVIDEO,
377  .init = framehash_init,
378  .write_header = framehash_write_header,
379  .write_packet = framehash_write_packet,
380  .deinit = framehash_free,
383  .priv_class = &framemd5_class,
384 };
385 #endif
AV_CODEC_ID_PCM_S16LE
@ AV_CODEC_ID_PCM_S16LE
Definition: codec_id.h:301
AVMEDIA_TYPE_SUBTITLE
@ AVMEDIA_TYPE_SUBTITLE
Definition: avutil.h:204
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:74
AVOutputFormat::name
const char * name
Definition: avformat.h:491
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
AV_HASH_MAX_SIZE
#define AV_HASH_MAX_SIZE
Maximum value that av_hash_get_size() will currently return.
Definition: hash.h:157
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:52
AVFMT_VARIABLE_FPS
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:465
get_media_type_char
static char get_media_type_char(enum AVMediaType type)
Definition: cmdutils.c:1484
AV_CODEC_ID_RAWVIDEO
@ AV_CODEC_ID_RAWVIDEO
Definition: codec_id.h:62
AVPacket::data
uint8_t * data
Definition: packet.h:355
AVOption
AVOption.
Definition: opt.h:246
AV_PKT_DATA_PALETTE
@ AV_PKT_DATA_PALETTE
An AV_PKT_DATA_PALETTE side data packet contains exactly AVPALETTE_SIZE bytes worth of palette.
Definition: packet.h:46
data
const char data[16]
Definition: mxf.c:91
av_mallocz_array
void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.c:190
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:373
av_strlcatf
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
Definition: avstring.c:101
av_hash_get_name
const char * av_hash_get_name(const AVHashContext *ctx)
Definition: hash.c:90
FORMAT_VERSION_OPT
#define FORMAT_VERSION_OPT
Definition: hashenc.c:42
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
avassert.h
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:257
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
av_hash_alloc
int av_hash_alloc(AVHashContext **ctx, const char *name)
Allocate a hash context for the algorithm specified by name.
Definition: hash.c:100
AVPacketSideData::data
uint8_t * data
Definition: packet.h:299
AVMEDIA_TYPE_DATA
@ AVMEDIA_TYPE_DATA
Opaque data information usually continuous.
Definition: avutil.h:203
AVFormatContext
Format I/O context.
Definition: avformat.h:1335
internal.h
HashContext
Definition: hashenc.c:30
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1012
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
NULL
#define NULL
Definition: coverity.c:32
av_hash_init
void av_hash_init(AVHashContext *ctx)
Initialize or reset a hash context.
Definition: hash.c:137
AVPacketSideData::type
enum AVPacketSideDataType type
Definition: packet.h:301
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
av_hash_freep
void av_hash_freep(AVHashContext **ctx)
Free hash context and set hash context pointer to NULL.
Definition: hash.c:238
AVCodecParameters::extradata_size
int extradata_size
Size of the extradata content in bytes.
Definition: codec_par.h:78
HashContext::hash_name
char * hash_name
Definition: hashenc.c:33
AVMediaType
AVMediaType
Definition: avutil.h:199
AVPacket::size
int size
Definition: packet.h:356
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:188
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:354
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:213
AVPacketSideData::size
int size
Definition: packet.h:300
AVHashContext
Definition: hash.c:56
ff_streamhash_muxer
AVOutputFormat ff_streamhash_muxer
HashContext::hashes
struct AVHashContext ** hashes
Definition: hashenc.c:32
AVOutputFormat
Definition: avformat.h:490
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:348
AVMEDIA_TYPE_ATTACHMENT
@ AVMEDIA_TYPE_ATTACHMENT
Opaque data information usually sparse.
Definition: avutil.h:205
uint8_t
uint8_t
Definition: audio_convert.c:194
len
int len
Definition: vorbis_enc_data.h:452
AVFMT_TS_NEGATIVE
#define AVFMT_TS_NEGATIVE
Format allows muxing negative timestamps.
Definition: avformat.h:475
AVFMT_TS_NONSTRICT
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition: avformat.h:472
AVStream
Stream structure.
Definition: avformat.h:865
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
avformat.h
ff_framehash_muxer
AVOutputFormat ff_framehash_muxer
AVPacket::side_data
AVPacketSideData * side_data
Additional packet data that can be provided by the container.
Definition: packet.h:366
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:88
avio_printf
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2
Writes a formatted string to the context.
HashContext::format_version
int format_version
Definition: hashenc.c:35
hash.h
ff_md5_muxer
AVOutputFormat ff_md5_muxer
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
HashContext::per_stream
int per_stream
Definition: hashenc.c:34
AVPacket::stream_index
int stream_index
Definition: packet.h:357
av_hash_update
void av_hash_update(AVHashContext *ctx, const uint8_t *src, int len)
Update a hash context with additional data.
Definition: hash.c:159
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
av_hash_final_hex
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:215
HASH_OPT
#define HASH_OPT(defaulttype)
Definition: hashenc.c:40
AVPacket
This structure stores compressed data.
Definition: packet.h:332
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
ff_framehash_write_header
int ff_framehash_write_header(AVFormatContext *s)
Set the timebase for each stream from the corresponding codec timebase and print it.
Definition: framehash.c:23
HashContext::avclass
const AVClass * avclass
Definition: hashenc.c:31
avstring.h
ff_framemd5_muxer
AVOutputFormat ff_framemd5_muxer
snprintf
#define snprintf
Definition: snprintf.h:34
AVPacket::side_data_elems
int side_data_elems
Definition: packet.h:367
ff_hash_muxer
AVOutputFormat ff_hash_muxer