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.

171 lines
5.2KB

  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. int format_version;
  32. };
  33. static void md5_finish(struct AVFormatContext *s, char *buf)
  34. {
  35. struct MD5Context *c = s->priv_data;
  36. uint8_t md5[AV_HASH_MAX_SIZE];
  37. int i, offset = strlen(buf);
  38. int len = av_hash_get_size(c->hash);
  39. av_assert0(len > 0 && len <= sizeof(md5));
  40. av_hash_final(c->hash, md5);
  41. for (i = 0; i < len; i++) {
  42. snprintf(buf + offset, 3, "%02"PRIx8, md5[i]);
  43. offset += 2;
  44. }
  45. buf[offset] = '\n';
  46. buf[offset+1] = 0;
  47. avio_write(s->pb, buf, strlen(buf));
  48. avio_flush(s->pb);
  49. }
  50. #define OFFSET(x) offsetof(struct MD5Context, x)
  51. #define ENC AV_OPT_FLAG_ENCODING_PARAM
  52. static const AVOption hash_options[] = {
  53. { "hash", "set hash to use", OFFSET(hash_name), AV_OPT_TYPE_STRING, {.str = "md5"}, 0, 0, ENC },
  54. { "format_version", "file format version", OFFSET(format_version), AV_OPT_TYPE_INT, {.i64 = 1}, 1, 1, ENC },
  55. { NULL },
  56. };
  57. static const AVClass md5enc_class = {
  58. .class_name = "hash encoder class",
  59. .item_name = av_default_item_name,
  60. .option = hash_options,
  61. .version = LIBAVUTIL_VERSION_INT,
  62. };
  63. #if CONFIG_MD5_MUXER
  64. static int write_header(struct AVFormatContext *s)
  65. {
  66. struct MD5Context *c = s->priv_data;
  67. int res = av_hash_alloc(&c->hash, c->hash_name);
  68. if (res < 0)
  69. return res;
  70. av_hash_init(c->hash);
  71. return 0;
  72. }
  73. static int write_packet(struct AVFormatContext *s, AVPacket *pkt)
  74. {
  75. struct MD5Context *c = s->priv_data;
  76. av_hash_update(c->hash, pkt->data, pkt->size);
  77. return 0;
  78. }
  79. static int write_trailer(struct AVFormatContext *s)
  80. {
  81. struct MD5Context *c = s->priv_data;
  82. char buf[256];
  83. av_strlcpy(buf, av_hash_get_name(c->hash), sizeof(buf) - 200);
  84. av_strlcat(buf, "=", sizeof(buf) - 200);
  85. md5_finish(s, buf);
  86. av_hash_freep(&c->hash);
  87. return 0;
  88. }
  89. AVOutputFormat ff_md5_muxer = {
  90. .name = "md5",
  91. .long_name = NULL_IF_CONFIG_SMALL("MD5 testing"),
  92. .priv_data_size = sizeof(struct MD5Context),
  93. .audio_codec = AV_CODEC_ID_PCM_S16LE,
  94. .video_codec = AV_CODEC_ID_RAWVIDEO,
  95. .write_header = write_header,
  96. .write_packet = write_packet,
  97. .write_trailer = write_trailer,
  98. .flags = AVFMT_NOTIMESTAMPS,
  99. .priv_class = &md5enc_class,
  100. };
  101. #endif
  102. #if CONFIG_FRAMEMD5_MUXER
  103. static int framemd5_write_header(struct AVFormatContext *s)
  104. {
  105. struct MD5Context *c = s->priv_data;
  106. int res = av_hash_alloc(&c->hash, c->hash_name);
  107. if (res < 0)
  108. return res;
  109. avio_printf(s->pb, "#format: frame checksums\n");
  110. avio_printf(s->pb, "#version: %d\n", c->format_version);
  111. avio_printf(s->pb, "#hash: %s\n", av_hash_get_name(c->hash));
  112. ff_framehash_write_header(s);
  113. avio_printf(s->pb, "#stream#, dts, pts, duration, size, hash\n");
  114. return 0;
  115. }
  116. static int framemd5_write_packet(struct AVFormatContext *s, AVPacket *pkt)
  117. {
  118. struct MD5Context *c = s->priv_data;
  119. char buf[256];
  120. av_hash_init(c->hash);
  121. av_hash_update(c->hash, pkt->data, pkt->size);
  122. snprintf(buf, sizeof(buf) - 64, "%d, %10"PRId64", %10"PRId64", %8d, %8d, ",
  123. pkt->stream_index, pkt->dts, pkt->pts, pkt->duration, pkt->size);
  124. md5_finish(s, buf);
  125. return 0;
  126. }
  127. static int framemd5_write_trailer(struct AVFormatContext *s)
  128. {
  129. struct MD5Context *c = s->priv_data;
  130. av_hash_freep(&c->hash);
  131. return 0;
  132. }
  133. static const AVClass framemd5_class = {
  134. .class_name = "frame hash encoder class",
  135. .item_name = av_default_item_name,
  136. .option = hash_options,
  137. .version = LIBAVUTIL_VERSION_INT,
  138. };
  139. AVOutputFormat ff_framemd5_muxer = {
  140. .name = "framemd5",
  141. .long_name = NULL_IF_CONFIG_SMALL("Per-frame MD5 testing"),
  142. .priv_data_size = sizeof(struct MD5Context),
  143. .audio_codec = AV_CODEC_ID_PCM_S16LE,
  144. .video_codec = AV_CODEC_ID_RAWVIDEO,
  145. .write_header = framemd5_write_header,
  146. .write_packet = framemd5_write_packet,
  147. .write_trailer = framemd5_write_trailer,
  148. .flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
  149. AVFMT_TS_NEGATIVE,
  150. .priv_class = &framemd5_class,
  151. };
  152. #endif