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.

388 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. avio_flush(s->pb);
  143. }
  144. return 0;
  145. }
  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. #endif
  158. #if CONFIG_HASH_MUXER
  159. static const AVClass hashenc_class = {
  160. .class_name = "hash muxer",
  161. .item_name = av_default_item_name,
  162. .option = hash_options,
  163. .version = LIBAVUTIL_VERSION_INT,
  164. };
  165. AVOutputFormat ff_hash_muxer = {
  166. .name = "hash",
  167. .long_name = NULL_IF_CONFIG_SMALL("Hash testing"),
  168. .priv_data_size = sizeof(struct HashContext),
  169. .audio_codec = AV_CODEC_ID_PCM_S16LE,
  170. .video_codec = AV_CODEC_ID_RAWVIDEO,
  171. .init = hash_init,
  172. .write_packet = hash_write_packet,
  173. .write_trailer = hash_write_trailer,
  174. .deinit = hash_free,
  175. .flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
  176. AVFMT_TS_NEGATIVE,
  177. .priv_class = &hashenc_class,
  178. };
  179. #endif
  180. #if CONFIG_MD5_MUXER
  181. static const AVClass md5enc_class = {
  182. .class_name = "MD5 muxer",
  183. .item_name = av_default_item_name,
  184. .option = md5_options,
  185. .version = LIBAVUTIL_VERSION_INT,
  186. };
  187. AVOutputFormat ff_md5_muxer = {
  188. .name = "md5",
  189. .long_name = NULL_IF_CONFIG_SMALL("MD5 testing"),
  190. .priv_data_size = sizeof(struct HashContext),
  191. .audio_codec = AV_CODEC_ID_PCM_S16LE,
  192. .video_codec = AV_CODEC_ID_RAWVIDEO,
  193. .init = hash_init,
  194. .write_packet = hash_write_packet,
  195. .write_trailer = hash_write_trailer,
  196. .deinit = hash_free,
  197. .flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
  198. AVFMT_TS_NEGATIVE,
  199. .priv_class = &md5enc_class,
  200. };
  201. #endif
  202. #if CONFIG_STREAMHASH_MUXER
  203. static const AVClass streamhashenc_class = {
  204. .class_name = "stream hash muxer",
  205. .item_name = av_default_item_name,
  206. .option = streamhash_options,
  207. .version = LIBAVUTIL_VERSION_INT,
  208. };
  209. AVOutputFormat ff_streamhash_muxer = {
  210. .name = "streamhash",
  211. .long_name = NULL_IF_CONFIG_SMALL("Per-stream hash testing"),
  212. .priv_data_size = sizeof(struct HashContext),
  213. .audio_codec = AV_CODEC_ID_PCM_S16LE,
  214. .video_codec = AV_CODEC_ID_RAWVIDEO,
  215. .init = streamhash_init,
  216. .write_packet = hash_write_packet,
  217. .write_trailer = hash_write_trailer,
  218. .deinit = hash_free,
  219. .flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
  220. AVFMT_TS_NEGATIVE,
  221. .priv_class = &streamhashenc_class,
  222. };
  223. #endif
  224. #if CONFIG_FRAMEHASH_MUXER || CONFIG_FRAMEMD5_MUXER
  225. static void framehash_print_extradata(struct AVFormatContext *s)
  226. {
  227. int i;
  228. for (i = 0; i < s->nb_streams; i++) {
  229. AVStream *st = s->streams[i];
  230. AVCodecParameters *par = st->codecpar;
  231. if (par->extradata) {
  232. struct HashContext *c = s->priv_data;
  233. char buf[AV_HASH_MAX_SIZE*2+1];
  234. avio_printf(s->pb, "#extradata %d, %31d, ", i, par->extradata_size);
  235. av_hash_init(c->hashes[0]);
  236. av_hash_update(c->hashes[0], par->extradata, par->extradata_size);
  237. av_hash_final_hex(c->hashes[0], buf, sizeof(buf));
  238. avio_write(s->pb, buf, strlen(buf));
  239. avio_printf(s->pb, "\n");
  240. }
  241. }
  242. }
  243. static int framehash_init(struct AVFormatContext *s)
  244. {
  245. int res;
  246. struct HashContext *c = s->priv_data;
  247. c->per_stream = 0;
  248. c->hashes = av_mallocz_array(1, sizeof(*c->hashes));
  249. if (!c->hashes)
  250. return AVERROR(ENOMEM);
  251. res = av_hash_alloc(&c->hashes[0], c->hash_name);
  252. if (res < 0)
  253. return res;
  254. return 0;
  255. }
  256. static int framehash_write_header(struct AVFormatContext *s)
  257. {
  258. struct HashContext *c = s->priv_data;
  259. avio_printf(s->pb, "#format: frame checksums\n");
  260. avio_printf(s->pb, "#version: %d\n", c->format_version);
  261. avio_printf(s->pb, "#hash: %s\n", av_hash_get_name(c->hashes[0]));
  262. framehash_print_extradata(s);
  263. ff_framehash_write_header(s);
  264. avio_printf(s->pb, "#stream#, dts, pts, duration, size, hash\n");
  265. return 0;
  266. }
  267. static int framehash_write_packet(struct AVFormatContext *s, AVPacket *pkt)
  268. {
  269. struct HashContext *c = s->priv_data;
  270. char buf[AV_HASH_MAX_SIZE*2+128];
  271. int len;
  272. av_hash_init(c->hashes[0]);
  273. av_hash_update(c->hashes[0], pkt->data, pkt->size);
  274. snprintf(buf, sizeof(buf) - (AV_HASH_MAX_SIZE * 2 + 1), "%d, %10"PRId64", %10"PRId64", %8"PRId64", %8d, ",
  275. pkt->stream_index, pkt->dts, pkt->pts, pkt->duration, pkt->size);
  276. len = strlen(buf);
  277. av_hash_final_hex(c->hashes[0], buf + len, sizeof(buf) - len);
  278. avio_write(s->pb, buf, strlen(buf));
  279. if (c->format_version > 1 && pkt->side_data_elems) {
  280. int i, j;
  281. avio_printf(s->pb, ", S=%d", pkt->side_data_elems);
  282. for (i = 0; i < pkt->side_data_elems; i++) {
  283. av_hash_init(c->hashes[0]);
  284. if (HAVE_BIGENDIAN && pkt->side_data[i].type == AV_PKT_DATA_PALETTE) {
  285. for (j = 0; j < pkt->side_data[i].size; j += sizeof(uint32_t)) {
  286. uint32_t data = AV_RL32(pkt->side_data[i].data + j);
  287. av_hash_update(c->hashes[0], (uint8_t *)&data, sizeof(uint32_t));
  288. }
  289. } else
  290. av_hash_update(c->hashes[0], pkt->side_data[i].data, pkt->side_data[i].size);
  291. snprintf(buf, sizeof(buf) - (AV_HASH_MAX_SIZE * 2 + 1), ", %8d, ", pkt->side_data[i].size);
  292. len = strlen(buf);
  293. av_hash_final_hex(c->hashes[0], buf + len, sizeof(buf) - len);
  294. avio_write(s->pb, buf, strlen(buf));
  295. }
  296. }
  297. avio_printf(s->pb, "\n");
  298. avio_flush(s->pb);
  299. return 0;
  300. }
  301. static void framehash_free(struct AVFormatContext *s)
  302. {
  303. struct HashContext *c = s->priv_data;
  304. if (c->hashes)
  305. av_hash_freep(&c->hashes[0]);
  306. av_freep(&c->hashes);
  307. }
  308. #endif
  309. #if CONFIG_FRAMEHASH_MUXER
  310. static const AVClass framehash_class = {
  311. .class_name = "frame hash muxer",
  312. .item_name = av_default_item_name,
  313. .option = framehash_options,
  314. .version = LIBAVUTIL_VERSION_INT,
  315. };
  316. AVOutputFormat ff_framehash_muxer = {
  317. .name = "framehash",
  318. .long_name = NULL_IF_CONFIG_SMALL("Per-frame hash testing"),
  319. .priv_data_size = sizeof(struct HashContext),
  320. .audio_codec = AV_CODEC_ID_PCM_S16LE,
  321. .video_codec = AV_CODEC_ID_RAWVIDEO,
  322. .init = framehash_init,
  323. .write_header = framehash_write_header,
  324. .write_packet = framehash_write_packet,
  325. .deinit = framehash_free,
  326. .flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
  327. AVFMT_TS_NEGATIVE,
  328. .priv_class = &framehash_class,
  329. };
  330. #endif
  331. #if CONFIG_FRAMEMD5_MUXER
  332. static const AVClass framemd5_class = {
  333. .class_name = "frame MD5 muxer",
  334. .item_name = av_default_item_name,
  335. .option = framemd5_options,
  336. .version = LIBAVUTIL_VERSION_INT,
  337. };
  338. AVOutputFormat ff_framemd5_muxer = {
  339. .name = "framemd5",
  340. .long_name = NULL_IF_CONFIG_SMALL("Per-frame MD5 testing"),
  341. .priv_data_size = sizeof(struct HashContext),
  342. .audio_codec = AV_CODEC_ID_PCM_S16LE,
  343. .video_codec = AV_CODEC_ID_RAWVIDEO,
  344. .init = framehash_init,
  345. .write_header = framehash_write_header,
  346. .write_packet = framehash_write_packet,
  347. .deinit = framehash_free,
  348. .flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
  349. AVFMT_TS_NEGATIVE,
  350. .priv_class = &framemd5_class,
  351. };
  352. #endif