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