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 || CONFIG_STREAMHASH_MUXER
46 static const AVOption hash_streamhash_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_MD5_MUXER
61 static const AVOption md5_options[] = {
62  HASH_OPT("md5"),
63  { NULL },
64 };
65 #endif
66 
67 #if CONFIG_FRAMEMD5_MUXER
68 static const AVOption framemd5_options[] = {
69  HASH_OPT("md5"),
71  { NULL },
72 };
73 #endif
74 
75 #if CONFIG_HASH_MUXER || CONFIG_MD5_MUXER
76 static int hash_init(struct AVFormatContext *s)
77 {
78  int res;
79  struct HashContext *c = s->priv_data;
80  c->per_stream = 0;
81  c->hashes = av_mallocz_array(1, sizeof(*c->hashes));
82  if (!c->hashes)
83  return AVERROR(ENOMEM);
84  res = av_hash_alloc(&c->hashes[0], c->hash_name);
85  if (res < 0)
86  return res;
87  av_hash_init(c->hashes[0]);
88  return 0;
89 }
90 #endif
91 
92 #if CONFIG_STREAMHASH_MUXER
93 static int streamhash_init(struct AVFormatContext *s)
94 {
95  int res, i;
96  struct HashContext *c = s->priv_data;
97  c->per_stream = 1;
98  c->hashes = av_mallocz_array(s->nb_streams, sizeof(*c->hashes));
99  if (!c->hashes)
100  return AVERROR(ENOMEM);
101  for (i = 0; i < s->nb_streams; i++) {
102  res = av_hash_alloc(&c->hashes[i], c->hash_name);
103  if (res < 0) {
104  return res;
105  }
106  av_hash_init(c->hashes[i]);
107  }
108  return 0;
109 }
110 #endif
111 
112 #if CONFIG_HASH_MUXER || CONFIG_MD5_MUXER || CONFIG_STREAMHASH_MUXER
113 static char get_media_type_char(enum AVMediaType type)
114 {
115  switch (type) {
116  case AVMEDIA_TYPE_VIDEO: return 'v';
117  case AVMEDIA_TYPE_AUDIO: return 'a';
118  case AVMEDIA_TYPE_DATA: return 'd';
119  case AVMEDIA_TYPE_SUBTITLE: return 's';
120  case AVMEDIA_TYPE_ATTACHMENT: return 't';
121  default: return '?';
122  }
123 }
124 
125 static int hash_write_packet(struct AVFormatContext *s, AVPacket *pkt)
126 {
127  struct HashContext *c = s->priv_data;
128  av_hash_update(c->hashes[c->per_stream ? pkt->stream_index : 0], pkt->data, pkt->size);
129  return 0;
130 }
131 
132 static int hash_write_trailer(struct AVFormatContext *s)
133 {
134  struct HashContext *c = s->priv_data;
135  int num_hashes = c->per_stream ? s->nb_streams : 1;
136  for (int i = 0; i < num_hashes; i++) {
137  char buf[AV_HASH_MAX_SIZE*2+128];
138  if (c->per_stream) {
139  AVStream *st = s->streams[i];
140  snprintf(buf, sizeof(buf) - 200, "%d,%c,%s=", i, get_media_type_char(st->codecpar->codec_type),
141  av_hash_get_name(c->hashes[i]));
142  } else {
143  snprintf(buf, sizeof(buf) - 200, "%s=", av_hash_get_name(c->hashes[i]));
144  }
145  av_hash_final_hex(c->hashes[i], buf + strlen(buf), sizeof(buf) - strlen(buf));
146  av_strlcatf(buf, sizeof(buf), "\n");
147  avio_write(s->pb, buf, strlen(buf));
148  }
149 
150  return 0;
151 }
152 #endif
153 
154 static void hash_free(struct AVFormatContext *s)
155 {
156  struct HashContext *c = s->priv_data;
157  if (c->hashes) {
158  int num_hashes = c->per_stream ? s->nb_streams : 1;
159  for (int i = 0; i < num_hashes; i++) {
160  av_hash_freep(&c->hashes[i]);
161  }
162  }
163  av_freep(&c->hashes);
164 }
165 
166 #if CONFIG_HASH_MUXER
167 static const AVClass hashenc_class = {
168  .class_name = "hash muxer",
169  .item_name = av_default_item_name,
170  .option = hash_streamhash_options,
171  .version = LIBAVUTIL_VERSION_INT,
172 };
173 
175  .name = "hash",
176  .long_name = NULL_IF_CONFIG_SMALL("Hash testing"),
177  .priv_data_size = sizeof(struct HashContext),
178  .audio_codec = AV_CODEC_ID_PCM_S16LE,
179  .video_codec = AV_CODEC_ID_RAWVIDEO,
180  .init = hash_init,
181  .write_packet = hash_write_packet,
182  .write_trailer = hash_write_trailer,
183  .deinit = hash_free,
186  .priv_class = &hashenc_class,
187 };
188 #endif
189 
190 #if CONFIG_MD5_MUXER
191 static const AVClass md5enc_class = {
192  .class_name = "MD5 muxer",
193  .item_name = av_default_item_name,
194  .option = md5_options,
195  .version = LIBAVUTIL_VERSION_INT,
196 };
197 
199  .name = "md5",
200  .long_name = NULL_IF_CONFIG_SMALL("MD5 testing"),
201  .priv_data_size = sizeof(struct HashContext),
202  .audio_codec = AV_CODEC_ID_PCM_S16LE,
203  .video_codec = AV_CODEC_ID_RAWVIDEO,
204  .init = hash_init,
205  .write_packet = hash_write_packet,
206  .write_trailer = hash_write_trailer,
207  .deinit = hash_free,
210  .priv_class = &md5enc_class,
211 };
212 #endif
213 
214 #if CONFIG_STREAMHASH_MUXER
215 static const AVClass streamhashenc_class = {
216  .class_name = "stream hash muxer",
217  .item_name = av_default_item_name,
218  .option = hash_streamhash_options,
219  .version = LIBAVUTIL_VERSION_INT,
220 };
221 
223  .name = "streamhash",
224  .long_name = NULL_IF_CONFIG_SMALL("Per-stream hash testing"),
225  .priv_data_size = sizeof(struct HashContext),
226  .audio_codec = AV_CODEC_ID_PCM_S16LE,
227  .video_codec = AV_CODEC_ID_RAWVIDEO,
228  .init = streamhash_init,
229  .write_packet = hash_write_packet,
230  .write_trailer = hash_write_trailer,
231  .deinit = hash_free,
234  .priv_class = &streamhashenc_class,
235 };
236 #endif
237 
238 #if CONFIG_FRAMEHASH_MUXER || CONFIG_FRAMEMD5_MUXER
239 static void framehash_print_extradata(struct AVFormatContext *s)
240 {
241  int i;
242 
243  for (i = 0; i < s->nb_streams; i++) {
244  AVStream *st = s->streams[i];
245  AVCodecParameters *par = st->codecpar;
246  if (par->extradata) {
247  struct HashContext *c = s->priv_data;
248  char buf[AV_HASH_MAX_SIZE*2+1];
249 
250  avio_printf(s->pb, "#extradata %d, %31d, ", i, par->extradata_size);
251  av_hash_init(c->hashes[0]);
252  av_hash_update(c->hashes[0], par->extradata, par->extradata_size);
253  av_hash_final_hex(c->hashes[0], buf, sizeof(buf));
254  avio_write(s->pb, buf, strlen(buf));
255  avio_printf(s->pb, "\n");
256  }
257  }
258 }
259 
260 static int framehash_init(struct AVFormatContext *s)
261 {
262  int res;
263  struct HashContext *c = s->priv_data;
264  c->per_stream = 0;
265  c->hashes = av_mallocz_array(1, sizeof(*c->hashes));
266  if (!c->hashes)
267  return AVERROR(ENOMEM);
268  res = av_hash_alloc(&c->hashes[0], c->hash_name);
269  if (res < 0)
270  return res;
271  return 0;
272 }
273 
274 static int framehash_write_header(struct AVFormatContext *s)
275 {
276  struct HashContext *c = s->priv_data;
277  avio_printf(s->pb, "#format: frame checksums\n");
278  avio_printf(s->pb, "#version: %d\n", c->format_version);
279  avio_printf(s->pb, "#hash: %s\n", av_hash_get_name(c->hashes[0]));
280  framehash_print_extradata(s);
282  avio_printf(s->pb, "#stream#, dts, pts, duration, size, hash\n");
283  return 0;
284 }
285 
286 static int framehash_write_packet(struct AVFormatContext *s, AVPacket *pkt)
287 {
288  struct HashContext *c = s->priv_data;
289  char buf[AV_HASH_MAX_SIZE*2+128];
290  int len;
291  av_hash_init(c->hashes[0]);
292  av_hash_update(c->hashes[0], pkt->data, pkt->size);
293 
294  snprintf(buf, sizeof(buf) - (AV_HASH_MAX_SIZE * 2 + 1), "%d, %10"PRId64", %10"PRId64", %8"PRId64", %8d, ",
296  len = strlen(buf);
297  av_hash_final_hex(c->hashes[0], buf + len, sizeof(buf) - len);
298  avio_write(s->pb, buf, strlen(buf));
299 
300  if (c->format_version > 1 && pkt->side_data_elems) {
301  int i;
302  avio_printf(s->pb, ", S=%d", pkt->side_data_elems);
303  for (i = 0; i < pkt->side_data_elems; i++) {
304  av_hash_init(c->hashes[0]);
305  if (HAVE_BIGENDIAN && pkt->side_data[i].type == AV_PKT_DATA_PALETTE) {
306  for (size_t j = 0; j < pkt->side_data[i].size; j += sizeof(uint32_t)) {
307  uint32_t data = AV_RL32(pkt->side_data[i].data + j);
308  av_hash_update(c->hashes[0], (uint8_t *)&data, sizeof(uint32_t));
309  }
310  } else
311  av_hash_update(c->hashes[0], pkt->side_data[i].data, pkt->side_data[i].size);
312  snprintf(buf, sizeof(buf) - (AV_HASH_MAX_SIZE * 2 + 1),
313  ", %8"SIZE_SPECIFIER", ", pkt->side_data[i].size);
314  len = strlen(buf);
315  av_hash_final_hex(c->hashes[0], buf + len, sizeof(buf) - len);
316  avio_write(s->pb, buf, strlen(buf));
317  }
318  }
319 
320  avio_printf(s->pb, "\n");
321  return 0;
322 }
323 #endif
324 
325 #if CONFIG_FRAMEHASH_MUXER
326 static const AVClass framehash_class = {
327  .class_name = "frame hash muxer",
328  .item_name = av_default_item_name,
329  .option = framehash_options,
330  .version = LIBAVUTIL_VERSION_INT,
331 };
332 
334  .name = "framehash",
335  .long_name = NULL_IF_CONFIG_SMALL("Per-frame hash testing"),
336  .priv_data_size = sizeof(struct HashContext),
337  .audio_codec = AV_CODEC_ID_PCM_S16LE,
338  .video_codec = AV_CODEC_ID_RAWVIDEO,
339  .init = framehash_init,
340  .write_header = framehash_write_header,
341  .write_packet = framehash_write_packet,
342  .deinit = hash_free,
345  .priv_class = &framehash_class,
346 };
347 #endif
348 
349 #if CONFIG_FRAMEMD5_MUXER
350 static const AVClass framemd5_class = {
351  .class_name = "frame MD5 muxer",
352  .item_name = av_default_item_name,
353  .option = framemd5_options,
354  .version = LIBAVUTIL_VERSION_INT,
355 };
356 
358  .name = "framemd5",
359  .long_name = NULL_IF_CONFIG_SMALL("Per-frame MD5 testing"),
360  .priv_data_size = sizeof(struct HashContext),
361  .audio_codec = AV_CODEC_ID_PCM_S16LE,
362  .video_codec = AV_CODEC_ID_RAWVIDEO,
363  .init = framehash_init,
364  .write_header = framehash_write_header,
365  .write_packet = framehash_write_packet,
366  .deinit = hash_free,
369  .priv_class = &framemd5_class,
370 };
371 #endif
AV_CODEC_ID_PCM_S16LE
@ AV_CODEC_ID_PCM_S16LE
Definition: codec_id.h:313
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:496
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:158
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:470
get_media_type_char
static char get_media_type_char(enum AVMediaType type)
Definition: cmdutils.c:1471
AV_CODEC_ID_RAWVIDEO
@ AV_CODEC_ID_RAWVIDEO
Definition: codec_id.h:63
AVPacket::data
uint8_t * data
Definition: packet.h:365
AVOption
AVOption.
Definition: opt.h:248
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:142
av_mallocz_array
void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.c:197
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:383
av_strlcatf
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
Definition: avstring.c:101
AVPacketSideData::size
size_t size
Definition: packet.h:308
av_hash_get_name
const char * av_hash_get_name(const AVHashContext *ctx)
Definition: hash.c:92
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
pkt
AVPacket * pkt
Definition: movenc.c:59
ff_streamhash_muxer
const AVOutputFormat ff_streamhash_muxer
hash_free
static void hash_free(struct AVFormatContext *s)
Definition: hashenc.c:154
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:102
AVPacketSideData::data
uint8_t * data
Definition: packet.h:307
AVMEDIA_TYPE_DATA
@ AVMEDIA_TYPE_DATA
Opaque data information usually continuous.
Definition: avutil.h:203
AVFormatContext
Format I/O context.
Definition: avformat.h:1097
internal.h
HashContext
Definition: hashenc.c:30
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:984
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:139
AVPacketSideData::type
enum AVPacketSideDataType type
Definition: packet.h:309
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
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:160
ff_framemd5_muxer
const AVOutputFormat ff_framemd5_muxer
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:236
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:366
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:117
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:364
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:215
AVHashContext
Definition: hash.c:58
ff_md5_muxer
const AVOutputFormat ff_md5_muxer
HashContext::hashes
struct AVHashContext ** hashes
Definition: hashenc.c:32
i
int i
Definition: input.c:407
AVOutputFormat
Definition: avformat.h:495
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:358
AVMEDIA_TYPE_ATTACHMENT
@ AVMEDIA_TYPE_ATTACHMENT
Opaque data information usually sparse.
Definition: avutil.h:205
ff_framehash_muxer
const AVOutputFormat ff_framehash_muxer
len
int len
Definition: vorbis_enc_data.h:426
AVFMT_TS_NEGATIVE
#define AVFMT_TS_NEGATIVE
Format allows muxing negative timestamps.
Definition: avformat.h:480
AVFMT_TS_NONSTRICT
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition: avformat.h:477
AVStream
Stream structure.
Definition: avformat.h:837
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
AVPacket::side_data
AVPacketSideData * side_data
Additional packet data that can be provided by the container.
Definition: packet.h:376
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
SIZE_SPECIFIER
#define SIZE_SPECIFIER
Definition: internal.h:193
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
HashContext::per_stream
int per_stream
Definition: hashenc.c:34
AVPacket::stream_index
int stream_index
Definition: packet.h:367
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:213
HASH_OPT
#define HASH_OPT(defaulttype)
Definition: hashenc.c:40
AVPacket
This structure stores compressed data.
Definition: packet.h:342
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
snprintf
#define snprintf
Definition: snprintf.h:34
ff_hash_muxer
const AVOutputFormat ff_hash_muxer
AVPacket::side_data_elems
int side_data_elems
Definition: packet.h:377