api-seek-test.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * Copyright (c) 2015 Ludmila Glinskih
  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. /**
  23. * Seek test.
  24. */
  25. #include "libavutil/adler32.h"
  26. #include "libavutil/mem.h"
  27. #include "libavcodec/avcodec.h"
  28. #include "libavformat/avformat.h"
  29. #include "libavutil/imgutils.h"
  30. int64_t *pts_array;
  31. uint32_t *crc_array;
  32. int size_of_array;
  33. int number_of_elements;
  34. static int add_crc_to_array(uint32_t crc, int64_t pts)
  35. {
  36. if (size_of_array <= number_of_elements) {
  37. if (size_of_array == 0)
  38. size_of_array = 10;
  39. size_of_array *= 2;
  40. crc_array = av_realloc_f(crc_array, size_of_array, sizeof(uint32_t));
  41. pts_array = av_realloc_f(pts_array, size_of_array, sizeof(int64_t));
  42. if ((crc_array == NULL) || (pts_array == NULL)) {
  43. av_log(NULL, AV_LOG_ERROR, "Can't allocate array to store crcs\n");
  44. return AVERROR(ENOMEM);
  45. }
  46. }
  47. crc_array[number_of_elements] = crc;
  48. pts_array[number_of_elements] = pts;
  49. number_of_elements++;
  50. return 0;
  51. }
  52. static int compare_crc_in_array(uint32_t crc, int64_t pts)
  53. {
  54. int i;
  55. for (i = 0; i < number_of_elements; i++) {
  56. if (pts_array[i] == pts) {
  57. if (crc_array[i] == crc) {
  58. printf("Comparing 0x%08"PRIx32" %"PRId64" %d is OK\n", crc, pts, i);
  59. return 0;
  60. }
  61. else {
  62. av_log(NULL, AV_LOG_ERROR, "Incorrect crc of a frame after seeking\n");
  63. return -1;
  64. }
  65. }
  66. }
  67. av_log(NULL, AV_LOG_ERROR, "Incorrect pts of a frame after seeking\n");
  68. return -1;
  69. }
  70. static int compute_crc_of_packets(AVFormatContext *fmt_ctx, int video_stream,
  71. AVCodecContext *ctx, AVPacket *pkt, AVFrame *fr,
  72. uint64_t ts_start, uint64_t ts_end, int no_seeking)
  73. {
  74. int number_of_written_bytes;
  75. int result;
  76. int byte_buffer_size;
  77. uint8_t *byte_buffer;
  78. uint32_t crc;
  79. byte_buffer_size = av_image_get_buffer_size(ctx->pix_fmt, ctx->width, ctx->height, 16);
  80. byte_buffer = av_malloc(byte_buffer_size);
  81. if (!byte_buffer) {
  82. av_log(NULL, AV_LOG_ERROR, "Can't allocate buffer\n");
  83. return AVERROR(ENOMEM);
  84. }
  85. if (!no_seeking) {
  86. result = av_seek_frame(fmt_ctx, video_stream, ts_start, AVSEEK_FLAG_ANY);
  87. printf("Seeking to %"PRId64", computing crc for frames with pts < %"PRId64"\n", ts_start, ts_end);
  88. if (result < 0) {
  89. av_log(NULL, AV_LOG_ERROR, "Error in seeking\n");
  90. return result;
  91. }
  92. avcodec_flush_buffers(ctx);
  93. }
  94. do {
  95. result = av_read_frame(fmt_ctx, pkt);
  96. if (result >= 0 && pkt->stream_index != video_stream) {
  97. av_packet_unref(pkt);
  98. continue;
  99. }
  100. if (result < 0)
  101. result = avcodec_send_packet(ctx, NULL);
  102. else {
  103. if (pkt->pts == AV_NOPTS_VALUE) {
  104. av_log(NULL, AV_LOG_ERROR, "Error: frames doesn't have pts values\n");
  105. return -1;
  106. }
  107. result = avcodec_send_packet(ctx, pkt);
  108. }
  109. av_packet_unref(pkt);
  110. if (result < 0) {
  111. av_log(NULL, AV_LOG_ERROR, "Error submitting a packet for decoding\n");
  112. return result;
  113. }
  114. while (result >= 0) {
  115. result = avcodec_receive_frame(ctx, fr);
  116. if (result == AVERROR_EOF)
  117. goto finish;
  118. else if (result == AVERROR(EAGAIN)) {
  119. result = 0;
  120. break;
  121. } else if (result < 0) {
  122. av_log(NULL, AV_LOG_ERROR, "Error decoding frame\n");
  123. return result;
  124. }
  125. number_of_written_bytes = av_image_copy_to_buffer(byte_buffer, byte_buffer_size,
  126. (const uint8_t* const *)fr->data, (const int*) fr->linesize,
  127. ctx->pix_fmt, ctx->width, ctx->height, 1);
  128. if (number_of_written_bytes < 0) {
  129. av_log(NULL, AV_LOG_ERROR, "Can't copy image to buffer\n");
  130. return number_of_written_bytes;
  131. }
  132. if ((!no_seeking) && (fr->pts > ts_end))
  133. break;
  134. crc = av_adler32_update(0, (const uint8_t*)byte_buffer, number_of_written_bytes);
  135. printf("%10"PRId64", 0x%08"PRIx32"\n", fr->pts, crc);
  136. if (no_seeking) {
  137. if (add_crc_to_array(crc, fr->pts) < 0)
  138. return -1;
  139. }
  140. else {
  141. if (compare_crc_in_array(crc, fr->pts) < 0)
  142. return -1;
  143. }
  144. av_frame_unref(fr);
  145. }
  146. } while (result >= 0 && (no_seeking || (fr->pts + fr->duration <= ts_end)));
  147. finish:
  148. av_freep(&byte_buffer);
  149. return 0;
  150. }
  151. static long int read_seek_range(const char *string_with_number)
  152. {
  153. long int number;
  154. char *end_of_string = NULL;
  155. number = strtol(string_with_number, &end_of_string, 10);
  156. if ((strlen(string_with_number) != end_of_string - string_with_number) || (number < 0)) {
  157. av_log(NULL, AV_LOG_ERROR, "Incorrect input ranges of seeking\n");
  158. return -1;
  159. }
  160. else if ((number == LONG_MAX) || (number == LONG_MIN)) {
  161. if (errno == ERANGE) {
  162. av_log(NULL, AV_LOG_ERROR, "Incorrect input ranges of seeking\n");
  163. return -1;
  164. }
  165. }
  166. return number;
  167. }
  168. static int seek_test(const char *input_filename, const char *start, const char *end)
  169. {
  170. const AVCodec *codec = NULL;
  171. AVCodecContext *ctx= NULL;
  172. AVCodecParameters *origin_par = NULL;
  173. AVPacket *pkt = NULL;
  174. AVFrame *fr = NULL;
  175. AVFormatContext *fmt_ctx = NULL;
  176. int video_stream;
  177. int result;
  178. int i, j;
  179. long int start_ts, end_ts;
  180. size_of_array = 0;
  181. number_of_elements = 0;
  182. crc_array = NULL;
  183. pts_array = NULL;
  184. result = avformat_open_input(&fmt_ctx, input_filename, NULL, NULL);
  185. if (result < 0) {
  186. av_log(NULL, AV_LOG_ERROR, "Can't open file\n");
  187. return result;
  188. }
  189. result = avformat_find_stream_info(fmt_ctx, NULL);
  190. if (result < 0) {
  191. av_log(NULL, AV_LOG_ERROR, "Can't get stream info\n");
  192. goto end;
  193. }
  194. start_ts = read_seek_range(start);
  195. end_ts = read_seek_range(end);
  196. if ((start_ts < 0) || (end_ts < 0)) {
  197. result = -1;
  198. goto end;
  199. }
  200. //TODO: add ability to work with audio format
  201. video_stream = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
  202. if (video_stream < 0) {
  203. av_log(NULL, AV_LOG_ERROR, "Can't find video stream in input file\n");
  204. result = video_stream;
  205. goto end;
  206. }
  207. origin_par = fmt_ctx->streams[video_stream]->codecpar;
  208. codec = avcodec_find_decoder(origin_par->codec_id);
  209. if (!codec) {
  210. av_log(NULL, AV_LOG_ERROR, "Can't find decoder\n");
  211. result = AVERROR_DECODER_NOT_FOUND;
  212. goto end;
  213. }
  214. ctx = avcodec_alloc_context3(codec);
  215. if (!ctx) {
  216. av_log(NULL, AV_LOG_ERROR, "Can't allocate decoder context\n");
  217. result = AVERROR(ENOMEM);
  218. goto end;
  219. }
  220. result = avcodec_parameters_to_context(ctx, origin_par);
  221. if (result) {
  222. av_log(NULL, AV_LOG_ERROR, "Can't copy decoder context\n");
  223. goto end;
  224. }
  225. result = avcodec_open2(ctx, codec, NULL);
  226. if (result < 0) {
  227. av_log(ctx, AV_LOG_ERROR, "Can't open decoder\n");
  228. goto end;
  229. }
  230. fr = av_frame_alloc();
  231. if (!fr) {
  232. av_log(NULL, AV_LOG_ERROR, "Can't allocate frame\n");
  233. result = AVERROR(ENOMEM);
  234. goto end;
  235. }
  236. pkt = av_packet_alloc();
  237. if (!pkt) {
  238. av_log(NULL, AV_LOG_ERROR, "Cannot allocate packet\n");
  239. result = AVERROR(ENOMEM);
  240. goto end;
  241. }
  242. result = compute_crc_of_packets(fmt_ctx, video_stream, ctx, pkt, fr, 0, 0, 1);
  243. if (result != 0)
  244. goto end;
  245. for (i = start_ts; i < end_ts; i += 100) {
  246. for (j = i + 100; j < end_ts; j += 100) {
  247. result = compute_crc_of_packets(fmt_ctx, video_stream, ctx, pkt, fr, i, j, 0);
  248. if (result != 0)
  249. break;
  250. }
  251. }
  252. end:
  253. av_freep(&crc_array);
  254. av_freep(&pts_array);
  255. av_packet_free(&pkt);
  256. av_frame_free(&fr);
  257. avformat_close_input(&fmt_ctx);
  258. avcodec_free_context(&ctx);
  259. return result;
  260. }
  261. int main(int argc, char **argv)
  262. {
  263. if (argc < 4) {
  264. av_log(NULL, AV_LOG_ERROR, "Incorrect input\n");
  265. return 1;
  266. }
  267. if (seek_test(argv[1], argv[2], argv[3]) != 0)
  268. return 1;
  269. return 0;
  270. }