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.

134 lines
3.8KB

  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 Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/md5.h"
  22. #include "avformat.h"
  23. #include "internal.h"
  24. struct MD5Context {
  25. struct AVMD5 *md5;
  26. };
  27. static void md5_finish(struct AVFormatContext *s, char *buf)
  28. {
  29. struct MD5Context *c = s->priv_data;
  30. uint8_t md5[16];
  31. int i, offset = strlen(buf);
  32. av_md5_final(c->md5, md5);
  33. for (i = 0; i < sizeof(md5); i++) {
  34. snprintf(buf + offset, 3, "%02"PRIx8, md5[i]);
  35. offset += 2;
  36. }
  37. buf[offset] = '\n';
  38. buf[offset+1] = 0;
  39. avio_write(s->pb, buf, strlen(buf));
  40. avio_flush(s->pb);
  41. }
  42. #if CONFIG_MD5_MUXER
  43. static int write_header(struct AVFormatContext *s)
  44. {
  45. struct MD5Context *c = s->priv_data;
  46. c->md5 = av_md5_alloc();
  47. if (!c->md5)
  48. return AVERROR(ENOMEM);
  49. av_md5_init(c->md5);
  50. return 0;
  51. }
  52. static int write_packet(struct AVFormatContext *s, AVPacket *pkt)
  53. {
  54. struct MD5Context *c = s->priv_data;
  55. av_md5_update(c->md5, pkt->data, pkt->size);
  56. return 0;
  57. }
  58. static int write_trailer(struct AVFormatContext *s)
  59. {
  60. struct MD5Context *c = s->priv_data;
  61. char buf[64] = "MD5=";
  62. md5_finish(s, buf);
  63. av_freep(&c->md5);
  64. return 0;
  65. }
  66. AVOutputFormat ff_md5_muxer = {
  67. .name = "md5",
  68. .long_name = NULL_IF_CONFIG_SMALL("MD5 testing"),
  69. .extensions = "",
  70. .priv_data_size = sizeof(struct MD5Context),
  71. .audio_codec = AV_CODEC_ID_PCM_S16LE,
  72. .video_codec = AV_CODEC_ID_RAWVIDEO,
  73. .write_header = write_header,
  74. .write_packet = write_packet,
  75. .write_trailer = write_trailer,
  76. .flags = AVFMT_NOTIMESTAMPS,
  77. };
  78. #endif
  79. #if CONFIG_FRAMEMD5_MUXER
  80. static int framemd5_write_header(struct AVFormatContext *s)
  81. {
  82. struct MD5Context *c = s->priv_data;
  83. c->md5 = av_md5_alloc();
  84. if (!c->md5)
  85. return AVERROR(ENOMEM);
  86. return ff_framehash_write_header(s);
  87. }
  88. static int framemd5_write_packet(struct AVFormatContext *s, AVPacket *pkt)
  89. {
  90. struct MD5Context *c = s->priv_data;
  91. char buf[256];
  92. av_md5_init(c->md5);
  93. av_md5_update(c->md5, pkt->data, pkt->size);
  94. snprintf(buf, sizeof(buf) - 64, "%d, %10"PRId64", %10"PRId64", %8"PRId64", %8d, ",
  95. pkt->stream_index, pkt->dts, pkt->pts, pkt->duration, pkt->size);
  96. md5_finish(s, buf);
  97. return 0;
  98. }
  99. static int framemd5_write_trailer(struct AVFormatContext *s)
  100. {
  101. struct MD5Context *c = s->priv_data;
  102. av_freep(&c->md5);
  103. return 0;
  104. }
  105. AVOutputFormat ff_framemd5_muxer = {
  106. .name = "framemd5",
  107. .long_name = NULL_IF_CONFIG_SMALL("Per-frame MD5 testing"),
  108. .extensions = "",
  109. .priv_data_size = sizeof(struct MD5Context),
  110. .audio_codec = AV_CODEC_ID_PCM_S16LE,
  111. .video_codec = AV_CODEC_ID_RAWVIDEO,
  112. .write_header = framemd5_write_header,
  113. .write_packet = framemd5_write_packet,
  114. .write_trailer = framemd5_write_trailer,
  115. .flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
  116. AVFMT_TS_NEGATIVE,
  117. };
  118. #endif