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 "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 #include "mux.h"
31 
32 struct HashContext {
33  const AVClass *avclass;
35  char *hash_name;
38 };
39 
40 #define OFFSET(x) offsetof(struct HashContext, x)
41 #define ENC AV_OPT_FLAG_ENCODING_PARAM
42 #define HASH_OPT(defaulttype) \
43  { "hash", "set hash to use", OFFSET(hash_name), AV_OPT_TYPE_STRING, {.str = defaulttype}, 0, 0, ENC }
44 #define FORMAT_VERSION_OPT \
45  { "format_version", "file format version", OFFSET(format_version), AV_OPT_TYPE_INT, {.i64 = 2}, 1, 2, ENC }
46 
47 #if CONFIG_HASH_MUXER || CONFIG_STREAMHASH_MUXER
48 static const AVOption hash_streamhash_options[] = {
49  HASH_OPT("sha256"),
50  { NULL },
51 };
52 
53 static const AVClass hash_streamhashenc_class = {
54  .class_name = "(stream) hash muxer",
55  .item_name = av_default_item_name,
56  .option = hash_streamhash_options,
57  .version = LIBAVUTIL_VERSION_INT,
58 };
59 #endif
60 
61 #if CONFIG_FRAMEHASH_MUXER
62 static const AVOption framehash_options[] = {
63  HASH_OPT("sha256"),
65  { NULL },
66 };
67 #endif
68 
69 #if CONFIG_MD5_MUXER
70 static const AVOption md5_options[] = {
71  HASH_OPT("md5"),
72  { NULL },
73 };
74 #endif
75 
76 #if CONFIG_FRAMEMD5_MUXER
77 static const AVOption framemd5_options[] = {
78  HASH_OPT("md5"),
80  { NULL },
81 };
82 #endif
83 
84 #if CONFIG_HASH_MUXER || CONFIG_MD5_MUXER
85 static int hash_init(struct AVFormatContext *s)
86 {
87  int res;
88  struct HashContext *c = s->priv_data;
89  c->per_stream = 0;
90  c->hashes = av_mallocz(sizeof(*c->hashes));
91  if (!c->hashes)
92  return AVERROR(ENOMEM);
93  res = av_hash_alloc(&c->hashes[0], c->hash_name);
94  if (res < 0)
95  return res;
96  av_hash_init(c->hashes[0]);
97  return 0;
98 }
99 #endif
100 
101 #if CONFIG_STREAMHASH_MUXER
102 static int streamhash_init(struct AVFormatContext *s)
103 {
104  int res, i;
105  struct HashContext *c = s->priv_data;
106  c->per_stream = 1;
107  c->hashes = av_calloc(s->nb_streams, sizeof(*c->hashes));
108  if (!c->hashes)
109  return AVERROR(ENOMEM);
110  for (i = 0; i < s->nb_streams; i++) {
111  res = av_hash_alloc(&c->hashes[i], c->hash_name);
112  if (res < 0) {
113  return res;
114  }
115  av_hash_init(c->hashes[i]);
116  }
117  return 0;
118 }
119 #endif
120 
121 #if CONFIG_HASH_MUXER || CONFIG_MD5_MUXER || CONFIG_STREAMHASH_MUXER
122 static char get_media_type_char(enum AVMediaType type)
123 {
124  switch (type) {
125  case AVMEDIA_TYPE_VIDEO: return 'v';
126  case AVMEDIA_TYPE_AUDIO: return 'a';
127  case AVMEDIA_TYPE_DATA: return 'd';
128  case AVMEDIA_TYPE_SUBTITLE: return 's';
129  case AVMEDIA_TYPE_ATTACHMENT: return 't';
130  default: return '?';
131  }
132 }
133 
134 static int hash_write_packet(struct AVFormatContext *s, AVPacket *pkt)
135 {
136  struct HashContext *c = s->priv_data;
137  av_hash_update(c->hashes[c->per_stream ? pkt->stream_index : 0], pkt->data, pkt->size);
138  return 0;
139 }
140 
141 static int hash_write_trailer(struct AVFormatContext *s)
142 {
143  struct HashContext *c = s->priv_data;
144  int num_hashes = c->per_stream ? s->nb_streams : 1;
145  for (int i = 0; i < num_hashes; i++) {
146  char buf[AV_HASH_MAX_SIZE*2+128];
147  if (c->per_stream) {
148  AVStream *st = s->streams[i];
149  snprintf(buf, sizeof(buf) - 200, "%d,%c,%s=", i, get_media_type_char(st->codecpar->codec_type),
150  av_hash_get_name(c->hashes[i]));
151  } else {
152  snprintf(buf, sizeof(buf) - 200, "%s=", av_hash_get_name(c->hashes[i]));
153  }
154  av_hash_final_hex(c->hashes[i], buf + strlen(buf), sizeof(buf) - strlen(buf));
155  av_strlcatf(buf, sizeof(buf), "\n");
156  avio_write(s->pb, buf, strlen(buf));
157  }
158 
159  return 0;
160 }
161 #endif
162 
163 static void hash_free(struct AVFormatContext *s)
164 {
165  struct HashContext *c = s->priv_data;
166  if (c->hashes) {
167  int num_hashes = c->per_stream ? s->nb_streams : 1;
168  for (int i = 0; i < num_hashes; i++) {
169  av_hash_freep(&c->hashes[i]);
170  }
171  }
172  av_freep(&c->hashes);
173 }
174 
175 #if CONFIG_HASH_MUXER
177  .p.name = "hash",
178  .p.long_name = NULL_IF_CONFIG_SMALL("Hash testing"),
179  .priv_data_size = sizeof(struct HashContext),
180  .p.audio_codec = AV_CODEC_ID_PCM_S16LE,
181  .p.video_codec = AV_CODEC_ID_RAWVIDEO,
182  .init = hash_init,
183  .write_packet = hash_write_packet,
184  .write_trailer = hash_write_trailer,
185  .deinit = hash_free,
188  .p.priv_class = &hash_streamhashenc_class,
189 };
190 #endif
191 
192 #if CONFIG_MD5_MUXER
193 static const AVClass md5enc_class = {
194  .class_name = "MD5 muxer",
195  .item_name = av_default_item_name,
196  .option = md5_options,
197  .version = LIBAVUTIL_VERSION_INT,
198 };
199 
201  .p.name = "md5",
202  .p.long_name = NULL_IF_CONFIG_SMALL("MD5 testing"),
203  .priv_data_size = sizeof(struct HashContext),
204  .p.audio_codec = AV_CODEC_ID_PCM_S16LE,
205  .p.video_codec = AV_CODEC_ID_RAWVIDEO,
206  .init = hash_init,
207  .write_packet = hash_write_packet,
208  .write_trailer = hash_write_trailer,
209  .deinit = hash_free,
212  .p.priv_class = &md5enc_class,
213 };
214 #endif
215 
216 #if CONFIG_STREAMHASH_MUXER
218  .p.name = "streamhash",
219  .p.long_name = NULL_IF_CONFIG_SMALL("Per-stream hash testing"),
220  .priv_data_size = sizeof(struct HashContext),
221  .p.audio_codec = AV_CODEC_ID_PCM_S16LE,
222  .p.video_codec = AV_CODEC_ID_RAWVIDEO,
223  .init = streamhash_init,
224  .write_packet = hash_write_packet,
225  .write_trailer = hash_write_trailer,
226  .deinit = hash_free,
229  .p.priv_class = &hash_streamhashenc_class,
230 };
231 #endif
232 
233 #if CONFIG_FRAMEHASH_MUXER || CONFIG_FRAMEMD5_MUXER
234 static void framehash_print_extradata(struct AVFormatContext *s)
235 {
236  int i;
237 
238  for (i = 0; i < s->nb_streams; i++) {
239  AVStream *st = s->streams[i];
240  AVCodecParameters *par = st->codecpar;
241  if (par->extradata) {
242  struct HashContext *c = s->priv_data;
243  char buf[AV_HASH_MAX_SIZE*2+1];
244 
245  avio_printf(s->pb, "#extradata %d, %31d, ", i, par->extradata_size);
246  av_hash_init(c->hashes[0]);
247  av_hash_update(c->hashes[0], par->extradata, par->extradata_size);
248  av_hash_final_hex(c->hashes[0], buf, sizeof(buf));
249  avio_write(s->pb, buf, strlen(buf));
250  avio_printf(s->pb, "\n");
251  }
252  }
253 }
254 
255 static int framehash_init(struct AVFormatContext *s)
256 {
257  int res;
258  struct HashContext *c = s->priv_data;
259  c->per_stream = 0;
260  c->hashes = av_mallocz(sizeof(*c->hashes));
261  if (!c->hashes)
262  return AVERROR(ENOMEM);
263  res = av_hash_alloc(&c->hashes[0], c->hash_name);
264  if (res < 0)
265  return res;
266  return 0;
267 }
268 
269 static int framehash_write_header(struct AVFormatContext *s)
270 {
271  struct HashContext *c = s->priv_data;
272  avio_printf(s->pb, "#format: frame checksums\n");
273  avio_printf(s->pb, "#version: %d\n", c->format_version);
274  avio_printf(s->pb, "#hash: %s\n", av_hash_get_name(c->hashes[0]));
275  framehash_print_extradata(s);
277  avio_printf(s->pb, "#stream#, dts, pts, duration, size, hash\n");
278  return 0;
279 }
280 
281 static int framehash_write_packet(struct AVFormatContext *s, AVPacket *pkt)
282 {
283  struct HashContext *c = s->priv_data;
284  char buf[AV_HASH_MAX_SIZE*2+128];
285  int len;
286  av_hash_init(c->hashes[0]);
287  av_hash_update(c->hashes[0], pkt->data, pkt->size);
288 
289  snprintf(buf, sizeof(buf) - (AV_HASH_MAX_SIZE * 2 + 1), "%d, %10"PRId64", %10"PRId64", %8"PRId64", %8d, ",
291  len = strlen(buf);
292  av_hash_final_hex(c->hashes[0], buf + len, sizeof(buf) - len);
293  avio_write(s->pb, buf, strlen(buf));
294 
295  if (c->format_version > 1 && pkt->side_data_elems) {
296  int i;
297  avio_printf(s->pb, ", S=%d", pkt->side_data_elems);
298  for (i = 0; i < pkt->side_data_elems; i++) {
299  av_hash_init(c->hashes[0]);
300  if (HAVE_BIGENDIAN && pkt->side_data[i].type == AV_PKT_DATA_PALETTE) {
301  for (size_t j = 0; j < pkt->side_data[i].size; j += sizeof(uint32_t)) {
302  uint32_t data = AV_RL32(pkt->side_data[i].data + j);
303  av_hash_update(c->hashes[0], (uint8_t *)&data, sizeof(uint32_t));
304  }
305  } else
306  av_hash_update(c->hashes[0], pkt->side_data[i].data, pkt->side_data[i].size);
307  snprintf(buf, sizeof(buf) - (AV_HASH_MAX_SIZE * 2 + 1),
308  ", %8"SIZE_SPECIFIER", ", pkt->side_data[i].size);
309  len = strlen(buf);
310  av_hash_final_hex(c->hashes[0], buf + len, sizeof(buf) - len);
311  avio_write(s->pb, buf, strlen(buf));
312  }
313  }
314 
315  avio_printf(s->pb, "\n");
316  return 0;
317 }
318 #endif
319 
320 #if CONFIG_FRAMEHASH_MUXER
321 static const AVClass framehash_class = {
322  .class_name = "frame hash muxer",
323  .item_name = av_default_item_name,
324  .option = framehash_options,
325  .version = LIBAVUTIL_VERSION_INT,
326 };
327 
329  .p.name = "framehash",
330  .p.long_name = NULL_IF_CONFIG_SMALL("Per-frame hash testing"),
331  .priv_data_size = sizeof(struct HashContext),
332  .p.audio_codec = AV_CODEC_ID_PCM_S16LE,
333  .p.video_codec = AV_CODEC_ID_RAWVIDEO,
334  .init = framehash_init,
335  .write_header = framehash_write_header,
336  .write_packet = framehash_write_packet,
337  .deinit = hash_free,
340  .p.priv_class = &framehash_class,
341 };
342 #endif
343 
344 #if CONFIG_FRAMEMD5_MUXER
345 static const AVClass framemd5_class = {
346  .class_name = "frame MD5 muxer",
347  .item_name = av_default_item_name,
348  .option = framemd5_options,
349  .version = LIBAVUTIL_VERSION_INT,
350 };
351 
353  .p.name = "framemd5",
354  .p.long_name = NULL_IF_CONFIG_SMALL("Per-frame MD5 testing"),
355  .priv_data_size = sizeof(struct HashContext),
356  .p.audio_codec = AV_CODEC_ID_PCM_S16LE,
357  .p.video_codec = AV_CODEC_ID_RAWVIDEO,
358  .init = framehash_init,
359  .write_header = framehash_write_header,
360  .write_packet = framehash_write_packet,
361  .deinit = hash_free,
364  .p.priv_class = &framemd5_class,
365 };
366 #endif
AV_CODEC_ID_PCM_S16LE
@ AV_CODEC_ID_PCM_S16LE
Definition: codec_id.h:328
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:69
AVOutputFormat::name
const char * name
Definition: avformat.h:510
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:51
AV_HASH_MAX_SIZE
#define AV_HASH_MAX_SIZE
Maximum value that av_hash_get_size() will currently return.
Definition: hash.h:156
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:47
AVFMT_VARIABLE_FPS
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:482
AV_CODEC_ID_RAWVIDEO
@ AV_CODEC_ID_RAWVIDEO
Definition: codec_id.h:65
AVPacket::data
uint8_t * data
Definition: packet.h:522
AVOption
AVOption.
Definition: opt.h:346
data
const char data[16]
Definition: mxf.c:148
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:540
get_media_type_char
static char get_media_type_char(enum AVMediaType type)
Definition: opt_common.c:654
av_strlcatf
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
Definition: avstring.c:103
FFOutputFormat::p
AVOutputFormat p
The public AVOutputFormat.
Definition: mux.h:65
ff_framemd5_muxer
const FFOutputFormat ff_framemd5_muxer
AVPacketSideData::size
size_t size
Definition: packet.h:375
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:47
av_hash_get_name
const char * av_hash_get_name(const AVHashContext *ctx)
Definition: hash.c:104
FORMAT_VERSION_OPT
#define FORMAT_VERSION_OPT
Definition: hashenc.c:44
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
pkt
AVPacket * pkt
Definition: movenc.c:59
hash_free
static void hash_free(struct AVFormatContext *s)
Definition: hashenc.c:163
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
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:114
AVPacketSideData::data
uint8_t * data
Definition: packet.h:374
AVMEDIA_TYPE_DATA
@ AVMEDIA_TYPE_DATA
Opaque data information usually continuous.
Definition: avutil.h:203
AVFormatContext
Format I/O context.
Definition: avformat.h:1255
internal.h
HashContext
Definition: hashenc.c:32
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:766
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
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:151
AVPacketSideData::type
enum AVPacketSideDataType type
Definition: packet.h:376
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
FFOutputFormat
Definition: mux.h:61
av_hash_update
void av_hash_update(AVHashContext *ctx, const uint8_t *src, size_t len)
Update a hash context with additional data.
Definition: hash.c:172
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:248
AVCodecParameters::extradata_size
int extradata_size
Size of the extradata content in bytes.
Definition: codec_par.h:73
HashContext::hash_name
char * hash_name
Definition: hashenc.c:35
AVMediaType
AVMediaType
Definition: avutil.h:199
AVPacket::size
int size
Definition: packet.h:523
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:106
ff_framehash_muxer
const FFOutputFormat ff_framehash_muxer
ff_streamhash_muxer
const FFOutputFormat ff_streamhash_muxer
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:521
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:200
AVHashContext
Definition: hash.c:66
HashContext::hashes
struct AVHashContext ** hashes
Definition: hashenc.c:34
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:515
ff_hash_muxer
const FFOutputFormat ff_hash_muxer
AVMEDIA_TYPE_ATTACHMENT
@ AVMEDIA_TYPE_ATTACHMENT
Opaque data information usually sparse.
Definition: avutil.h:205
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:254
len
int len
Definition: vorbis_enc_data.h:426
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:262
AVFMT_TS_NEGATIVE
#define AVFMT_TS_NEGATIVE
Format allows muxing negative timestamps.
Definition: avformat.h:494
AVFMT_TS_NONSTRICT
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition: avformat.h:491
AVStream
Stream structure.
Definition: avformat.h:743
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:71
avformat.h
AVPacket::side_data
AVPacketSideData * side_data
Additional packet data that can be provided by the container.
Definition: packet.h:533
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
SIZE_SPECIFIER
#define SIZE_SPECIFIER
Definition: internal.h:141
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:37
ff_md5_muxer
const FFOutputFormat ff_md5_muxer
hash.h
HashContext::per_stream
int per_stream
Definition: hashenc.c:36
AVPacket::stream_index
int stream_index
Definition: packet.h:524
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:225
HASH_OPT
#define HASH_OPT(defaulttype)
Definition: hashenc.c:42
AVPacket
This structure stores compressed data.
Definition: packet.h:499
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
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:25
HashContext::avclass
const AVClass * avclass
Definition: hashenc.c:33
avstring.h
snprintf
#define snprintf
Definition: snprintf.h:34
AVPacket::side_data_elems
int side_data_elems
Definition: packet.h:534
mux.h