decode_to_md5

00001 /*
00002  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
00003  *
00004  *  Use of this source code is governed by a BSD-style license
00005  *  that can be found in the LICENSE file in the root of the source
00006  *  tree. An additional intellectual property rights grant can be found
00007  *  in the file PATENTS.  All contributing project authors may
00008  *  be found in the AUTHORS file in the root of the source tree.
00009  */
00010 
00011 // Frame-by-frame MD5 Checksum
00012 // ===========================
00013 //
00014 // This example builds upon the simple decoder loop to show how checksums
00015 // of the decoded output can be generated. These are used for validating
00016 // decoder implementations against the reference implementation, for example.
00017 //
00018 // MD5 algorithm
00019 // -------------
00020 // The Message-Digest 5 (MD5) is a well known hash function. We have provided
00021 // an implementation derived from the RSA Data Security, Inc. MD5 Message-Digest
00022 // Algorithm for your use. Our implmentation only changes the interface of this
00023 // reference code. You must include the `md5_utils.h` header for access to these
00024 // functions.
00025 //
00026 // Processing The Decoded Data
00027 // ---------------------------
00028 // Each row of the image is passed to the MD5 accumulator. First the Y plane
00029 // is processed, then U, then V. It is important to honor the image's `stride`
00030 // values.
00031 
00032 #include <stdio.h>
00033 #include <stdlib.h>
00034 #include <string.h>
00035 
00036 #include "vpx/vp8dx.h"
00037 #include "vpx/vpx_decoder.h"
00038 
00039 #include "../md5_utils.h"
00040 #include "../tools_common.h"
00041 #include "../video_reader.h"
00042 #include "./vpx_config.h"
00043 
00044 static void get_image_md5(const vpx_image_t *img, unsigned char digest[16]) {
00045   int plane, y;
00046   MD5Context md5;
00047 
00048   MD5Init(&md5);
00049 
00050   for (plane = 0; plane < 3; ++plane) {
00051     const unsigned char *buf = img->planes[plane];
00052     const int stride = img->stride[plane];
00053     const int w = plane ? (img->d_w + 1) >> 1 : img->d_w;
00054     const int h = plane ? (img->d_h + 1) >> 1 : img->d_h;
00055 
00056     for (y = 0; y < h; ++y) {
00057       MD5Update(&md5, buf, w);
00058       buf += stride;
00059     }
00060   }
00061 
00062   MD5Final(digest, &md5);
00063 }
00064 
00065 static void print_md5(FILE *stream, unsigned char digest[16]) {
00066   int i;
00067 
00068   for (i = 0; i < 16; ++i) fprintf(stream, "%02x", digest[i]);
00069 }
00070 
00071 static const char *exec_name;
00072 
00073 void usage_exit(void) {
00074   fprintf(stderr, "Usage: %s <infile> <outfile>\n", exec_name);
00075   exit(EXIT_FAILURE);
00076 }
00077 
00078 int main(int argc, char **argv) {
00079   int frame_cnt = 0;
00080   FILE *outfile = NULL;
00081   vpx_codec_ctx_t codec;
00082   VpxVideoReader *reader = NULL;
00083   const VpxVideoInfo *info = NULL;
00084   const VpxInterface *decoder = NULL;
00085 
00086   exec_name = argv[0];
00087 
00088   if (argc != 3) die("Invalid number of arguments.");
00089 
00090   reader = vpx_video_reader_open(argv[1]);
00091   if (!reader) die("Failed to open %s for reading.", argv[1]);
00092 
00093   if (!(outfile = fopen(argv[2], "wb")))
00094     die("Failed to open %s for writing.", argv[2]);
00095 
00096   info = vpx_video_reader_get_info(reader);
00097 
00098   decoder = get_vpx_decoder_by_fourcc(info->codec_fourcc);
00099   if (!decoder) die("Unknown input codec.");
00100 
00101   printf("Using %s\n", vpx_codec_iface_name(decoder->codec_interface()));
00102 
00103   if (vpx_codec_dec_init(&codec, decoder->codec_interface(), NULL, 0))
00104     die_codec(&codec, "Failed to initialize decoder");
00105 
00106   while (vpx_video_reader_read_frame(reader)) {
00107     vpx_codec_iter_t iter = NULL;
00108     vpx_image_t *img = NULL;
00109     size_t frame_size = 0;
00110     const unsigned char *frame =
00111         vpx_video_reader_get_frame(reader, &frame_size);
00112     if (vpx_codec_decode(&codec, frame, (unsigned int)frame_size, NULL, 0))
00113       die_codec(&codec, "Failed to decode frame");
00114 
00115     while ((img = vpx_codec_get_frame(&codec, &iter)) != NULL) {
00116       unsigned char digest[16];
00117 
00118       get_image_md5(img, digest);
00119       print_md5(outfile, digest);
00120       fprintf(outfile, "  img-%dx%d-%04d.i420\n", img->d_w, img->d_h,
00121               ++frame_cnt);
00122     }
00123   }
00124 
00125   printf("Processed %d frames.\n", frame_cnt);
00126   if (vpx_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec.");
00127 
00128   vpx_video_reader_close(reader);
00129 
00130   fclose(outfile);
00131   return EXIT_SUCCESS;
00132 }

Generated on 16 Jun 2017 for WebM Codec SDK by  doxygen 1.6.1