]> git.sesse.net Git - ffmpeg/blob - libavformat/md5enc.c
ac3enc_template: Use the correct context field
[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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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     .extensions        = "",
80     .priv_data_size    = sizeof(struct MD5Context),
81     .audio_codec       = AV_CODEC_ID_PCM_S16LE,
82     .video_codec       = AV_CODEC_ID_RAWVIDEO,
83     .write_header      = write_header,
84     .write_packet      = write_packet,
85     .write_trailer     = write_trailer,
86     .flags             = AVFMT_NOTIMESTAMPS,
87 };
88 #endif
89
90 #if CONFIG_FRAMEMD5_MUXER
91 static int framemd5_write_header(struct AVFormatContext *s)
92 {
93     struct MD5Context *c = s->priv_data;
94     c->md5 = av_md5_alloc();
95     if (!c->md5)
96         return AVERROR(ENOMEM);
97     return ff_framehash_write_header(s);
98 }
99
100 static int framemd5_write_packet(struct AVFormatContext *s, AVPacket *pkt)
101 {
102     struct MD5Context *c = s->priv_data;
103     char buf[256];
104     av_md5_init(c->md5);
105     av_md5_update(c->md5, pkt->data, pkt->size);
106
107     snprintf(buf, sizeof(buf) - 64, "%d, %10"PRId64", %10"PRId64", %8d, %8d, ",
108              pkt->stream_index, pkt->dts, pkt->pts, pkt->duration, pkt->size);
109     md5_finish(s, buf);
110     return 0;
111 }
112
113 static int framemd5_write_trailer(struct AVFormatContext *s)
114 {
115     struct MD5Context *c = s->priv_data;
116     av_freep(&c->md5);
117     return 0;
118 }
119
120 AVOutputFormat ff_framemd5_muxer = {
121     .name              = "framemd5",
122     .long_name         = NULL_IF_CONFIG_SMALL("Per-frame MD5 testing"),
123     .extensions        = "",
124     .priv_data_size    = sizeof(struct MD5Context),
125     .audio_codec       = AV_CODEC_ID_PCM_S16LE,
126     .video_codec       = AV_CODEC_ID_RAWVIDEO,
127     .write_header      = framemd5_write_header,
128     .write_packet      = framemd5_write_packet,
129     .write_trailer     = framemd5_write_trailer,
130     .flags             = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
131                          AVFMT_TS_NEGATIVE,
132 };
133 #endif