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.

222 lines
6.3KB

  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 void 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. exit(1);
  52. }
  53. while (ret >= 0) {
  54. const AVPixFmtDescriptor *desc;
  55. char *sum;
  56. struct AVHashContext *hash;
  57. ret = avcodec_receive_frame(dec_ctx, frame);
  58. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
  59. return;
  60. } else if (ret < 0) {
  61. fprintf(stderr, "Error during decoding: %s\n", av_err2str(ret));
  62. exit(1);
  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. av_hash_alloc(&hash, "md5");
  79. av_hash_init(hash);
  80. sum = av_mallocz(av_hash_get_size(hash) * 2 + 1);
  81. for (int i = 0; i < frame->height; i++)
  82. av_hash_update(hash, &frame->data[0][i * frame->linesize[0]], frame->width);
  83. for (int i = 0; i < frame->height >> desc->log2_chroma_h; i++)
  84. av_hash_update(hash, &frame->data[1][i * frame->linesize[1]], frame->width >> desc->log2_chroma_w);
  85. for (int i = 0; i < frame->height >> desc->log2_chroma_h; i++)
  86. av_hash_update(hash, &frame->data[2][i * frame->linesize[2]], frame->width >> desc->log2_chroma_w);
  87. av_hash_final_hex(hash, sum, av_hash_get_size(hash) * 2 + 1);
  88. printf("0, %10"PRId64", %10"PRId64", 1, %8d, %s\n",
  89. frame_cnt, frame_cnt,
  90. (frame->width * frame->height + 2 * (frame->height >> desc->log2_chroma_h) * (frame->width >> desc->log2_chroma_w)), sum);
  91. frame_cnt += 1;
  92. av_hash_freep(&hash);
  93. av_free(sum);
  94. }
  95. }
  96. int main(int argc, char **argv)
  97. {
  98. const AVCodec *codec;
  99. AVCodecContext *c = NULL;
  100. AVFrame *frame;
  101. unsigned int threads;
  102. AVPacket *pkt;
  103. FILE *fd;
  104. char nal[MAX_SLICES * UINT16_MAX + AV_INPUT_BUFFER_PADDING_SIZE];
  105. int nals = 0;
  106. char *p = nal;
  107. if (argc < 4) {
  108. fprintf(stderr, "Usage: %s <threads> <input file> <output file>\n", argv[0]);
  109. exit(1);
  110. }
  111. if (!(threads = strtoul(argv[1], NULL, 0)))
  112. threads = 1;
  113. else if (threads > MAX_SLICES)
  114. threads = MAX_SLICES;
  115. #ifdef _WIN32
  116. setmode(fileno(stdout), O_BINARY);
  117. #endif
  118. if (!(pkt = av_packet_alloc()))
  119. exit(1);
  120. if (!(codec = avcodec_find_decoder(AV_CODEC_ID_H264))) {
  121. fprintf(stderr, "Codec not found\n");
  122. exit(1);
  123. }
  124. if (!(c = avcodec_alloc_context3(codec))) {
  125. fprintf(stderr, "Could not allocate video codec context\n");
  126. exit(1);
  127. }
  128. c->width = 352;
  129. c->height = 288;
  130. c->flags2 |= AV_CODEC_FLAG2_CHUNKS;
  131. c->thread_type = FF_THREAD_SLICE;
  132. c->thread_count = threads;
  133. if (avcodec_open2(c, codec, NULL) < 0) {
  134. fprintf(stderr, "Could not open codec\n");
  135. exit(1);
  136. }
  137. #if HAVE_THREADS
  138. if (c->active_thread_type != FF_THREAD_SLICE) {
  139. fprintf(stderr, "Couldn't activate slice threading: %d\n", c->active_thread_type);
  140. exit(1);
  141. }
  142. #else
  143. fprintf(stderr, "WARN: not using threads, only checking decoding slice NALUs\n");
  144. #endif
  145. if (!(frame = av_frame_alloc())) {
  146. fprintf(stderr, "Could not allocate video frame\n");
  147. exit(1);
  148. }
  149. if (!(fd = fopen(argv[2], "rb"))) {
  150. fprintf(stderr, "Couldn't open NALU file: %s\n", argv[2]);
  151. exit(1);
  152. }
  153. while(1) {
  154. uint16_t size = 0;
  155. ssize_t ret = fread(&size, 1, sizeof(uint16_t), fd);
  156. if (ret < 0) {
  157. perror("Couldn't read size");
  158. exit(1);
  159. } else if (ret != sizeof(uint16_t))
  160. break;
  161. size = ntohs(size);
  162. ret = fread(p, 1, size, fd);
  163. if (ret < 0 || ret != size) {
  164. perror("Couldn't read data");
  165. exit(1);
  166. }
  167. p += ret;
  168. if (++nals >= threads) {
  169. pkt->data = nal;
  170. pkt->size = p - nal;
  171. decode(c, frame, pkt);
  172. memset(nal, 0, MAX_SLICES * UINT16_MAX + AV_INPUT_BUFFER_PADDING_SIZE);
  173. nals = 0;
  174. p = nal;
  175. }
  176. }
  177. if (nals) {
  178. pkt->data = nal;
  179. pkt->size = p - nal;
  180. decode(c, frame, pkt);
  181. }
  182. decode(c, frame, NULL);
  183. fclose(fd);
  184. avcodec_free_context(&c);
  185. av_frame_free(&frame);
  186. av_packet_free(&pkt);
  187. return 0;
  188. }