FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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;
35 };
36 
37 #define OFFSET(x) offsetof(struct HashContext, x)
38 #define ENC AV_OPT_FLAG_ENCODING_PARAM
39 #if CONFIG_HASH_MUXER || CONFIG_FRAMEHASH_MUXER
40 static const AVOption hash_options[] = {
41  { "hash", "set hash to use", OFFSET(hash_name), AV_OPT_TYPE_STRING, {.str = "sha256"}, 0, 0, ENC },
42  { "format_version", "file format version", OFFSET(format_version), AV_OPT_TYPE_INT, {.i64 = 2}, 1, 2, ENC },
43  { NULL },
44 };
45 #endif
46 
47 #if CONFIG_MD5_MUXER || CONFIG_FRAMEMD5_MUXER
48 static const AVOption md5_options[] = {
49  { "hash", "set hash to use", OFFSET(hash_name), AV_OPT_TYPE_STRING, {.str = "md5"}, 0, 0, ENC },
50  { "format_version", "file format version", OFFSET(format_version), AV_OPT_TYPE_INT, {.i64 = 2}, 1, 2, ENC },
51  { NULL },
52 };
53 #endif
54 
55 #if CONFIG_HASH_MUXER || CONFIG_MD5_MUXER
56 static int hash_write_header(struct AVFormatContext *s)
57 {
58  struct HashContext *c = s->priv_data;
59  int res = av_hash_alloc(&c->hash, c->hash_name);
60  if (res < 0)
61  return res;
62  av_hash_init(c->hash);
63  return 0;
64 }
65 
66 static int hash_write_packet(struct AVFormatContext *s, AVPacket *pkt)
67 {
68  struct HashContext *c = s->priv_data;
69  av_hash_update(c->hash, pkt->data, pkt->size);
70  return 0;
71 }
72 
73 static int hash_write_trailer(struct AVFormatContext *s)
74 {
75  struct HashContext *c = s->priv_data;
76  char buf[AV_HASH_MAX_SIZE*2+128];
77  snprintf(buf, sizeof(buf) - 200, "%s=", av_hash_get_name(c->hash));
78 
79  av_hash_final_hex(c->hash, buf + strlen(buf), sizeof(buf) - strlen(buf));
80  av_strlcatf(buf, sizeof(buf), "\n");
81  avio_write(s->pb, buf, strlen(buf));
82  avio_flush(s->pb);
83 
84  av_hash_freep(&c->hash);
85  return 0;
86 }
87 #endif
88 
89 #if CONFIG_HASH_MUXER
90 static const AVClass hashenc_class = {
91  .class_name = "hash encoder class",
92  .item_name = av_default_item_name,
93  .option = hash_options,
94  .version = LIBAVUTIL_VERSION_INT,
95 };
96 
97 AVOutputFormat ff_hash_muxer = {
98  .name = "hash",
99  .long_name = NULL_IF_CONFIG_SMALL("Hash testing"),
100  .priv_data_size = sizeof(struct HashContext),
101  .audio_codec = AV_CODEC_ID_PCM_S16LE,
102  .video_codec = AV_CODEC_ID_RAWVIDEO,
103  .write_header = hash_write_header,
104  .write_packet = hash_write_packet,
105  .write_trailer = hash_write_trailer,
108  .priv_class = &hashenc_class,
109 };
110 #endif
111 
112 #if CONFIG_MD5_MUXER
113 static const AVClass md5enc_class = {
114  .class_name = "MD5 encoder class",
115  .item_name = av_default_item_name,
116  .option = md5_options,
117  .version = LIBAVUTIL_VERSION_INT,
118 };
119 
120 AVOutputFormat ff_md5_muxer = {
121  .name = "md5",
122  .long_name = NULL_IF_CONFIG_SMALL("MD5 testing"),
123  .priv_data_size = sizeof(struct HashContext),
124  .audio_codec = AV_CODEC_ID_PCM_S16LE,
125  .video_codec = AV_CODEC_ID_RAWVIDEO,
126  .write_header = hash_write_header,
127  .write_packet = hash_write_packet,
128  .write_trailer = hash_write_trailer,
131  .priv_class = &md5enc_class,
132 };
133 #endif
134 
135 #if CONFIG_FRAMEHASH_MUXER || CONFIG_FRAMEMD5_MUXER
136 static void framehash_print_extradata(struct AVFormatContext *s)
137 {
138  int i;
139 
140  for (i = 0; i < s->nb_streams; i++) {
141  AVStream *st = s->streams[i];
142  AVCodecParameters *par = st->codecpar;
143  if (par->extradata) {
144  struct HashContext *c = s->priv_data;
145  char buf[AV_HASH_MAX_SIZE*2+1];
146 
147  avio_printf(s->pb, "#extradata %d, %31d, ", i, par->extradata_size);
148  av_hash_init(c->hash);
150  av_hash_final_hex(c->hash, buf, sizeof(buf));
151  avio_write(s->pb, buf, strlen(buf));
152  avio_printf(s->pb, "\n");
153  }
154  }
155 }
156 
157 static int framehash_write_header(struct AVFormatContext *s)
158 {
159  struct HashContext *c = s->priv_data;
160  int res = av_hash_alloc(&c->hash, c->hash_name);
161  if (res < 0)
162  return res;
163  avio_printf(s->pb, "#format: frame checksums\n");
164  avio_printf(s->pb, "#version: %d\n", c->format_version);
165  avio_printf(s->pb, "#hash: %s\n", av_hash_get_name(c->hash));
166  framehash_print_extradata(s);
168  avio_printf(s->pb, "#stream#, dts, pts, duration, size, hash\n");
169  return 0;
170 }
171 
172 static int framehash_write_packet(struct AVFormatContext *s, AVPacket *pkt)
173 {
174  struct HashContext *c = s->priv_data;
175  char buf[AV_HASH_MAX_SIZE*2+128];
176  int len;
177  av_hash_init(c->hash);
178  av_hash_update(c->hash, pkt->data, pkt->size);
179 
180  snprintf(buf, sizeof(buf) - (AV_HASH_MAX_SIZE * 2 + 1), "%d, %10"PRId64", %10"PRId64", %8"PRId64", %8d, ",
181  pkt->stream_index, pkt->dts, pkt->pts, pkt->duration, pkt->size);
182  len = strlen(buf);
183  av_hash_final_hex(c->hash, buf + len, sizeof(buf) - len);
184  avio_write(s->pb, buf, strlen(buf));
185 
186  if (c->format_version > 1 && pkt->side_data_elems) {
187  int i, j;
188  avio_printf(s->pb, ", S=%d", pkt->side_data_elems);
189  for (i = 0; i < pkt->side_data_elems; i++) {
190  av_hash_init(c->hash);
191  if (HAVE_BIGENDIAN && pkt->side_data[i].type == AV_PKT_DATA_PALETTE) {
192  for (j = 0; j < pkt->side_data[i].size; j += sizeof(uint32_t)) {
193  uint32_t data = AV_RL32(pkt->side_data[i].data + j);
194  av_hash_update(c->hash, (uint8_t *)&data, sizeof(uint32_t));
195  }
196  } else
197  av_hash_update(c->hash, pkt->side_data[i].data, pkt->side_data[i].size);
198  snprintf(buf, sizeof(buf) - (AV_HASH_MAX_SIZE * 2 + 1), ", %8d, ", pkt->side_data[i].size);
199  len = strlen(buf);
200  av_hash_final_hex(c->hash, buf + len, sizeof(buf) - len);
201  avio_write(s->pb, buf, strlen(buf));
202  }
203  }
204 
205  avio_printf(s->pb, "\n");
206  avio_flush(s->pb);
207  return 0;
208 }
209 
210 static int framehash_write_trailer(struct AVFormatContext *s)
211 {
212  struct HashContext *c = s->priv_data;
213  av_hash_freep(&c->hash);
214  return 0;
215 }
216 #endif
217 
218 #if CONFIG_FRAMEHASH_MUXER
219 static const AVClass framehash_class = {
220  .class_name = "frame hash encoder class",
221  .item_name = av_default_item_name,
222  .option = hash_options,
223  .version = LIBAVUTIL_VERSION_INT,
224 };
225 
226 AVOutputFormat ff_framehash_muxer = {
227  .name = "framehash",
228  .long_name = NULL_IF_CONFIG_SMALL("Per-frame hash testing"),
229  .priv_data_size = sizeof(struct HashContext),
230  .audio_codec = AV_CODEC_ID_PCM_S16LE,
231  .video_codec = AV_CODEC_ID_RAWVIDEO,
232  .write_header = framehash_write_header,
233  .write_packet = framehash_write_packet,
234  .write_trailer = framehash_write_trailer,
237  .priv_class = &framehash_class,
238 };
239 #endif
240 
241 #if CONFIG_FRAMEMD5_MUXER
242 static const AVClass framemd5_class = {
243  .class_name = "frame hash encoder class",
244  .item_name = av_default_item_name,
245  .option = md5_options,
246  .version = LIBAVUTIL_VERSION_INT,
247 };
248 
249 AVOutputFormat ff_framemd5_muxer = {
250  .name = "framemd5",
251  .long_name = NULL_IF_CONFIG_SMALL("Per-frame MD5 testing"),
252  .priv_data_size = sizeof(struct HashContext),
253  .audio_codec = AV_CODEC_ID_PCM_S16LE,
254  .video_codec = AV_CODEC_ID_RAWVIDEO,
255  .write_header = framehash_write_header,
256  .write_packet = framehash_write_packet,
257  .write_trailer = framehash_write_trailer,
260  .priv_class = &framemd5_class,
261 };
262 #endif
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
AVOption.
Definition: opt.h:245
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
const AVClass * avclass
Definition: hashenc.c:31
int size
Definition: avcodec.h:1602
static AVPacket pkt
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition: avformat.h:496
This struct describes the properties of an encoded stream.
Definition: avcodec.h:3972
Format I/O context.
Definition: avformat.h:1338
#define AV_HASH_MAX_SIZE
Maximum value that av_hash_get_size() will currently return.
Definition: hash.h:155
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
uint8_t
Generic hashing API.
AVOptions.
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1619
int format_version
Definition: hashenc.c:34
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1406
uint8_t * data
Definition: avcodec.h:1601
uint8_t * data
Definition: avcodec.h:1545
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:204
void av_hash_init(AVHashContext *ctx)
Initialize or reset a hash context.
Definition: hash.c:137
av_default_item_name
#define OFFSET(x)
Definition: hashenc.c:37
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
simple assert() macros that are a bit more flexible than ISO C assert().
enum AVPacketSideDataType type
Definition: avcodec.h:1547
int side_data_elems
Definition: avcodec.h:1613
int av_hash_alloc(AVHashContext **ctx, const char *name)
Allocate a hash context for the algorithm specified by name.
Definition: hash.c:100
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:3998
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1394
int void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition: aviobuf.c:224
const char * name
Definition: avformat.h:524
const char * av_hash_get_name(const AVHashContext *ctx)
Definition: hash.c:90
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
Stream structure.
Definition: avformat.h:889
AVIOContext * pb
I/O context.
Definition: avformat.h:1380
void av_hash_update(AVHashContext *ctx, const uint8_t *src, int len)
Update a hash context with additional data.
Definition: hash.c:158
void * buf
Definition: avisynth_c.h:690
#define ENC
Definition: hashenc.c:38
Describe the class of an AVClass context structure.
Definition: log.h:67
void av_hash_freep(AVHashContext **ctx)
Free hash context and set hash context pointer to NULL.
Definition: hash.c:234
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:211
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
Definition: avstring.c:101
#define snprintf
Definition: snprintf.h:34
Main libavformat public API header.
AVPacketSideData * side_data
Additional packet data that can be provided by the container.
Definition: avcodec.h:1612
static double c[64]
char * hash_name
Definition: hashenc.c:33
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:489
int len
void * priv_data
Format private data.
Definition: avformat.h:1366
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3994
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1600
struct AVHashContext * hash
Definition: hashenc.c:32
AVCodecParameters * codecpar
Definition: avformat.h:1241
int stream_index
Definition: avcodec.h:1603
#define AVFMT_TS_NEGATIVE
Format allows muxing negative timestamps.
Definition: avformat.h:501
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
This structure stores compressed data.
Definition: avcodec.h:1578
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1594
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2