api-h264-test.c 5.5 KB

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