• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 // Frame-by-frame MD5 Checksum
12 // ===========================
13 //
14 // This example builds upon the simple decoder loop to show how checksums
15 // of the decoded output can be generated. These are used for validating
16 // decoder implementations against the reference implementation, for example.
17 //
18 // MD5 algorithm
19 // -------------
20 // The Message-Digest 5 (MD5) is a well known hash function. We have provided
21 // an implementation derived from the RSA Data Security, Inc. MD5 Message-Digest
22 // Algorithm for your use. Our implmentation only changes the interface of this
23 // reference code. You must include the `md5_utils.h` header for access to these
24 // functions.
25 //
26 // Processing The Decoded Data
27 // ---------------------------
28 // Each row of the image is passed to the MD5 accumulator. First the Y plane
29 // is processed, then U, then V. It is important to honor the image's `stride`
30 // values.
31 
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 
36 #define VPX_CODEC_DISABLE_COMPAT 1
37 
38 #include "vpx/vp8dx.h"
39 #include "vpx/vpx_decoder.h"
40 
41 #include "./md5_utils.h"
42 #include "./tools_common.h"
43 #include "./video_reader.h"
44 #include "./vpx_config.h"
45 
get_image_md5(const vpx_image_t * img,unsigned char digest[16])46 static void get_image_md5(const vpx_image_t *img, unsigned char digest[16]) {
47   int plane, y;
48   MD5Context md5;
49 
50   MD5Init(&md5);
51 
52   for (plane = 0; plane < 3; ++plane) {
53     const unsigned char *buf = img->planes[plane];
54     const int stride = img->stride[plane];
55     const int w = plane ? (img->d_w + 1) >> 1 : img->d_w;
56     const int h = plane ? (img->d_h + 1) >> 1 : img->d_h;
57 
58     for (y = 0; y < h; ++y) {
59       MD5Update(&md5, buf, w);
60       buf += stride;
61     }
62   }
63 
64   MD5Final(digest, &md5);
65 }
66 
print_md5(FILE * stream,unsigned char digest[16])67 static void print_md5(FILE *stream, unsigned char digest[16]) {
68   int i;
69 
70   for (i = 0; i < 16; ++i)
71     fprintf(stream, "%02x", digest[i]);
72 }
73 
74 static const char *exec_name;
75 
usage_exit()76 void usage_exit() {
77   fprintf(stderr, "Usage: %s <infile> <outfile>\n", exec_name);
78   exit(EXIT_FAILURE);
79 }
80 
main(int argc,char ** argv)81 int main(int argc, char **argv) {
82   int frame_cnt = 0;
83   FILE *outfile = NULL;
84   vpx_codec_ctx_t codec;
85   VpxVideoReader *reader = NULL;
86   const VpxVideoInfo *info = NULL;
87   const VpxInterface *decoder = NULL;
88 
89   exec_name = argv[0];
90 
91   if (argc != 3)
92     die("Invalid number of arguments.");
93 
94   reader = vpx_video_reader_open(argv[1]);
95   if (!reader)
96     die("Failed to open %s for reading.", argv[1]);
97 
98   if (!(outfile = fopen(argv[2], "wb")))
99     die("Failed to open %s for writing.", argv[2]);
100 
101   info = vpx_video_reader_get_info(reader);
102 
103   decoder = get_vpx_decoder_by_fourcc(info->codec_fourcc);
104   if (!decoder)
105     die("Unknown input codec.");
106 
107   printf("Using %s\n", vpx_codec_iface_name(decoder->interface()));
108 
109   if (vpx_codec_dec_init(&codec, decoder->interface(), NULL, 0))
110     die_codec(&codec, "Failed to initialize decoder");
111 
112   while (vpx_video_reader_read_frame(reader)) {
113     vpx_codec_iter_t iter = NULL;
114     vpx_image_t *img = NULL;
115     size_t frame_size = 0;
116     const unsigned char *frame = vpx_video_reader_get_frame(reader,
117                                                             &frame_size);
118     if (vpx_codec_decode(&codec, frame, (unsigned int)frame_size, NULL, 0))
119       die_codec(&codec, "Failed to decode frame");
120 
121     while ((img = vpx_codec_get_frame(&codec, &iter)) != NULL) {
122       unsigned char digest[16];
123 
124       get_image_md5(img, digest);
125       print_md5(outfile, digest);
126       fprintf(outfile, "  img-%dx%d-%04d.i420\n",
127               img->d_w, img->d_h, ++frame_cnt);
128     }
129   }
130 
131   printf("Processed %d frames.\n", frame_cnt);
132   if (vpx_codec_destroy(&codec))
133     die_codec(&codec, "Failed to destroy codec.");
134 
135   vpx_video_reader_close(reader);
136 
137   fclose(outfile);
138   return EXIT_SUCCESS;
139 }
140