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.

234 lines
6.6KB

  1. /*
  2. * Copyright (c) 2001 Fabrice Bellard
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. #define MAX_SLICES 8
  23. // ./fate 2 ./crew_cif out.y4m
  24. #include "config.h"
  25. #include <stdbool.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #if HAVE_UNISTD_H
  30. #include <unistd.h>
  31. #endif
  32. #if HAVE_IO_H
  33. #include <io.h>
  34. #endif
  35. #include <sys/types.h>
  36. #include <sys/stat.h>
  37. #include <fcntl.h>
  38. #include "libavformat/network.h"
  39. #include "libavcodec/avcodec.h"
  40. #include "libavutil/pixdesc.h"
  41. #include "libavutil/hash.h"
  42. static int header = 0;
  43. static int decode(AVCodecContext *dec_ctx, AVFrame *frame,
  44. AVPacket *pkt)
  45. {
  46. static uint64_t frame_cnt = 0;
  47. int ret;
  48. ret = avcodec_send_packet(dec_ctx, pkt);
  49. if (ret < 0) {
  50. fprintf(stderr, "Error sending a packet for decoding: %s\n", av_err2str(ret));
  51. return ret;
  52. }
  53. while (ret >= 0) {
  54. const AVPixFmtDescriptor *desc;
  55. char sum[AV_HASH_MAX_SIZE * 2 + 1];
  56. struct AVHashContext *hash;
  57. ret = avcodec_receive_frame(dec_ctx, frame);
  58. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
  59. return 0;
  60. } else if (ret < 0) {
  61. fprintf(stderr, "Error during decoding: %s\n", av_err2str(ret));
  62. return ret;
  63. }
  64. if (!header) {
  65. printf(
  66. "#format: frame checksums\n"
  67. "#version: 2\n"
  68. "#hash: MD5\n"
  69. "#tb 0: 1/30\n"
  70. "#media_type 0: video\n"
  71. "#codec_id 0: rawvideo\n"
  72. "#dimensions 0: 352x288\n"
  73. "#sar 0: 128/117\n"
  74. "#stream#, dts, pts, duration, size, hash\n");
  75. header = 1;
  76. }
  77. desc = av_pix_fmt_desc_get(dec_ctx->pix_fmt);
  78. if ((ret = av_hash_alloc(&hash, "md5")) < 0) {
  79. return ret;
  80. }
  81. av_hash_init(hash);
  82. for (int i = 0; i < frame->height; i++)
  83. av_hash_update(hash, &frame->data[0][i * frame->linesize[0]], frame->width);
  84. for (int i = 0; i < frame->height >> desc->log2_chroma_h; i++)
  85. av_hash_update(hash, &frame->data[1][i * frame->linesize[1]], frame->width >> desc->log2_chroma_w);
  86. for (int i = 0; i < frame->height >> desc->log2_chroma_h; i++)
  87. av_hash_update(hash, &frame->data[2][i * frame->linesize[2]], frame->width >> desc->log2_chroma_w);
  88. av_hash_final_hex(hash, sum, av_hash_get_size(hash) * 2 + 1);
  89. printf("0, %10"PRId64", %10"PRId64", 1, %8d, %s\n",
  90. frame_cnt, frame_cnt,
  91. (frame->width * frame->height + 2 * (frame->height >> desc->log2_chroma_h) * (frame->width >> desc->log2_chroma_w)), sum);
  92. frame_cnt += 1;
  93. av_hash_freep(&hash);
  94. }
  95. return 0;
  96. }
  97. int main(int argc, char **argv)
  98. {
  99. const AVCodec *codec = NULL;
  100. AVCodecContext *c = NULL;
  101. AVFrame *frame = NULL;
  102. unsigned int threads;
  103. AVPacket *pkt;
  104. FILE *file = NULL;
  105. char nal[MAX_SLICES * UINT16_MAX + AV_INPUT_BUFFER_PADDING_SIZE];
  106. int nals = 0, ret = 0;
  107. char *p = nal;
  108. if (argc < 4) {
  109. fprintf(stderr, "Usage: %s <threads> <input file> <output file>\n", argv[0]);
  110. return -1;
  111. }
  112. if (!(threads = strtoul(argv[1], NULL, 0)))
  113. threads = 1;
  114. else if (threads > MAX_SLICES)
  115. threads = MAX_SLICES;
  116. #ifdef _WIN32
  117. setmode(fileno(stdout), O_BINARY);
  118. #endif
  119. if (!(pkt = av_packet_alloc())) {
  120. return -1;
  121. }
  122. if (!(codec = avcodec_find_decoder(AV_CODEC_ID_H264))) {
  123. fprintf(stderr, "Codec not found\n");
  124. ret = -1;
  125. goto err;
  126. }
  127. if (!(c = avcodec_alloc_context3(codec))) {
  128. fprintf(stderr, "Could not allocate video codec context\n");
  129. ret = -1;
  130. goto err;
  131. }
  132. c->width = 352;
  133. c->height = 288;
  134. c->flags2 |= AV_CODEC_FLAG2_CHUNKS;
  135. c->thread_type = FF_THREAD_SLICE;
  136. c->thread_count = threads;
  137. if ((ret = avcodec_open2(c, codec, NULL)) < 0) {
  138. fprintf(stderr, "Could not open codec\n");
  139. goto err;
  140. }
  141. #if HAVE_THREADS
  142. if (c->active_thread_type != FF_THREAD_SLICE) {
  143. fprintf(stderr, "Couldn't activate slice threading: %d\n", c->active_thread_type);
  144. ret = -1;
  145. goto err;
  146. }
  147. #else
  148. fprintf(stderr, "WARN: not using threads, only checking decoding slice NALUs\n");
  149. #endif
  150. if (!(frame = av_frame_alloc())) {
  151. fprintf(stderr, "Could not allocate video frame\n");
  152. ret = -1;
  153. goto err;
  154. }
  155. if (!(file = fopen(argv[2], "rb"))) {
  156. fprintf(stderr, "Couldn't open NALU file: %s\n", argv[2]);
  157. ret = -1;
  158. goto err;
  159. }
  160. while(1) {
  161. uint16_t size = 0;
  162. size_t ret = fread(&size, 1, sizeof(uint16_t), file);
  163. if (ret != sizeof(uint16_t))
  164. break;
  165. size = ntohs(size);
  166. ret = fread(p, 1, size, file);
  167. if (ret != size) {
  168. perror("Couldn't read data");
  169. goto err;
  170. }
  171. p += ret;
  172. if (++nals >= threads) {
  173. int decret = 0;
  174. pkt->data = nal;
  175. pkt->size = p - nal;
  176. if ((decret = decode(c, frame, pkt)) < 0) {
  177. goto err;
  178. }
  179. memset(nal, 0, MAX_SLICES * UINT16_MAX + AV_INPUT_BUFFER_PADDING_SIZE);
  180. nals = 0;
  181. p = nal;
  182. }
  183. }
  184. if (nals) {
  185. pkt->data = nal;
  186. pkt->size = p - nal;
  187. if ((ret = decode(c, frame, pkt)) < 0) {
  188. goto err;
  189. }
  190. }
  191. ret = decode(c, frame, NULL);
  192. err:
  193. if (file)
  194. fclose(file);
  195. av_frame_free(&frame);
  196. avcodec_free_context(&c);
  197. av_packet_free(&pkt);
  198. return ret;
  199. }