1 /*
2  * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 
12 // Frame-by-frame MD5 Checksum
13 // ===========================
14 //
15 // This example builds upon the simple decoder loop to show how checksums
16 // of the decoded output can be generated. These are used for validating
17 // decoder implementations against the reference implementation, for example.
18 //
19 // MD5 algorithm
20 // -------------
21 // The Message-Digest 5 (MD5) is a well known hash function. We have provided
22 // an implementation derived from the RSA Data Security, Inc. MD5 Message-Digest
23 // Algorithm for your use. Our implmentation only changes the interface of this
24 // reference code. You must include the `md5_utils.h` header for access to these
25 // functions.
26 //
27 // Processing The Decoded Data
28 // ---------------------------
29 // Each row of the image is passed to the MD5 accumulator. First the Y plane
30 // is processed, then U, then V. It is important to honor the image's `stride`
31 // values.
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 
37 #include "aom/aom_decoder.h"
38 #include "aom/aomdx.h"
39 #include "common/md5_utils.h"
40 #include "common/tools_common.h"
41 #include "common/video_reader.h"
42 
get_image_md5(const aom_image_t * img,unsigned char digest[16])43 static void get_image_md5(const aom_image_t *img, unsigned char digest[16]) {
44   int plane, y;
45   MD5Context md5;
46 
47   MD5Init(&md5);
48 
49   for (plane = 0; plane < 3; ++plane) {
50     const unsigned char *buf = img->planes[plane];
51     const int stride = img->stride[plane];
52     const int w = plane ? (img->d_w + 1) >> 1 : img->d_w;
53     const int h = plane ? (img->d_h + 1) >> 1 : img->d_h;
54 
55     for (y = 0; y < h; ++y) {
56       MD5Update(&md5, buf, w);
57       buf += stride;
58     }
59   }
60 
61   MD5Final(digest, &md5);
62 }
63 
print_md5(FILE * stream,unsigned char digest[16])64 static void print_md5(FILE *stream, unsigned char digest[16]) {
65   int i;
66 
67   for (i = 0; i < 16; ++i) fprintf(stream, "%02x", digest[i]);
68 }
69 
70 static const char *exec_name;
71 
usage_exit(void)72 void usage_exit(void) {
73   fprintf(stderr, "Usage: %s <infile> <outfile>\n", exec_name);
74   exit(EXIT_FAILURE);
75 }
76 
main(int argc,char ** argv)77 int main(int argc, char **argv) {
78   int frame_cnt = 0;
79   FILE *outfile = NULL;
80   aom_codec_ctx_t codec;
81   AvxVideoReader *reader = NULL;
82   const AvxVideoInfo *info = NULL;
83   const AvxInterface *decoder = NULL;
84 
85   exec_name = argv[0];
86 
87   if (argc != 3) die("Invalid number of arguments.");
88 
89   reader = aom_video_reader_open(argv[1]);
90   if (!reader) die("Failed to open %s for reading.", argv[1]);
91 
92   if (!(outfile = fopen(argv[2], "wb")))
93     die("Failed to open %s for writing.", argv[2]);
94 
95   info = aom_video_reader_get_info(reader);
96 
97   decoder = get_aom_decoder_by_fourcc(info->codec_fourcc);
98   if (!decoder) die("Unknown input codec.");
99 
100   printf("Using %s\n", aom_codec_iface_name(decoder->codec_interface()));
101 
102   if (aom_codec_dec_init(&codec, decoder->codec_interface(), NULL, 0))
103     die_codec(&codec, "Failed to initialize decoder");
104 
105   while (aom_video_reader_read_frame(reader)) {
106     aom_codec_iter_t iter = NULL;
107     aom_image_t *img = NULL;
108     size_t frame_size = 0;
109     const unsigned char *frame =
110         aom_video_reader_get_frame(reader, &frame_size);
111     if (aom_codec_decode(&codec, frame, frame_size, NULL))
112       die_codec(&codec, "Failed to decode frame");
113 
114     while ((img = aom_codec_get_frame(&codec, &iter)) != NULL) {
115       unsigned char digest[16];
116 
117       get_image_md5(img, digest);
118       print_md5(outfile, digest);
119       fprintf(outfile, "  img-%dx%d-%04d.i420\n", img->d_w, img->d_h,
120               ++frame_cnt);
121     }
122   }
123 
124   printf("Processed %d frames.\n", frame_cnt);
125   if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec.");
126 
127   aom_video_reader_close(reader);
128 
129   fclose(outfile);
130   return EXIT_SUCCESS;
131 }
132