api-h264-slice-test.c 6.6 KB

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