md5enc.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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/md5.h"
  22. #include "avformat.h"
  23. #define PRIVSIZE 512
  24. static void md5_finish(struct AVFormatContext *s, char *buf)
  25. {
  26. uint8_t md5[16];
  27. int i, offset = strlen(buf);
  28. av_md5_final(s->priv_data, md5);
  29. for (i = 0; i < sizeof(md5); i++) {
  30. snprintf(buf + offset, 3, "%02"PRIx8, md5[i]);
  31. offset += 2;
  32. }
  33. buf[offset] = '\n';
  34. buf[offset+1] = 0;
  35. avio_write(s->pb, buf, strlen(buf));
  36. avio_flush(s->pb);
  37. }
  38. #if CONFIG_MD5_MUXER
  39. static int write_header(struct AVFormatContext *s)
  40. {
  41. if (PRIVSIZE < av_md5_size) {
  42. av_log(s, AV_LOG_ERROR, "Insuffient size for md5 context\n");
  43. return -1;
  44. }
  45. av_md5_init(s->priv_data);
  46. return 0;
  47. }
  48. static int write_packet(struct AVFormatContext *s, AVPacket *pkt)
  49. {
  50. av_md5_update(s->priv_data, pkt->data, pkt->size);
  51. return 0;
  52. }
  53. static int write_trailer(struct AVFormatContext *s)
  54. {
  55. char buf[64] = "MD5=";
  56. md5_finish(s, buf);
  57. return 0;
  58. }
  59. AVOutputFormat ff_md5_muxer = {
  60. .name = "md5",
  61. .long_name = NULL_IF_CONFIG_SMALL("MD5 testing format"),
  62. .extensions = "",
  63. .priv_data_size = PRIVSIZE,
  64. .audio_codec = CODEC_ID_PCM_S16LE,
  65. .video_codec = CODEC_ID_RAWVIDEO,
  66. .write_header = write_header,
  67. .write_packet = write_packet,
  68. .write_trailer = write_trailer,
  69. };
  70. #endif
  71. #if CONFIG_FRAMEMD5_MUXER
  72. static int framemd5_write_packet(struct AVFormatContext *s, AVPacket *pkt)
  73. {
  74. char buf[256];
  75. if (PRIVSIZE < av_md5_size) {
  76. av_log(s, AV_LOG_ERROR, "Insuffient size for md5 context\n");
  77. return -1;
  78. }
  79. av_md5_init(s->priv_data);
  80. av_md5_update(s->priv_data, pkt->data, pkt->size);
  81. snprintf(buf, sizeof(buf) - 64, "%d, %"PRId64", %d, ", pkt->stream_index, pkt->dts, pkt->size);
  82. md5_finish(s, buf);
  83. return 0;
  84. }
  85. AVOutputFormat ff_framemd5_muxer = {
  86. .name = "framemd5",
  87. .long_name = NULL_IF_CONFIG_SMALL("Per-frame MD5 testing format"),
  88. .extensions = "",
  89. .priv_data_size = PRIVSIZE,
  90. .audio_codec = CODEC_ID_PCM_S16LE,
  91. .video_codec = CODEC_ID_RAWVIDEO,
  92. .write_packet = framemd5_write_packet,
  93. };
  94. #endif