]> git.sesse.net Git - ffmpeg/blob - libavformat/hashenc.c
Merge commit 'd40cb726d271b0284642a1ba159eb26a5c579f77'
[ffmpeg] / libavformat / hashenc.c
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/opt.h"
26 #include "avformat.h"
27 #include "internal.h"
28
29 struct HashContext {
30     const AVClass *avclass;
31     struct AVHashContext *hash;
32     char *hash_name;
33     int format_version;
34 };
35
36 static void hash_finish(struct AVFormatContext *s, char *buf)
37 {
38     struct HashContext *c = s->priv_data;
39     uint8_t hash[AV_HASH_MAX_SIZE];
40     int i, offset = strlen(buf);
41     int len = av_hash_get_size(c->hash);
42     av_assert0(len > 0 && len <= sizeof(hash));
43     av_hash_final(c->hash, hash);
44     for (i = 0; i < len; i++) {
45         snprintf(buf + offset, 3, "%02"PRIx8, hash[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 HashContext, x)
56 #define ENC AV_OPT_FLAG_ENCODING_PARAM
57 #if CONFIG_HASH_MUXER || CONFIG_FRAMEHASH_MUXER
58 static const AVOption hash_options[] = {
59     { "hash", "set hash to use", OFFSET(hash_name), AV_OPT_TYPE_STRING, {.str = "sha256"}, 0, 0, ENC },
60     { "format_version", "file format version", OFFSET(format_version), AV_OPT_TYPE_INT, {.i64 = 1}, 1, 2, ENC },
61     { NULL },
62 };
63 #endif
64
65 #if CONFIG_MD5_MUXER || CONFIG_FRAMEMD5_MUXER
66 static const AVOption md5_options[] = {
67     { "hash", "set hash to use", OFFSET(hash_name), AV_OPT_TYPE_STRING, {.str = "md5"}, 0, 0, ENC },
68     { "format_version", "file format version", OFFSET(format_version), AV_OPT_TYPE_INT, {.i64 = 1}, 1, 2, ENC },
69     { NULL },
70 };
71 #endif
72
73 #if CONFIG_HASH_MUXER || CONFIG_MD5_MUXER
74 static int hash_write_header(struct AVFormatContext *s)
75 {
76     struct HashContext *c = s->priv_data;
77     int res = av_hash_alloc(&c->hash, c->hash_name);
78     if (res < 0)
79         return res;
80     av_hash_init(c->hash);
81     return 0;
82 }
83
84 static int hash_write_packet(struct AVFormatContext *s, AVPacket *pkt)
85 {
86     struct HashContext *c = s->priv_data;
87     av_hash_update(c->hash, pkt->data, pkt->size);
88     return 0;
89 }
90
91 static int hash_write_trailer(struct AVFormatContext *s)
92 {
93     struct HashContext *c = s->priv_data;
94     char buf[256];
95     av_strlcpy(buf, av_hash_get_name(c->hash), sizeof(buf) - 200);
96     av_strlcat(buf, "=", sizeof(buf) - 200);
97
98     hash_finish(s, buf);
99
100     av_hash_freep(&c->hash);
101     return 0;
102 }
103 #endif
104
105 #if CONFIG_HASH_MUXER
106 static const AVClass hashenc_class = {
107     .class_name = "hash encoder class",
108     .item_name  = av_default_item_name,
109     .option     = hash_options,
110     .version    = LIBAVUTIL_VERSION_INT,
111 };
112
113 AVOutputFormat ff_hash_muxer = {
114     .name              = "hash",
115     .long_name         = NULL_IF_CONFIG_SMALL("Hash testing"),
116     .priv_data_size    = sizeof(struct HashContext),
117     .audio_codec       = AV_CODEC_ID_PCM_S16LE,
118     .video_codec       = AV_CODEC_ID_RAWVIDEO,
119     .write_header      = hash_write_header,
120     .write_packet      = hash_write_packet,
121     .write_trailer     = hash_write_trailer,
122     .flags             = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
123                          AVFMT_TS_NEGATIVE,
124     .priv_class        = &hashenc_class,
125 };
126 #endif
127
128 #if CONFIG_MD5_MUXER
129 static const AVClass md5enc_class = {
130     .class_name = "MD5 encoder class",
131     .item_name  = av_default_item_name,
132     .option     = md5_options,
133     .version    = LIBAVUTIL_VERSION_INT,
134 };
135
136 AVOutputFormat ff_md5_muxer = {
137     .name              = "md5",
138     .long_name         = NULL_IF_CONFIG_SMALL("MD5 testing"),
139     .priv_data_size    = sizeof(struct HashContext),
140     .audio_codec       = AV_CODEC_ID_PCM_S16LE,
141     .video_codec       = AV_CODEC_ID_RAWVIDEO,
142     .write_header      = hash_write_header,
143     .write_packet      = hash_write_packet,
144     .write_trailer     = hash_write_trailer,
145     .flags             = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
146                          AVFMT_TS_NEGATIVE,
147     .priv_class        = &md5enc_class,
148 };
149 #endif
150
151 #if CONFIG_FRAMEHASH_MUXER || CONFIG_FRAMEMD5_MUXER
152 static int framehash_write_header(struct AVFormatContext *s)
153 {
154     struct HashContext *c = s->priv_data;
155     int res = av_hash_alloc(&c->hash, c->hash_name);
156     if (res < 0)
157         return res;
158     avio_printf(s->pb, "#format: frame checksums\n");
159     avio_printf(s->pb, "#version: %d\n", c->format_version);
160     avio_printf(s->pb, "#hash: %s\n", av_hash_get_name(c->hash));
161     ff_framehash_write_header(s, c->format_version);
162     avio_printf(s->pb, "#stream#, dts,        pts, duration,     size, hash\n");
163     return 0;
164 }
165
166 static int framehash_write_packet(struct AVFormatContext *s, AVPacket *pkt)
167 {
168     struct HashContext *c = s->priv_data;
169     char buf[256];
170     av_hash_init(c->hash);
171     av_hash_update(c->hash, pkt->data, pkt->size);
172
173     snprintf(buf, sizeof(buf) - 64, "%d, %10"PRId64", %10"PRId64", %8"PRId64", %8d, ",
174              pkt->stream_index, pkt->dts, pkt->pts, pkt->duration, pkt->size);
175     hash_finish(s, buf);
176     return 0;
177 }
178
179 static int framehash_write_trailer(struct AVFormatContext *s)
180 {
181     struct HashContext *c = s->priv_data;
182     av_hash_freep(&c->hash);
183     return 0;
184 }
185 #endif
186
187 #if CONFIG_FRAMEHASH_MUXER
188 static const AVClass framehash_class = {
189     .class_name = "frame hash encoder class",
190     .item_name  = av_default_item_name,
191     .option     = hash_options,
192     .version    = LIBAVUTIL_VERSION_INT,
193 };
194
195 AVOutputFormat ff_framehash_muxer = {
196     .name              = "framehash",
197     .long_name         = NULL_IF_CONFIG_SMALL("Per-frame hash testing"),
198     .priv_data_size    = sizeof(struct HashContext),
199     .audio_codec       = AV_CODEC_ID_PCM_S16LE,
200     .video_codec       = AV_CODEC_ID_RAWVIDEO,
201     .write_header      = framehash_write_header,
202     .write_packet      = framehash_write_packet,
203     .write_trailer     = framehash_write_trailer,
204     .flags             = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
205                          AVFMT_TS_NEGATIVE,
206     .priv_class        = &framehash_class,
207 };
208 #endif
209
210 #if CONFIG_FRAMEMD5_MUXER
211 static const AVClass framemd5_class = {
212     .class_name = "frame hash encoder class",
213     .item_name  = av_default_item_name,
214     .option     = md5_options,
215     .version    = LIBAVUTIL_VERSION_INT,
216 };
217
218 AVOutputFormat ff_framemd5_muxer = {
219     .name              = "framemd5",
220     .long_name         = NULL_IF_CONFIG_SMALL("Per-frame MD5 testing"),
221     .priv_data_size    = sizeof(struct HashContext),
222     .audio_codec       = AV_CODEC_ID_PCM_S16LE,
223     .video_codec       = AV_CODEC_ID_RAWVIDEO,
224     .write_header      = framehash_write_header,
225     .write_packet      = framehash_write_packet,
226     .write_trailer     = framehash_write_trailer,
227     .flags             = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
228                          AVFMT_TS_NEGATIVE,
229     .priv_class        = &framemd5_class,
230 };
231 #endif