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