FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
md5enc.c
Go to the documentation of this file.
1 /*
2  * 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/opt.h"
26 #include "avformat.h"
27 #include "internal.h"
28 
29 struct MD5Context {
30  const AVClass *avclass;
32  char *hash_name;
34 };
35 
36 static void md5_finish(struct AVFormatContext *s, char *buf)
37 {
38  struct MD5Context *c = s->priv_data;
40  int i, offset = strlen(buf);
41  int len = av_hash_get_size(c->hash);
42  av_assert0(len > 0 && len <= sizeof(md5));
43  av_hash_final(c->hash, md5);
44  for (i = 0; i < len; i++) {
45  snprintf(buf + offset, 3, "%02"PRIx8, md5[i]);
46  offset += 2;
47  }
48  buf[offset] = '\n';
49  buf[offset+1] = 0;
50 
51  avio_write(s->pb, buf, strlen(buf));
52  avio_flush(s->pb);
53 }
54 
55 #define OFFSET(x) offsetof(struct MD5Context, x)
56 #define ENC AV_OPT_FLAG_ENCODING_PARAM
57 static const AVOption hash_options[] = {
58  { "hash", "set hash to use", OFFSET(hash_name), AV_OPT_TYPE_STRING, {.str = "md5"}, 0, 0, ENC },
59  { "format_version", "file format version", OFFSET(format_version), AV_OPT_TYPE_INT, {.i64 = 1}, 1, 1, ENC },
60  { NULL },
61 };
62 
63 static const AVClass md5enc_class = {
64  .class_name = "hash encoder class",
65  .item_name = av_default_item_name,
66  .option = hash_options,
67  .version = LIBAVUTIL_VERSION_INT,
68 };
69 
70 #if CONFIG_MD5_MUXER
71 static int write_header(struct AVFormatContext *s)
72 {
73  struct MD5Context *c = s->priv_data;
74  int res = av_hash_alloc(&c->hash, c->hash_name);
75  if (res < 0)
76  return res;
77  av_hash_init(c->hash);
78  return 0;
79 }
80 
81 static int write_packet(struct AVFormatContext *s, AVPacket *pkt)
82 {
83  struct MD5Context *c = s->priv_data;
84  av_hash_update(c->hash, pkt->data, pkt->size);
85  return 0;
86 }
87 
88 static int write_trailer(struct AVFormatContext *s)
89 {
90  struct MD5Context *c = s->priv_data;
91  char buf[256];
92  av_strlcpy(buf, av_hash_get_name(c->hash), sizeof(buf) - 200);
93  av_strlcat(buf, "=", sizeof(buf) - 200);
94 
95  md5_finish(s, buf);
96 
97  av_hash_freep(&c->hash);
98  return 0;
99 }
100 
101 AVOutputFormat ff_md5_muxer = {
102  .name = "md5",
103  .long_name = NULL_IF_CONFIG_SMALL("MD5 testing"),
104  .priv_data_size = sizeof(struct MD5Context),
105  .audio_codec = AV_CODEC_ID_PCM_S16LE,
106  .video_codec = AV_CODEC_ID_RAWVIDEO,
107  .write_header = write_header,
108  .write_packet = write_packet,
109  .write_trailer = write_trailer,
110  .flags = AVFMT_NOTIMESTAMPS,
111  .priv_class = &md5enc_class,
112 };
113 #endif
114 
115 #if CONFIG_FRAMEMD5_MUXER
116 static int framemd5_write_header(struct AVFormatContext *s)
117 {
118  struct MD5Context *c = s->priv_data;
119  int res = av_hash_alloc(&c->hash, c->hash_name);
120  if (res < 0)
121  return res;
122  avio_printf(s->pb, "#format: frame checksums\n");
123  avio_printf(s->pb, "#version: %d\n", c->format_version);
124  avio_printf(s->pb, "#hash: %s\n", av_hash_get_name(c->hash));
126  avio_printf(s->pb, "#stream#, dts, pts, duration, size, hash\n");
127  return 0;
128 }
129 
130 static int framemd5_write_packet(struct AVFormatContext *s, AVPacket *pkt)
131 {
132  struct MD5Context *c = s->priv_data;
133  char buf[256];
134  av_hash_init(c->hash);
135  av_hash_update(c->hash, pkt->data, pkt->size);
136 
137  snprintf(buf, sizeof(buf) - 64, "%d, %10"PRId64", %10"PRId64", %8d, %8d, ",
138  pkt->stream_index, pkt->dts, pkt->pts, pkt->duration, pkt->size);
139  md5_finish(s, buf);
140  return 0;
141 }
142 
143 static int framemd5_write_trailer(struct AVFormatContext *s)
144 {
145  struct MD5Context *c = s->priv_data;
146  av_hash_freep(&c->hash);
147  return 0;
148 }
149 
150 static const AVClass framemd5_class = {
151  .class_name = "frame hash encoder class",
152  .item_name = av_default_item_name,
153  .option = hash_options,
154  .version = LIBAVUTIL_VERSION_INT,
155 };
156 
157 AVOutputFormat ff_framemd5_muxer = {
158  .name = "framemd5",
159  .long_name = NULL_IF_CONFIG_SMALL("Per-frame MD5 testing"),
160  .priv_data_size = sizeof(struct MD5Context),
161  .audio_codec = AV_CODEC_ID_PCM_S16LE,
162  .video_codec = AV_CODEC_ID_RAWVIDEO,
163  .write_header = framemd5_write_header,
164  .write_packet = framemd5_write_packet,
165  .write_trailer = framemd5_write_trailer,
168  .priv_class = &framemd5_class,
169 };
170 #endif
#define NULL
Definition: coverity.c:32
struct AVMD5 * md5
Definition: md5proto.c:31
const char * s
Definition: avisynth_c.h:631
AVOption.
Definition: opt.h:255
static const AVClass md5enc_class
Definition: md5enc.c:63
#define LIBAVUTIL_VERSION_INT
Definition: version.h:62
int size
Definition: avcodec.h:1424
static AVPacket pkt
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition: avformat.h:483
const AVClass * avclass
Definition: md5enc.c:30
Format I/O context.
Definition: avformat.h:1273
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
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
uint8_t
#define AV_HASH_MAX_SIZE
Maximum value that av_hash_get_size will currently return.
Definition: hash.h:61
AVOptions.
void av_hash_init(AVHashContext *ctx)
Initialize or reset a hash context.
Definition: hash.c:137
uint8_t * data
Definition: avcodec.h:1423
char * hash_name
Definition: md5enc.c:32
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:178
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1441
static const AVOption hash_options[]
Definition: md5enc.c:57
av_default_item_name
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
simple assert() macros that are a bit more flexible than ISO C assert().
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition: avstring.c:83
int void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition: aviobuf.c:198
int av_hash_alloc(AVHashContext **ctx, const char *name)
Allocate a hash context for the algorithm specified by name.
Definition: hash.c:100
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:94
const char * name
Definition: avformat.h:513
const char * av_hash_get_name(const AVHashContext *ctx)
Definition: hash.c:90
void av_hash_freep(AVHashContext **ctx)
Free hash context.
Definition: hash.c:234
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
int format_version
Definition: md5enc.c:33
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:472
void av_hash_final(AVHashContext *ctx, uint8_t *dst)
Finalize a hash context and compute the actual hash value.
Definition: hash.c:179
int av_hash_get_size(const AVHashContext *ctx)
Definition: hash.c:95
AVIOContext * pb
I/O context.
Definition: avformat.h:1315
void * buf
Definition: avisynth_c.h:553
void av_hash_update(AVHashContext *ctx, const uint8_t *src, int len)
Update a hash context with additional data.
Definition: hash.c:158
Describe the class of an AVClass context structure.
Definition: log.h:67
#define ENC
Definition: md5enc.c:56
#define snprintf
Definition: snprintf.h:34
size_t av_strlcat(char *dst, const char *src, size_t size)
Append the string src to the string dst, but to a total length of no more than size - 1 bytes...
Definition: avstring.c:93
Main libavformat public API header.
static double c[64]
static void md5_finish(struct AVFormatContext *s, char *buf)
Definition: md5enc.c:36
struct AVHashContext * hash
Definition: md5enc.c:31
#define OFFSET(x)
Definition: md5enc.c:55
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:475
int len
void * priv_data
Format private data.
Definition: avformat.h:1301
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:493
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1422
int stream_index
Definition: avcodec.h:1425
#define AVFMT_TS_NEGATIVE
Format allows muxing negative timestamps.
Definition: avformat.h:490
This structure stores compressed data.
Definition: avcodec.h:1400
static int write_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: v4l2enc.c:86
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1416
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2