api-h264-test.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. * H264 codec test.
  24. */
  25. #include "libavutil/adler32.h"
  26. #include "libavcodec/avcodec.h"
  27. #include "libavformat/avformat.h"
  28. #include "libavutil/imgutils.h"
  29. static int video_decode_example(const char *input_filename)
  30. {
  31. AVCodec *codec = NULL;
  32. AVCodecContext *ctx= NULL;
  33. AVCodecParameters *origin_par = NULL;
  34. AVFrame *fr = NULL;
  35. uint8_t *byte_buffer = NULL;
  36. AVPacket pkt;
  37. AVFormatContext *fmt_ctx = NULL;
  38. int number_of_written_bytes;
  39. int video_stream;
  40. int got_frame = 0;
  41. int byte_buffer_size;
  42. int i = 0;
  43. int result;
  44. int end_of_stream = 0;
  45. result = avformat_open_input(&fmt_ctx, input_filename, NULL, NULL);
  46. if (result < 0) {
  47. av_log(NULL, AV_LOG_ERROR, "Can't open file\n");
  48. return result;
  49. }
  50. result = avformat_find_stream_info(fmt_ctx, NULL);
  51. if (result < 0) {
  52. av_log(NULL, AV_LOG_ERROR, "Can't get stream info\n");
  53. return result;
  54. }
  55. video_stream = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
  56. if (video_stream < 0) {
  57. av_log(NULL, AV_LOG_ERROR, "Can't find video stream in input file\n");
  58. return -1;
  59. }
  60. origin_par = fmt_ctx->streams[video_stream]->codecpar;
  61. codec = avcodec_find_decoder(origin_par->codec_id);
  62. if (!codec) {
  63. av_log(NULL, AV_LOG_ERROR, "Can't find decoder\n");
  64. return -1;
  65. }
  66. ctx = avcodec_alloc_context3(codec);
  67. if (!ctx) {
  68. av_log(NULL, AV_LOG_ERROR, "Can't allocate decoder context\n");
  69. return AVERROR(ENOMEM);
  70. }
  71. result = avcodec_parameters_to_context(ctx, origin_par);
  72. if (result) {
  73. av_log(NULL, AV_LOG_ERROR, "Can't copy decoder context\n");
  74. return result;
  75. }
  76. result = avcodec_open2(ctx, codec, NULL);
  77. if (result < 0) {
  78. av_log(ctx, AV_LOG_ERROR, "Can't open decoder\n");
  79. return result;
  80. }
  81. fr = av_frame_alloc();
  82. if (!fr) {
  83. av_log(NULL, AV_LOG_ERROR, "Can't allocate frame\n");
  84. return AVERROR(ENOMEM);
  85. }
  86. byte_buffer_size = av_image_get_buffer_size(ctx->pix_fmt, ctx->width, ctx->height, 16);
  87. byte_buffer = av_malloc(byte_buffer_size);
  88. if (!byte_buffer) {
  89. av_log(NULL, AV_LOG_ERROR, "Can't allocate buffer\n");
  90. return AVERROR(ENOMEM);
  91. }
  92. printf("#tb %d: %d/%d\n", video_stream, fmt_ctx->streams[video_stream]->time_base.num, fmt_ctx->streams[video_stream]->time_base.den);
  93. i = 0;
  94. av_init_packet(&pkt);
  95. do {
  96. if (!end_of_stream)
  97. if (av_read_frame(fmt_ctx, &pkt) < 0)
  98. end_of_stream = 1;
  99. if (end_of_stream) {
  100. pkt.data = NULL;
  101. pkt.size = 0;
  102. }
  103. if (pkt.stream_index == video_stream || end_of_stream) {
  104. got_frame = 0;
  105. if (pkt.pts == AV_NOPTS_VALUE)
  106. pkt.pts = pkt.dts = i;
  107. result = avcodec_decode_video2(ctx, fr, &got_frame, &pkt);
  108. if (result < 0) {
  109. av_log(NULL, AV_LOG_ERROR, "Error decoding frame\n");
  110. return result;
  111. }
  112. if (got_frame) {
  113. number_of_written_bytes = av_image_copy_to_buffer(byte_buffer, byte_buffer_size,
  114. (const uint8_t* const *)fr->data, (const int*) fr->linesize,
  115. ctx->pix_fmt, ctx->width, ctx->height, 1);
  116. if (number_of_written_bytes < 0) {
  117. av_log(NULL, AV_LOG_ERROR, "Can't copy image to buffer\n");
  118. return number_of_written_bytes;
  119. }
  120. printf("%d, %10"PRId64", %10"PRId64", %8"PRId64", %8d, 0x%08lx\n", video_stream,
  121. fr->pts, fr->pkt_dts, av_frame_get_pkt_duration(fr),
  122. number_of_written_bytes, av_adler32_update(0, (const uint8_t*)byte_buffer, number_of_written_bytes));
  123. }
  124. av_packet_unref(&pkt);
  125. av_init_packet(&pkt);
  126. }
  127. i++;
  128. } while (!end_of_stream || got_frame);
  129. av_packet_unref(&pkt);
  130. av_frame_free(&fr);
  131. avcodec_close(ctx);
  132. avformat_close_input(&fmt_ctx);
  133. avcodec_free_context(&ctx);
  134. av_freep(&byte_buffer);
  135. return 0;
  136. }
  137. int main(int argc, char **argv)
  138. {
  139. if (argc < 2)
  140. {
  141. av_log(NULL, AV_LOG_ERROR, "Incorrect input\n");
  142. return 1;
  143. }
  144. av_register_all();
  145. if (video_decode_example(argv[1]) != 0)
  146. return 1;
  147. return 0;
  148. }