decode_to_md5.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. // Frame-by-frame MD5 Checksum
  11. // ===========================
  12. //
  13. // This example builds upon the simple decoder loop to show how checksums
  14. // of the decoded output can be generated. These are used for validating
  15. // decoder implementations against the reference implementation, for example.
  16. //
  17. // MD5 algorithm
  18. // -------------
  19. // The Message-Digest 5 (MD5) is a well known hash function. We have provided
  20. // an implementation derived from the RSA Data Security, Inc. MD5 Message-Digest
  21. // Algorithm for your use. Our implmentation only changes the interface of this
  22. // reference code. You must include the `md5_utils.h` header for access to these
  23. // functions.
  24. //
  25. // Processing The Decoded Data
  26. // ---------------------------
  27. // Each row of the image is passed to the MD5 accumulator. First the Y plane
  28. // is processed, then U, then V. It is important to honor the image's `stride`
  29. // values.
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include "vpx/vp8dx.h"
  34. #include "vpx/vpx_decoder.h"
  35. #include "../md5_utils.h"
  36. #include "../tools_common.h"
  37. #include "../video_reader.h"
  38. #include "./vpx_config.h"
  39. static void get_image_md5(const vpx_image_t *img, unsigned char digest[16]) {
  40. int plane, y;
  41. MD5Context md5;
  42. MD5Init(&md5);
  43. for (plane = 0; plane < 3; ++plane) {
  44. const unsigned char *buf = img->planes[plane];
  45. const int stride = img->stride[plane];
  46. const int w = plane ? (img->d_w + 1) >> 1 : img->d_w;
  47. const int h = plane ? (img->d_h + 1) >> 1 : img->d_h;
  48. for (y = 0; y < h; ++y) {
  49. MD5Update(&md5, buf, w);
  50. buf += stride;
  51. }
  52. }
  53. MD5Final(digest, &md5);
  54. }
  55. static void print_md5(FILE *stream, unsigned char digest[16]) {
  56. int i;
  57. for (i = 0; i < 16; ++i) fprintf(stream, "%02x", digest[i]);
  58. }
  59. static const char *exec_name;
  60. void usage_exit(void) {
  61. fprintf(stderr, "Usage: %s <infile> <outfile>\n", exec_name);
  62. exit(EXIT_FAILURE);
  63. }
  64. int main(int argc, char **argv) {
  65. int frame_cnt = 0;
  66. FILE *outfile = NULL;
  67. vpx_codec_ctx_t codec;
  68. VpxVideoReader *reader = NULL;
  69. const VpxVideoInfo *info = NULL;
  70. const VpxInterface *decoder = NULL;
  71. exec_name = argv[0];
  72. if (argc != 3) die("Invalid number of arguments.");
  73. reader = vpx_video_reader_open(argv[1]);
  74. if (!reader) die("Failed to open %s for reading.", argv[1]);
  75. if (!(outfile = fopen(argv[2], "wb")))
  76. die("Failed to open %s for writing.", argv[2]);
  77. info = vpx_video_reader_get_info(reader);
  78. decoder = get_vpx_decoder_by_fourcc(info->codec_fourcc);
  79. if (!decoder) die("Unknown input codec.");
  80. printf("Using %s\n", vpx_codec_iface_name(decoder->codec_interface()));
  81. if (vpx_codec_dec_init(&codec, decoder->codec_interface(), NULL, 0))
  82. die_codec(&codec, "Failed to initialize decoder");
  83. while (vpx_video_reader_read_frame(reader)) {
  84. vpx_codec_iter_t iter = NULL;
  85. vpx_image_t *img = NULL;
  86. size_t frame_size = 0;
  87. const unsigned char *frame =
  88. vpx_video_reader_get_frame(reader, &frame_size);
  89. if (vpx_codec_decode(&codec, frame, (unsigned int)frame_size, NULL, 0))
  90. die_codec(&codec, "Failed to decode frame");
  91. while ((img = vpx_codec_get_frame(&codec, &iter)) != NULL) {
  92. unsigned char digest[16];
  93. get_image_md5(img, digest);
  94. print_md5(outfile, digest);
  95. fprintf(outfile, " img-%dx%d-%04d.i420\n", img->d_w, img->d_h,
  96. ++frame_cnt);
  97. }
  98. }
  99. printf("Processed %d frames.\n", frame_cnt);
  100. if (vpx_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec.");
  101. vpx_video_reader_close(reader);
  102. fclose(outfile);
  103. return EXIT_SUCCESS;
  104. }