You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
5.1KB

  1. /*
  2. * MD5 encoder (for codec/format testing)
  3. * Copyright (c) 2009 Reimar Döffinger, based on crcenc (c) 2002 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/avassert.h"
  22. #include "libavutil/avstring.h"
  23. #include "libavutil/hash.h"
  24. #include "libavutil/opt.h"
  25. #include "avformat.h"
  26. #include "internal.h"
  27. struct MD5Context {
  28. const AVClass *avclass;
  29. struct AVHashContext *hash;
  30. char *hash_name;
  31. };
  32. static void md5_finish(struct AVFormatContext *s, char *buf)
  33. {
  34. struct MD5Context *c = s->priv_data;
  35. uint8_t md5[AV_HASH_MAX_SIZE];
  36. int i, offset = strlen(buf);
  37. int len = av_hash_get_size(c->hash);
  38. av_assert0(len > 0 && len <= sizeof(md5));
  39. av_hash_final(c->hash, md5);
  40. for (i = 0; i < len; i++) {
  41. snprintf(buf + offset, 3, "%02"PRIx8, md5[i]);
  42. offset += 2;
  43. }
  44. buf[offset] = '\n';
  45. buf[offset+1] = 0;
  46. avio_write(s->pb, buf, strlen(buf));
  47. avio_flush(s->pb);
  48. }
  49. #define OFFSET(x) offsetof(struct MD5Context, x)
  50. #define ENC AV_OPT_FLAG_ENCODING_PARAM
  51. static const AVOption hash_options[] = {
  52. { "hash", "set hash to use", OFFSET(hash_name), AV_OPT_TYPE_STRING, {.str = "md5"}, 0, 0, ENC },
  53. { NULL },
  54. };
  55. static const AVClass md5enc_class = {
  56. .class_name = "hash encoder class",
  57. .item_name = av_default_item_name,
  58. .option = hash_options,
  59. .version = LIBAVUTIL_VERSION_INT,
  60. };
  61. #if CONFIG_MD5_MUXER
  62. static int write_header(struct AVFormatContext *s)
  63. {
  64. struct MD5Context *c = s->priv_data;
  65. int res = av_hash_alloc(&c->hash, c->hash_name);
  66. if (res < 0)
  67. return res;
  68. av_hash_init(c->hash);
  69. return 0;
  70. }
  71. static int write_packet(struct AVFormatContext *s, AVPacket *pkt)
  72. {
  73. struct MD5Context *c = s->priv_data;
  74. av_hash_update(c->hash, pkt->data, pkt->size);
  75. return 0;
  76. }
  77. static int write_trailer(struct AVFormatContext *s)
  78. {
  79. struct MD5Context *c = s->priv_data;
  80. char buf[256];
  81. av_strlcpy(buf, av_hash_get_name(c->hash), sizeof(buf) - 200);
  82. av_strlcat(buf, "=", sizeof(buf) - 200);
  83. md5_finish(s, buf);
  84. av_hash_freep(&c->hash);
  85. return 0;
  86. }
  87. AVOutputFormat ff_md5_muxer = {
  88. .name = "md5",
  89. .long_name = NULL_IF_CONFIG_SMALL("MD5 testing"),
  90. .priv_data_size = sizeof(struct MD5Context),
  91. .audio_codec = AV_CODEC_ID_PCM_S16LE,
  92. .video_codec = AV_CODEC_ID_RAWVIDEO,
  93. .write_header = write_header,
  94. .write_packet = write_packet,
  95. .write_trailer = write_trailer,
  96. .flags = AVFMT_NOTIMESTAMPS,
  97. .priv_class = &md5enc_class,
  98. };
  99. #endif
  100. #if CONFIG_FRAMEMD5_MUXER
  101. static int framemd5_write_header(struct AVFormatContext *s)
  102. {
  103. struct MD5Context *c = s->priv_data;
  104. int res = av_hash_alloc(&c->hash, c->hash_name);
  105. if (res < 0)
  106. return res;
  107. avio_printf(s->pb, "#format: frame checksums\n");
  108. avio_printf(s->pb, "#version: 1\n");
  109. avio_printf(s->pb, "#hash: %s\n", av_hash_get_name(c->hash));
  110. ff_framehash_write_header(s);
  111. avio_printf(s->pb, "#stream#, dts, pts, duration, size, hash\n");
  112. return 0;
  113. }
  114. static int framemd5_write_packet(struct AVFormatContext *s, AVPacket *pkt)
  115. {
  116. struct MD5Context *c = s->priv_data;
  117. char buf[256];
  118. av_hash_init(c->hash);
  119. av_hash_update(c->hash, pkt->data, pkt->size);
  120. snprintf(buf, sizeof(buf) - 64, "%d, %10"PRId64", %10"PRId64", %8d, %8d, ",
  121. pkt->stream_index, pkt->dts, pkt->pts, pkt->duration, pkt->size);
  122. md5_finish(s, buf);
  123. return 0;
  124. }
  125. static int framemd5_write_trailer(struct AVFormatContext *s)
  126. {
  127. struct MD5Context *c = s->priv_data;
  128. av_hash_freep(&c->hash);
  129. return 0;
  130. }
  131. static const AVClass framemd5_class = {
  132. .class_name = "frame hash encoder class",
  133. .item_name = av_default_item_name,
  134. .option = hash_options,
  135. .version = LIBAVUTIL_VERSION_INT,
  136. };
  137. AVOutputFormat ff_framemd5_muxer = {
  138. .name = "framemd5",
  139. .long_name = NULL_IF_CONFIG_SMALL("Per-frame MD5 testing"),
  140. .priv_data_size = sizeof(struct MD5Context),
  141. .audio_codec = AV_CODEC_ID_PCM_S16LE,
  142. .video_codec = AV_CODEC_ID_RAWVIDEO,
  143. .write_header = framemd5_write_header,
  144. .write_packet = framemd5_write_packet,
  145. .write_trailer = framemd5_write_trailer,
  146. .flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
  147. AVFMT_TS_NEGATIVE,
  148. .priv_class = &framemd5_class,
  149. };
  150. #endif