]> git.sesse.net Git - ffmpeg/blob - libavformat/hashenc.c
avformat/hashenc: use an array of hashes
[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/intreadwrite.h"
26 #include "libavutil/opt.h"
27 #include "avformat.h"
28 #include "internal.h"
29
30 struct HashContext {
31     const AVClass *avclass;
32     struct AVHashContext **hashes;
33     char *hash_name;
34     int format_version;
35 };
36
37 #define OFFSET(x) offsetof(struct HashContext, x)
38 #define ENC AV_OPT_FLAG_ENCODING_PARAM
39 #define HASH_OPT(defaulttype) \
40     { "hash", "set hash to use", OFFSET(hash_name), AV_OPT_TYPE_STRING, {.str = defaulttype}, 0, 0, ENC }
41 #define FORMAT_VERSION_OPT \
42     { "format_version", "file format version", OFFSET(format_version), AV_OPT_TYPE_INT, {.i64 = 2}, 1, 2, ENC }
43
44 #if CONFIG_HASH_MUXER
45 static const AVOption hash_options[] = {
46     HASH_OPT("sha256"),
47     { NULL },
48 };
49 #endif
50
51 #if CONFIG_FRAMEHASH_MUXER
52 static const AVOption framehash_options[] = {
53     HASH_OPT("sha256"),
54     FORMAT_VERSION_OPT,
55     { NULL },
56 };
57 #endif
58
59 #if CONFIG_MD5_MUXER
60 static const AVOption md5_options[] = {
61     HASH_OPT("md5"),
62     { NULL },
63 };
64 #endif
65
66 #if CONFIG_FRAMEMD5_MUXER
67 static const AVOption framemd5_options[] = {
68     HASH_OPT("md5"),
69     FORMAT_VERSION_OPT,
70     { NULL },
71 };
72 #endif
73
74 #if CONFIG_HASH_MUXER || CONFIG_MD5_MUXER
75 static int hash_init(struct AVFormatContext *s)
76 {
77     int res;
78     struct HashContext *c = s->priv_data;
79     c->hashes = av_mallocz_array(1, sizeof(c->hashes));
80     if (!c->hashes)
81         return AVERROR(ENOMEM);
82     res = av_hash_alloc(&c->hashes[0], c->hash_name);
83     if (res < 0)
84         return res;
85     av_hash_init(c->hashes[0]);
86     return 0;
87 }
88
89 static int hash_write_packet(struct AVFormatContext *s, AVPacket *pkt)
90 {
91     struct HashContext *c = s->priv_data;
92     av_hash_update(c->hashes[0], pkt->data, pkt->size);
93     return 0;
94 }
95
96 static int hash_write_trailer(struct AVFormatContext *s)
97 {
98     struct HashContext *c = s->priv_data;
99     char buf[AV_HASH_MAX_SIZE*2+128];
100     snprintf(buf, sizeof(buf) - 200, "%s=", av_hash_get_name(c->hashes[0]));
101
102     av_hash_final_hex(c->hashes[0], buf + strlen(buf), sizeof(buf) - strlen(buf));
103     av_strlcatf(buf, sizeof(buf), "\n");
104     avio_write(s->pb, buf, strlen(buf));
105     avio_flush(s->pb);
106
107     return 0;
108 }
109
110 static void hash_free(struct AVFormatContext *s)
111 {
112     struct HashContext *c = s->priv_data;
113     if (c->hashes)
114         av_hash_freep(&c->hashes[0]);
115     av_freep(&c->hashes);
116 }
117 #endif
118
119 #if CONFIG_HASH_MUXER
120 static const AVClass hashenc_class = {
121     .class_name = "hash muxer",
122     .item_name  = av_default_item_name,
123     .option     = hash_options,
124     .version    = LIBAVUTIL_VERSION_INT,
125 };
126
127 AVOutputFormat ff_hash_muxer = {
128     .name              = "hash",
129     .long_name         = NULL_IF_CONFIG_SMALL("Hash testing"),
130     .priv_data_size    = sizeof(struct HashContext),
131     .audio_codec       = AV_CODEC_ID_PCM_S16LE,
132     .video_codec       = AV_CODEC_ID_RAWVIDEO,
133     .init              = hash_init,
134     .write_packet      = hash_write_packet,
135     .write_trailer     = hash_write_trailer,
136     .deinit            = hash_free,
137     .flags             = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
138                          AVFMT_TS_NEGATIVE,
139     .priv_class        = &hashenc_class,
140 };
141 #endif
142
143 #if CONFIG_MD5_MUXER
144 static const AVClass md5enc_class = {
145     .class_name = "MD5 muxer",
146     .item_name  = av_default_item_name,
147     .option     = md5_options,
148     .version    = LIBAVUTIL_VERSION_INT,
149 };
150
151 AVOutputFormat ff_md5_muxer = {
152     .name              = "md5",
153     .long_name         = NULL_IF_CONFIG_SMALL("MD5 testing"),
154     .priv_data_size    = sizeof(struct HashContext),
155     .audio_codec       = AV_CODEC_ID_PCM_S16LE,
156     .video_codec       = AV_CODEC_ID_RAWVIDEO,
157     .init              = hash_init,
158     .write_packet      = hash_write_packet,
159     .write_trailer     = hash_write_trailer,
160     .deinit            = hash_free,
161     .flags             = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
162                          AVFMT_TS_NEGATIVE,
163     .priv_class        = &md5enc_class,
164 };
165 #endif
166
167 #if CONFIG_FRAMEHASH_MUXER || CONFIG_FRAMEMD5_MUXER
168 static void framehash_print_extradata(struct AVFormatContext *s)
169 {
170     int i;
171
172     for (i = 0; i < s->nb_streams; i++) {
173         AVStream *st = s->streams[i];
174         AVCodecParameters *par = st->codecpar;
175         if (par->extradata) {
176             struct HashContext *c = s->priv_data;
177             char buf[AV_HASH_MAX_SIZE*2+1];
178
179             avio_printf(s->pb, "#extradata %d, %31d, ", i, par->extradata_size);
180             av_hash_init(c->hashes[0]);
181             av_hash_update(c->hashes[0], par->extradata, par->extradata_size);
182             av_hash_final_hex(c->hashes[0], buf, sizeof(buf));
183             avio_write(s->pb, buf, strlen(buf));
184             avio_printf(s->pb, "\n");
185         }
186     }
187 }
188
189 static int framehash_init(struct AVFormatContext *s)
190 {
191     int res;
192     struct HashContext *c = s->priv_data;
193     c->hashes = av_mallocz_array(1, sizeof(c->hashes));
194     if (!c->hashes)
195         return AVERROR(ENOMEM);
196     res = av_hash_alloc(&c->hashes[0], c->hash_name);
197     if (res < 0)
198         return res;
199     return 0;
200 }
201
202 static int framehash_write_header(struct AVFormatContext *s)
203 {
204     struct HashContext *c = s->priv_data;
205     avio_printf(s->pb, "#format: frame checksums\n");
206     avio_printf(s->pb, "#version: %d\n", c->format_version);
207     avio_printf(s->pb, "#hash: %s\n", av_hash_get_name(c->hashes[0]));
208     framehash_print_extradata(s);
209     ff_framehash_write_header(s);
210     avio_printf(s->pb, "#stream#, dts,        pts, duration,     size, hash\n");
211     return 0;
212 }
213
214 static int framehash_write_packet(struct AVFormatContext *s, AVPacket *pkt)
215 {
216     struct HashContext *c = s->priv_data;
217     char buf[AV_HASH_MAX_SIZE*2+128];
218     int len;
219     av_hash_init(c->hashes[0]);
220     av_hash_update(c->hashes[0], pkt->data, pkt->size);
221
222     snprintf(buf, sizeof(buf) - (AV_HASH_MAX_SIZE * 2 + 1), "%d, %10"PRId64", %10"PRId64", %8"PRId64", %8d, ",
223              pkt->stream_index, pkt->dts, pkt->pts, pkt->duration, pkt->size);
224     len = strlen(buf);
225     av_hash_final_hex(c->hashes[0], buf + len, sizeof(buf) - len);
226     avio_write(s->pb, buf, strlen(buf));
227
228     if (c->format_version > 1 && pkt->side_data_elems) {
229         int i, j;
230         avio_printf(s->pb, ", S=%d", pkt->side_data_elems);
231         for (i = 0; i < pkt->side_data_elems; i++) {
232             av_hash_init(c->hashes[0]);
233             if (HAVE_BIGENDIAN && pkt->side_data[i].type == AV_PKT_DATA_PALETTE) {
234                 for (j = 0; j < pkt->side_data[i].size; j += sizeof(uint32_t)) {
235                     uint32_t data = AV_RL32(pkt->side_data[i].data + j);
236                     av_hash_update(c->hashes[0], (uint8_t *)&data, sizeof(uint32_t));
237                 }
238             } else
239                 av_hash_update(c->hashes[0], pkt->side_data[i].data, pkt->side_data[i].size);
240             snprintf(buf, sizeof(buf) - (AV_HASH_MAX_SIZE * 2 + 1), ", %8d, ", pkt->side_data[i].size);
241             len = strlen(buf);
242             av_hash_final_hex(c->hashes[0], buf + len, sizeof(buf) - len);
243             avio_write(s->pb, buf, strlen(buf));
244         }
245     }
246
247     avio_printf(s->pb, "\n");
248     avio_flush(s->pb);
249     return 0;
250 }
251
252 static void framehash_free(struct AVFormatContext *s)
253 {
254     struct HashContext *c = s->priv_data;
255     if (c->hashes)
256         av_hash_freep(&c->hashes[0]);
257     av_freep(&c->hashes);
258 }
259 #endif
260
261 #if CONFIG_FRAMEHASH_MUXER
262 static const AVClass framehash_class = {
263     .class_name = "frame hash muxer",
264     .item_name  = av_default_item_name,
265     .option     = framehash_options,
266     .version    = LIBAVUTIL_VERSION_INT,
267 };
268
269 AVOutputFormat ff_framehash_muxer = {
270     .name              = "framehash",
271     .long_name         = NULL_IF_CONFIG_SMALL("Per-frame hash testing"),
272     .priv_data_size    = sizeof(struct HashContext),
273     .audio_codec       = AV_CODEC_ID_PCM_S16LE,
274     .video_codec       = AV_CODEC_ID_RAWVIDEO,
275     .init              = framehash_init,
276     .write_header      = framehash_write_header,
277     .write_packet      = framehash_write_packet,
278     .deinit            = framehash_free,
279     .flags             = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
280                          AVFMT_TS_NEGATIVE,
281     .priv_class        = &framehash_class,
282 };
283 #endif
284
285 #if CONFIG_FRAMEMD5_MUXER
286 static const AVClass framemd5_class = {
287     .class_name = "frame MD5 muxer",
288     .item_name  = av_default_item_name,
289     .option     = framemd5_options,
290     .version    = LIBAVUTIL_VERSION_INT,
291 };
292
293 AVOutputFormat ff_framemd5_muxer = {
294     .name              = "framemd5",
295     .long_name         = NULL_IF_CONFIG_SMALL("Per-frame MD5 testing"),
296     .priv_data_size    = sizeof(struct HashContext),
297     .audio_codec       = AV_CODEC_ID_PCM_S16LE,
298     .video_codec       = AV_CODEC_ID_RAWVIDEO,
299     .init              = framehash_init,
300     .write_header      = framehash_write_header,
301     .write_packet      = framehash_write_packet,
302     .deinit            = framehash_free,
303     .flags             = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
304                          AVFMT_TS_NEGATIVE,
305     .priv_class        = &framemd5_class,
306 };
307 #endif