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.

378 lines
12KB

  1. /*
  2. * Hash/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/intreadwrite.h"
  25. #include "libavutil/opt.h"
  26. #include "avformat.h"
  27. #include "internal.h"
  28. struct HashContext {
  29. const AVClass *avclass;
  30. struct AVHashContext **hashes;
  31. char *hash_name;
  32. int per_stream;
  33. int format_version;
  34. };
  35. #define OFFSET(x) offsetof(struct HashContext, x)
  36. #define ENC AV_OPT_FLAG_ENCODING_PARAM
  37. #define HASH_OPT(defaulttype) \
  38. { "hash", "set hash to use", OFFSET(hash_name), AV_OPT_TYPE_STRING, {.str = defaulttype}, 0, 0, ENC }
  39. #define FORMAT_VERSION_OPT \
  40. { "format_version", "file format version", OFFSET(format_version), AV_OPT_TYPE_INT, {.i64 = 2}, 1, 2, ENC }
  41. #if CONFIG_HASH_MUXER
  42. static const AVOption hash_options[] = {
  43. HASH_OPT("sha256"),
  44. { NULL },
  45. };
  46. #endif
  47. #if CONFIG_FRAMEHASH_MUXER
  48. static const AVOption framehash_options[] = {
  49. HASH_OPT("sha256"),
  50. FORMAT_VERSION_OPT,
  51. { NULL },
  52. };
  53. #endif
  54. #if CONFIG_STREAMHASH_MUXER
  55. static const AVOption streamhash_options[] = {
  56. HASH_OPT("sha256"),
  57. { NULL },
  58. };
  59. #endif
  60. #if CONFIG_MD5_MUXER
  61. static const AVOption md5_options[] = {
  62. HASH_OPT("md5"),
  63. { NULL },
  64. };
  65. #endif
  66. #if CONFIG_FRAMEMD5_MUXER
  67. static const AVOption framemd5_options[] = {
  68. HASH_OPT("md5"),
  69. FORMAT_VERSION_OPT,
  70. { NULL },
  71. };
  72. #endif
  73. #if CONFIG_HASH_MUXER || CONFIG_MD5_MUXER
  74. static int hash_init(struct AVFormatContext *s)
  75. {
  76. int res;
  77. struct HashContext *c = s->priv_data;
  78. c->per_stream = 0;
  79. c->hashes = av_mallocz_array(1, sizeof(*c->hashes));
  80. if (!c->hashes)
  81. return AVERROR(ENOMEM);
  82. res = av_hash_alloc(&c->hashes[0], c->hash_name);
  83. if (res < 0)
  84. return res;
  85. av_hash_init(c->hashes[0]);
  86. return 0;
  87. }
  88. #endif
  89. #if CONFIG_STREAMHASH_MUXER
  90. static int streamhash_init(struct AVFormatContext *s)
  91. {
  92. int res, i;
  93. struct HashContext *c = s->priv_data;
  94. c->per_stream = 1;
  95. c->hashes = av_mallocz_array(s->nb_streams, sizeof(*c->hashes));
  96. if (!c->hashes)
  97. return AVERROR(ENOMEM);
  98. for (i = 0; i < s->nb_streams; i++) {
  99. res = av_hash_alloc(&c->hashes[i], c->hash_name);
  100. if (res < 0) {
  101. return res;
  102. }
  103. av_hash_init(c->hashes[i]);
  104. }
  105. return 0;
  106. }
  107. #endif
  108. #if CONFIG_HASH_MUXER || CONFIG_MD5_MUXER || CONFIG_STREAMHASH_MUXER
  109. static char get_media_type_char(enum AVMediaType type)
  110. {
  111. switch (type) {
  112. case AVMEDIA_TYPE_VIDEO: return 'v';
  113. case AVMEDIA_TYPE_AUDIO: return 'a';
  114. case AVMEDIA_TYPE_DATA: return 'd';
  115. case AVMEDIA_TYPE_SUBTITLE: return 's';
  116. case AVMEDIA_TYPE_ATTACHMENT: return 't';
  117. default: return '?';
  118. }
  119. }
  120. static int hash_write_packet(struct AVFormatContext *s, AVPacket *pkt)
  121. {
  122. struct HashContext *c = s->priv_data;
  123. av_hash_update(c->hashes[c->per_stream ? pkt->stream_index : 0], pkt->data, pkt->size);
  124. return 0;
  125. }
  126. static int hash_write_trailer(struct AVFormatContext *s)
  127. {
  128. struct HashContext *c = s->priv_data;
  129. int num_hashes = c->per_stream ? s->nb_streams : 1;
  130. for (int i = 0; i < num_hashes; i++) {
  131. char buf[AV_HASH_MAX_SIZE*2+128];
  132. if (c->per_stream) {
  133. AVStream *st = s->streams[i];
  134. snprintf(buf, sizeof(buf) - 200, "%d,%c,%s=", i, get_media_type_char(st->codecpar->codec_type),
  135. av_hash_get_name(c->hashes[i]));
  136. } else {
  137. snprintf(buf, sizeof(buf) - 200, "%s=", av_hash_get_name(c->hashes[i]));
  138. }
  139. av_hash_final_hex(c->hashes[i], buf + strlen(buf), sizeof(buf) - strlen(buf));
  140. av_strlcatf(buf, sizeof(buf), "\n");
  141. avio_write(s->pb, buf, strlen(buf));
  142. }
  143. return 0;
  144. }
  145. #endif
  146. static void hash_free(struct AVFormatContext *s)
  147. {
  148. struct HashContext *c = s->priv_data;
  149. if (c->hashes) {
  150. int num_hashes = c->per_stream ? s->nb_streams : 1;
  151. for (int i = 0; i < num_hashes; i++) {
  152. av_hash_freep(&c->hashes[i]);
  153. }
  154. }
  155. av_freep(&c->hashes);
  156. }
  157. #if CONFIG_HASH_MUXER
  158. static const AVClass hashenc_class = {
  159. .class_name = "hash muxer",
  160. .item_name = av_default_item_name,
  161. .option = hash_options,
  162. .version = LIBAVUTIL_VERSION_INT,
  163. };
  164. AVOutputFormat ff_hash_muxer = {
  165. .name = "hash",
  166. .long_name = NULL_IF_CONFIG_SMALL("Hash testing"),
  167. .priv_data_size = sizeof(struct HashContext),
  168. .audio_codec = AV_CODEC_ID_PCM_S16LE,
  169. .video_codec = AV_CODEC_ID_RAWVIDEO,
  170. .init = hash_init,
  171. .write_packet = hash_write_packet,
  172. .write_trailer = hash_write_trailer,
  173. .deinit = hash_free,
  174. .flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
  175. AVFMT_TS_NEGATIVE,
  176. .priv_class = &hashenc_class,
  177. };
  178. #endif
  179. #if CONFIG_MD5_MUXER
  180. static const AVClass md5enc_class = {
  181. .class_name = "MD5 muxer",
  182. .item_name = av_default_item_name,
  183. .option = md5_options,
  184. .version = LIBAVUTIL_VERSION_INT,
  185. };
  186. AVOutputFormat ff_md5_muxer = {
  187. .name = "md5",
  188. .long_name = NULL_IF_CONFIG_SMALL("MD5 testing"),
  189. .priv_data_size = sizeof(struct HashContext),
  190. .audio_codec = AV_CODEC_ID_PCM_S16LE,
  191. .video_codec = AV_CODEC_ID_RAWVIDEO,
  192. .init = hash_init,
  193. .write_packet = hash_write_packet,
  194. .write_trailer = hash_write_trailer,
  195. .deinit = hash_free,
  196. .flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
  197. AVFMT_TS_NEGATIVE,
  198. .priv_class = &md5enc_class,
  199. };
  200. #endif
  201. #if CONFIG_STREAMHASH_MUXER
  202. static const AVClass streamhashenc_class = {
  203. .class_name = "stream hash muxer",
  204. .item_name = av_default_item_name,
  205. .option = streamhash_options,
  206. .version = LIBAVUTIL_VERSION_INT,
  207. };
  208. AVOutputFormat ff_streamhash_muxer = {
  209. .name = "streamhash",
  210. .long_name = NULL_IF_CONFIG_SMALL("Per-stream hash testing"),
  211. .priv_data_size = sizeof(struct HashContext),
  212. .audio_codec = AV_CODEC_ID_PCM_S16LE,
  213. .video_codec = AV_CODEC_ID_RAWVIDEO,
  214. .init = streamhash_init,
  215. .write_packet = hash_write_packet,
  216. .write_trailer = hash_write_trailer,
  217. .deinit = hash_free,
  218. .flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
  219. AVFMT_TS_NEGATIVE,
  220. .priv_class = &streamhashenc_class,
  221. };
  222. #endif
  223. #if CONFIG_FRAMEHASH_MUXER || CONFIG_FRAMEMD5_MUXER
  224. static void framehash_print_extradata(struct AVFormatContext *s)
  225. {
  226. int i;
  227. for (i = 0; i < s->nb_streams; i++) {
  228. AVStream *st = s->streams[i];
  229. AVCodecParameters *par = st->codecpar;
  230. if (par->extradata) {
  231. struct HashContext *c = s->priv_data;
  232. char buf[AV_HASH_MAX_SIZE*2+1];
  233. avio_printf(s->pb, "#extradata %d, %31d, ", i, par->extradata_size);
  234. av_hash_init(c->hashes[0]);
  235. av_hash_update(c->hashes[0], par->extradata, par->extradata_size);
  236. av_hash_final_hex(c->hashes[0], buf, sizeof(buf));
  237. avio_write(s->pb, buf, strlen(buf));
  238. avio_printf(s->pb, "\n");
  239. }
  240. }
  241. }
  242. static int framehash_init(struct AVFormatContext *s)
  243. {
  244. int res;
  245. struct HashContext *c = s->priv_data;
  246. c->per_stream = 0;
  247. c->hashes = av_mallocz_array(1, sizeof(*c->hashes));
  248. if (!c->hashes)
  249. return AVERROR(ENOMEM);
  250. res = av_hash_alloc(&c->hashes[0], c->hash_name);
  251. if (res < 0)
  252. return res;
  253. return 0;
  254. }
  255. static int framehash_write_header(struct AVFormatContext *s)
  256. {
  257. struct HashContext *c = s->priv_data;
  258. avio_printf(s->pb, "#format: frame checksums\n");
  259. avio_printf(s->pb, "#version: %d\n", c->format_version);
  260. avio_printf(s->pb, "#hash: %s\n", av_hash_get_name(c->hashes[0]));
  261. framehash_print_extradata(s);
  262. ff_framehash_write_header(s);
  263. avio_printf(s->pb, "#stream#, dts, pts, duration, size, hash\n");
  264. return 0;
  265. }
  266. static int framehash_write_packet(struct AVFormatContext *s, AVPacket *pkt)
  267. {
  268. struct HashContext *c = s->priv_data;
  269. char buf[AV_HASH_MAX_SIZE*2+128];
  270. int len;
  271. av_hash_init(c->hashes[0]);
  272. av_hash_update(c->hashes[0], pkt->data, pkt->size);
  273. snprintf(buf, sizeof(buf) - (AV_HASH_MAX_SIZE * 2 + 1), "%d, %10"PRId64", %10"PRId64", %8"PRId64", %8d, ",
  274. pkt->stream_index, pkt->dts, pkt->pts, pkt->duration, pkt->size);
  275. len = strlen(buf);
  276. av_hash_final_hex(c->hashes[0], buf + len, sizeof(buf) - len);
  277. avio_write(s->pb, buf, strlen(buf));
  278. if (c->format_version > 1 && pkt->side_data_elems) {
  279. int i, j;
  280. avio_printf(s->pb, ", S=%d", pkt->side_data_elems);
  281. for (i = 0; i < pkt->side_data_elems; i++) {
  282. av_hash_init(c->hashes[0]);
  283. if (HAVE_BIGENDIAN && pkt->side_data[i].type == AV_PKT_DATA_PALETTE) {
  284. for (j = 0; j < pkt->side_data[i].size; j += sizeof(uint32_t)) {
  285. uint32_t data = AV_RL32(pkt->side_data[i].data + j);
  286. av_hash_update(c->hashes[0], (uint8_t *)&data, sizeof(uint32_t));
  287. }
  288. } else
  289. av_hash_update(c->hashes[0], pkt->side_data[i].data, pkt->side_data[i].size);
  290. snprintf(buf, sizeof(buf) - (AV_HASH_MAX_SIZE * 2 + 1), ", %8d, ", pkt->side_data[i].size);
  291. len = strlen(buf);
  292. av_hash_final_hex(c->hashes[0], buf + len, sizeof(buf) - len);
  293. avio_write(s->pb, buf, strlen(buf));
  294. }
  295. }
  296. avio_printf(s->pb, "\n");
  297. return 0;
  298. }
  299. #endif
  300. #if CONFIG_FRAMEHASH_MUXER
  301. static const AVClass framehash_class = {
  302. .class_name = "frame hash muxer",
  303. .item_name = av_default_item_name,
  304. .option = framehash_options,
  305. .version = LIBAVUTIL_VERSION_INT,
  306. };
  307. AVOutputFormat ff_framehash_muxer = {
  308. .name = "framehash",
  309. .long_name = NULL_IF_CONFIG_SMALL("Per-frame hash testing"),
  310. .priv_data_size = sizeof(struct HashContext),
  311. .audio_codec = AV_CODEC_ID_PCM_S16LE,
  312. .video_codec = AV_CODEC_ID_RAWVIDEO,
  313. .init = framehash_init,
  314. .write_header = framehash_write_header,
  315. .write_packet = framehash_write_packet,
  316. .deinit = hash_free,
  317. .flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
  318. AVFMT_TS_NEGATIVE,
  319. .priv_class = &framehash_class,
  320. };
  321. #endif
  322. #if CONFIG_FRAMEMD5_MUXER
  323. static const AVClass framemd5_class = {
  324. .class_name = "frame MD5 muxer",
  325. .item_name = av_default_item_name,
  326. .option = framemd5_options,
  327. .version = LIBAVUTIL_VERSION_INT,
  328. };
  329. AVOutputFormat ff_framemd5_muxer = {
  330. .name = "framemd5",
  331. .long_name = NULL_IF_CONFIG_SMALL("Per-frame MD5 testing"),
  332. .priv_data_size = sizeof(struct HashContext),
  333. .audio_codec = AV_CODEC_ID_PCM_S16LE,
  334. .video_codec = AV_CODEC_ID_RAWVIDEO,
  335. .init = framehash_init,
  336. .write_header = framehash_write_header,
  337. .write_packet = framehash_write_packet,
  338. .deinit = hash_free,
  339. .flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
  340. AVFMT_TS_NEGATIVE,
  341. .priv_class = &framemd5_class,
  342. };
  343. #endif