api-h264-slice-test.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. #include "config.h"
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #if HAVE_UNISTD_H
  28. #include <unistd.h>
  29. #endif
  30. #if HAVE_IO_H
  31. #include <io.h>
  32. #endif
  33. #include <sys/types.h>
  34. #include <sys/stat.h>
  35. #include <fcntl.h>
  36. #include "libavcodec/avcodec.h"
  37. #include "libavutil/pixdesc.h"
  38. #include "libavutil/hash.h"
  39. #include "libavutil/bswap.h"
  40. static int header = 0;
  41. static int decode(AVCodecContext *dec_ctx, AVFrame *frame,
  42. AVPacket *pkt)
  43. {
  44. static uint64_t frame_cnt = 0;
  45. int ret;
  46. ret = avcodec_send_packet(dec_ctx, pkt);
  47. if (ret < 0) {
  48. fprintf(stderr, "Error sending a packet for decoding: %s\n", av_err2str(ret));
  49. return ret;
  50. }
  51. while (ret >= 0) {
  52. const AVPixFmtDescriptor *desc;
  53. char sum[AV_HASH_MAX_SIZE * 2 + 1];
  54. struct AVHashContext *hash;
  55. ret = avcodec_receive_frame(dec_ctx, frame);
  56. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
  57. return 0;
  58. } else if (ret < 0) {
  59. fprintf(stderr, "Error during decoding: %s\n", av_err2str(ret));
  60. return ret;
  61. }
  62. if (!header) {
  63. printf(
  64. "#format: frame checksums\n"
  65. "#version: 2\n"
  66. "#hash: MD5\n"
  67. "#tb 0: 1/30\n"
  68. "#media_type 0: video\n"
  69. "#codec_id 0: rawvideo\n"
  70. "#dimensions 0: 352x288\n"
  71. "#sar 0: 128/117\n"
  72. "#stream#, dts, pts, duration, size, hash\n");
  73. header = 1;
  74. }
  75. desc = av_pix_fmt_desc_get(dec_ctx->pix_fmt);
  76. if ((ret = av_hash_alloc(&hash, "md5")) < 0) {
  77. return ret;
  78. }
  79. av_hash_init(hash);
  80. for (int i = 0; i < frame->height; i++)
  81. av_hash_update(hash, &frame->data[0][i * frame->linesize[0]], frame->width);
  82. for (int i = 0; i < frame->height >> desc->log2_chroma_h; i++)
  83. av_hash_update(hash, &frame->data[1][i * frame->linesize[1]], frame->width >> desc->log2_chroma_w);
  84. for (int i = 0; i < frame->height >> desc->log2_chroma_h; i++)
  85. av_hash_update(hash, &frame->data[2][i * frame->linesize[2]], frame->width >> desc->log2_chroma_w);
  86. av_hash_final_hex(hash, sum, av_hash_get_size(hash) * 2 + 1);
  87. printf("0, %10"PRId64", %10"PRId64", 1, %8d, %s\n",
  88. frame_cnt, frame_cnt,
  89. (frame->width * frame->height + 2 * (frame->height >> desc->log2_chroma_h) * (frame->width >> desc->log2_chroma_w)), sum);
  90. frame_cnt += 1;
  91. av_hash_freep(&hash);
  92. }
  93. return 0;
  94. }
  95. int main(int argc, char **argv)
  96. {
  97. const AVCodec *codec = NULL;
  98. AVCodecContext *c = NULL;
  99. AVFrame *frame = NULL;
  100. unsigned int threads;
  101. AVPacket *pkt;
  102. FILE *file = NULL;
  103. char * nal = NULL;
  104. int nals = 0, ret = 0;
  105. char *p;
  106. if (argc < 3) {
  107. fprintf(stderr, "Usage: %s <threads> <input file>\n", argv[0]);
  108. return -1;
  109. }
  110. if (!(threads = strtoul(argv[1], NULL, 0)))
  111. threads = 1;
  112. else if (threads > MAX_SLICES)
  113. threads = MAX_SLICES;
  114. #ifdef _WIN32
  115. setmode(fileno(stdout), O_BINARY);
  116. #endif
  117. if (!(pkt = av_packet_alloc())) {
  118. return -1;
  119. }
  120. nal = av_malloc(MAX_SLICES * UINT16_MAX + AV_INPUT_BUFFER_PADDING_SIZE);
  121. if (!nal)
  122. goto err;
  123. p = nal;
  124. if (!(codec = avcodec_find_decoder(AV_CODEC_ID_H264))) {
  125. fprintf(stderr, "Codec not found\n");
  126. ret = -1;
  127. goto err;
  128. }
  129. if (!(c = avcodec_alloc_context3(codec))) {
  130. fprintf(stderr, "Could not allocate video codec context\n");
  131. ret = -1;
  132. goto err;
  133. }
  134. c->width = 352;
  135. c->height = 288;
  136. c->flags2 |= AV_CODEC_FLAG2_CHUNKS;
  137. c->thread_type = FF_THREAD_SLICE;
  138. c->thread_count = threads;
  139. if ((ret = avcodec_open2(c, codec, NULL)) < 0) {
  140. fprintf(stderr, "Could not open codec\n");
  141. goto err;
  142. }
  143. #if HAVE_THREADS
  144. if (c->active_thread_type != FF_THREAD_SLICE) {
  145. fprintf(stderr, "Couldn't activate slice threading: %d\n", c->active_thread_type);
  146. ret = -1;
  147. goto err;
  148. }
  149. #else
  150. fprintf(stderr, "WARN: not using threads, only checking decoding slice NALUs\n");
  151. #endif
  152. if (!(frame = av_frame_alloc())) {
  153. fprintf(stderr, "Could not allocate video frame\n");
  154. ret = -1;
  155. goto err;
  156. }
  157. if (!(file = fopen(argv[2], "rb"))) {
  158. fprintf(stderr, "Couldn't open NALU file: %s\n", argv[2]);
  159. ret = -1;
  160. goto err;
  161. }
  162. while(1) {
  163. uint16_t size = 0;
  164. size_t ret = fread(&size, 1, sizeof(uint16_t), file);
  165. if (ret != sizeof(uint16_t))
  166. break;
  167. size = av_be2ne16(size);
  168. ret = fread(p, 1, size, file);
  169. if (ret != size) {
  170. perror("Couldn't read data");
  171. goto err;
  172. }
  173. p += ret;
  174. if (++nals >= threads) {
  175. int decret = 0;
  176. pkt->data = nal;
  177. pkt->size = p - nal;
  178. if ((decret = decode(c, frame, pkt)) < 0) {
  179. goto err;
  180. }
  181. memset(nal, 0, MAX_SLICES * UINT16_MAX + AV_INPUT_BUFFER_PADDING_SIZE);
  182. nals = 0;
  183. p = nal;
  184. }
  185. }
  186. if (nals) {
  187. pkt->data = nal;
  188. pkt->size = p - nal;
  189. if ((ret = decode(c, frame, pkt)) < 0) {
  190. goto err;
  191. }
  192. }
  193. ret = decode(c, frame, NULL);
  194. err:
  195. if (nal)
  196. av_free(nal);
  197. if (file)
  198. fclose(file);
  199. av_frame_free(&frame);
  200. avcodec_free_context(&c);
  201. av_packet_free(&pkt);
  202. return ret;
  203. }