Libav
md5enc.c
Go to the documentation of this file.
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,
132 };
133 #endif
struct AVMD5 * md5
Definition: md5enc.c:27
static int write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: assenc.c:58
int size
Definition: avcodec.h:974
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition: avformat.h:426
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
Format I/O context.
Definition: avformat.h:922
struct AVMD5 * av_md5_alloc(void)
Definition: md5.c:49
uint8_t
uint8_t * data
Definition: avcodec.h:973
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:165
Definition: md5.c:39
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:991
static int write_trailer(AVFormatContext *s)
Definition: assenc.c:64
void av_md5_update(AVMD5 *ctx, const uint8_t *src, const int len)
Definition: md5.c:144
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:150
int void avio_flush(AVIOContext *s)
Definition: aviobuf.c:180
const char * name
Definition: avformat.h:446
int ff_framehash_write_header(AVFormatContext *s)
Set the timebase for each stream from the corresponding codec timebase and print it.
Definition: framehash.c:23
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:416
AVIOContext * pb
I/O context.
Definition: avformat.h:964
void av_md5_init(AVMD5 *ctx)
Definition: md5.c:134
void av_md5_final(AVMD5 *ctx, uint8_t *dst)
Definition: md5.c:160
Main libavformat public API header.
static void md5_finish(struct AVFormatContext *s, char *buf)
Definition: md5enc.c:30
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:419
void * priv_data
Format private data.
Definition: avformat.h:950
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:380
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:972
int stream_index
Definition: avcodec.h:975
#define AVFMT_TS_NEGATIVE
Format allows muxing negative timestamps.
Definition: avformat.h:431
This structure stores compressed data.
Definition: avcodec.h:950
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:966