]> git.sesse.net Git - ffmpeg/blob - libavformat/md5enc.c
Merge commit '57231e4d5b467833fb289439cd35a92513bb55c1'
[ffmpeg] / libavformat / md5enc.c
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/md5.h"
23 #include "avformat.h"
24 #include "internal.h"
25
26 struct MD5Context {
27     struct AVMD5 *md5;
28 };
29
30 static void md5_finish(struct AVFormatContext *s, char *buf)
31 {
32     struct MD5Context *c = s->priv_data;
33     uint8_t md5[16];
34     int i, offset = strlen(buf);
35     av_md5_final(c->md5, md5);
36     for (i = 0; i < sizeof(md5); i++) {
37         snprintf(buf + offset, 3, "%02"PRIx8, md5[i]);
38         offset += 2;
39     }
40     buf[offset] = '\n';
41     buf[offset+1] = 0;
42
43     avio_write(s->pb, buf, strlen(buf));
44     avio_flush(s->pb);
45 }
46
47 #if CONFIG_MD5_MUXER
48 static int write_header(struct AVFormatContext *s)
49 {
50     struct MD5Context *c = s->priv_data;
51     c->md5 = av_md5_alloc();
52     if (!c->md5)
53         return AVERROR(ENOMEM);
54     av_md5_init(c->md5);
55     return 0;
56 }
57
58 static int write_packet(struct AVFormatContext *s, AVPacket *pkt)
59 {
60     struct MD5Context *c = s->priv_data;
61     av_md5_update(c->md5, pkt->data, pkt->size);
62     return 0;
63 }
64
65 static int write_trailer(struct AVFormatContext *s)
66 {
67     struct MD5Context *c = s->priv_data;
68     char buf[64] = "MD5=";
69
70     md5_finish(s, buf);
71
72     av_freep(&c->md5);
73     return 0;
74 }
75
76 AVOutputFormat ff_md5_muxer = {
77     .name              = "md5",
78     .long_name         = NULL_IF_CONFIG_SMALL("MD5 testing"),
79     .priv_data_size    = sizeof(struct MD5Context),
80     .audio_codec       = AV_CODEC_ID_PCM_S16LE,
81     .video_codec       = AV_CODEC_ID_RAWVIDEO,
82     .write_header      = write_header,
83     .write_packet      = write_packet,
84     .write_trailer     = write_trailer,
85     .flags             = AVFMT_NOTIMESTAMPS,
86 };
87 #endif
88
89 #if CONFIG_FRAMEMD5_MUXER
90 static int framemd5_write_header(struct AVFormatContext *s)
91 {
92     struct MD5Context *c = s->priv_data;
93     c->md5 = av_md5_alloc();
94     if (!c->md5)
95         return AVERROR(ENOMEM);
96     return ff_framehash_write_header(s);
97 }
98
99 static int framemd5_write_packet(struct AVFormatContext *s, AVPacket *pkt)
100 {
101     struct MD5Context *c = s->priv_data;
102     char buf[256];
103     av_md5_init(c->md5);
104     av_md5_update(c->md5, pkt->data, pkt->size);
105
106     snprintf(buf, sizeof(buf) - 64, "%d, %10"PRId64", %10"PRId64", %8d, %8d, ",
107              pkt->stream_index, pkt->dts, pkt->pts, pkt->duration, pkt->size);
108     md5_finish(s, buf);
109     return 0;
110 }
111
112 static int framemd5_write_trailer(struct AVFormatContext *s)
113 {
114     struct MD5Context *c = s->priv_data;
115     av_freep(&c->md5);
116     return 0;
117 }
118
119 AVOutputFormat ff_framemd5_muxer = {
120     .name              = "framemd5",
121     .long_name         = NULL_IF_CONFIG_SMALL("Per-frame MD5 testing"),
122     .priv_data_size    = sizeof(struct MD5Context),
123     .audio_codec       = AV_CODEC_ID_PCM_S16LE,
124     .video_codec       = AV_CODEC_ID_RAWVIDEO,
125     .write_header      = framemd5_write_header,
126     .write_packet      = framemd5_write_packet,
127     .write_trailer     = framemd5_write_trailer,
128     .flags             = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT,
129 };
130 #endif