OSDN Git Service

Merge remote-tracking branch 'qatar/master'
[coroid/ffmpeg_saccubus.git] / 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
25 #define PRIVSIZE 512
26
27 static void md5_finish(struct AVFormatContext *s, char *buf)
28 {
29     uint8_t md5[16];
30     int i, offset = strlen(buf);
31     av_md5_final(s->priv_data, md5);
32     for (i = 0; i < sizeof(md5); i++) {
33         snprintf(buf + offset, 3, "%02"PRIx8, md5[i]);
34         offset += 2;
35     }
36     buf[offset] = '\n';
37     buf[offset+1] = 0;
38
39     avio_write(s->pb, buf, strlen(buf));
40     avio_flush(s->pb);
41 }
42
43 #if CONFIG_MD5_MUXER
44 static int write_header(struct AVFormatContext *s)
45 {
46     if (PRIVSIZE < av_md5_size) {
47         av_log(s, AV_LOG_ERROR, "Insuffient size for md5 context\n");
48         return -1;
49     }
50     av_md5_init(s->priv_data);
51     return 0;
52 }
53
54 static int write_packet(struct AVFormatContext *s, AVPacket *pkt)
55 {
56     av_md5_update(s->priv_data, pkt->data, pkt->size);
57     return 0;
58 }
59
60 static int write_trailer(struct AVFormatContext *s)
61 {
62     char buf[64] = "MD5=";
63
64     md5_finish(s, buf);
65     return 0;
66 }
67
68 AVOutputFormat ff_md5_muxer = {
69     .name              = "md5",
70     .long_name         = NULL_IF_CONFIG_SMALL("MD5 testing format"),
71     .extensions        = "",
72     .priv_data_size    = PRIVSIZE,
73     .audio_codec       = CODEC_ID_PCM_S16LE,
74     .video_codec       = CODEC_ID_RAWVIDEO,
75     .write_header      = write_header,
76     .write_packet      = write_packet,
77     .write_trailer     = write_trailer,
78 };
79 #endif
80
81 #if CONFIG_FRAMEMD5_MUXER
82 static int framemd5_write_packet(struct AVFormatContext *s, AVPacket *pkt)
83 {
84     char buf[256];
85     if (PRIVSIZE < av_md5_size) {
86         av_log(s, AV_LOG_ERROR, "Insuffient size for md5 context\n");
87         return -1;
88     }
89     av_md5_init(s->priv_data);
90     av_md5_update(s->priv_data, pkt->data, pkt->size);
91
92     snprintf(buf, sizeof(buf) - 64, "%d, %"PRId64", %d, ", pkt->stream_index, pkt->dts, pkt->size);
93     md5_finish(s, buf);
94     return 0;
95 }
96
97 AVOutputFormat ff_framemd5_muxer = {
98     .name              = "framemd5",
99     .long_name         = NULL_IF_CONFIG_SMALL("Per-frame MD5 testing format"),
100     .extensions        = "",
101     .priv_data_size    = PRIVSIZE,
102     .audio_codec       = CODEC_ID_PCM_S16LE,
103     .video_codec       = CODEC_ID_RAWVIDEO,
104     .write_packet      = framemd5_write_packet,
105 };
106 #endif