xref: /aosp_15_r20/external/libaom/examples/decode_to_md5.c (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
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   AvxVideoReader *reader = NULL;
81   const AvxVideoInfo *info = NULL;
82 
83   exec_name = argv[0];
84 
85   if (argc != 3) die("Invalid number of arguments.");
86 
87   reader = aom_video_reader_open(argv[1]);
88   if (!reader) die("Failed to open %s for reading.", argv[1]);
89 
90   if (!(outfile = fopen(argv[2], "wb")))
91     die("Failed to open %s for writing.", argv[2]);
92 
93   info = aom_video_reader_get_info(reader);
94 
95   aom_codec_iface_t *decoder = get_aom_decoder_by_fourcc(info->codec_fourcc);
96   if (!decoder) die("Unknown input codec.");
97 
98   printf("Using %s\n", aom_codec_iface_name(decoder));
99 
100   aom_codec_ctx_t codec;
101   if (aom_codec_dec_init(&codec, decoder, NULL, 0))
102     die("Failed to initialize decoder");
103 
104   while (aom_video_reader_read_frame(reader)) {
105     aom_codec_iter_t iter = NULL;
106     aom_image_t *img = NULL;
107     size_t frame_size = 0;
108     const unsigned char *frame =
109         aom_video_reader_get_frame(reader, &frame_size);
110     if (aom_codec_decode(&codec, frame, frame_size, NULL))
111       die_codec(&codec, "Failed to decode frame");
112 
113     while ((img = aom_codec_get_frame(&codec, &iter)) != NULL) {
114       unsigned char digest[16];
115 
116       get_image_md5(img, digest);
117       print_md5(outfile, digest);
118       fprintf(outfile, "  img-%ux%u-%04d.i420\n", img->d_w, img->d_h,
119               ++frame_cnt);
120     }
121   }
122 
123   printf("Processed %d frames.\n", frame_cnt);
124   if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec.");
125 
126   aom_video_reader_close(reader);
127 
128   fclose(outfile);
129   return EXIT_SUCCESS;
130 }
131