api-h264-test.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. const 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 byte_buffer_size;
  42. int i = 0;
  43. int result;
  44. result = avformat_open_input(&fmt_ctx, input_filename, NULL, NULL);
  45. if (result < 0) {
  46. av_log(NULL, AV_LOG_ERROR, "Can't open file\n");
  47. return result;
  48. }
  49. result = avformat_find_stream_info(fmt_ctx, NULL);
  50. if (result < 0) {
  51. av_log(NULL, AV_LOG_ERROR, "Can't get stream info\n");
  52. return result;
  53. }
  54. video_stream = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
  55. if (video_stream < 0) {
  56. av_log(NULL, AV_LOG_ERROR, "Can't find video stream in input file\n");
  57. return -1;
  58. }
  59. origin_par = fmt_ctx->streams[video_stream]->codecpar;
  60. codec = avcodec_find_decoder(origin_par->codec_id);
  61. if (!codec) {
  62. av_log(NULL, AV_LOG_ERROR, "Can't find decoder\n");
  63. return -1;
  64. }
  65. ctx = avcodec_alloc_context3(codec);
  66. if (!ctx) {
  67. av_log(NULL, AV_LOG_ERROR, "Can't allocate decoder context\n");
  68. return AVERROR(ENOMEM);
  69. }
  70. result = avcodec_parameters_to_context(ctx, origin_par);
  71. if (result) {
  72. av_log(NULL, AV_LOG_ERROR, "Can't copy decoder context\n");
  73. return result;
  74. }
  75. result = avcodec_open2(ctx, codec, NULL);
  76. if (result < 0) {
  77. av_log(ctx, AV_LOG_ERROR, "Can't open decoder\n");
  78. return result;
  79. }
  80. fr = av_frame_alloc();
  81. if (!fr) {
  82. av_log(NULL, AV_LOG_ERROR, "Can't allocate frame\n");
  83. return AVERROR(ENOMEM);
  84. }
  85. pkt = av_packet_alloc();
  86. if (!pkt) {
  87. av_log(NULL, AV_LOG_ERROR, "Cannot allocate packet\n");
  88. return AVERROR(ENOMEM);
  89. }
  90. byte_buffer_size = av_image_get_buffer_size(ctx->pix_fmt, ctx->width, ctx->height, 16);
  91. byte_buffer = av_malloc(byte_buffer_size);
  92. if (!byte_buffer) {
  93. av_log(NULL, AV_LOG_ERROR, "Can't allocate buffer\n");
  94. return AVERROR(ENOMEM);
  95. }
  96. printf("#tb %d: %d/%d\n", video_stream, fmt_ctx->streams[video_stream]->time_base.num, fmt_ctx->streams[video_stream]->time_base.den);
  97. i = 0;
  98. result = 0;
  99. while (result >= 0) {
  100. result = av_read_frame(fmt_ctx, pkt);
  101. if (result >= 0 && pkt->stream_index != video_stream) {
  102. av_packet_unref(pkt);
  103. continue;
  104. }
  105. if (result < 0)
  106. result = avcodec_send_packet(ctx, NULL);
  107. else {
  108. if (pkt->pts == AV_NOPTS_VALUE)
  109. pkt->pts = pkt->dts = i;
  110. result = avcodec_send_packet(ctx, pkt);
  111. }
  112. av_packet_unref(pkt);
  113. if (result < 0) {
  114. av_log(NULL, AV_LOG_ERROR, "Error submitting a packet for decoding\n");
  115. return result;
  116. }
  117. while (result >= 0) {
  118. result = avcodec_receive_frame(ctx, fr);
  119. if (result == AVERROR_EOF)
  120. goto finish;
  121. else if (result == AVERROR(EAGAIN)) {
  122. result = 0;
  123. break;
  124. } else if (result < 0) {
  125. av_log(NULL, AV_LOG_ERROR, "Error decoding frame\n");
  126. return result;
  127. }
  128. number_of_written_bytes = av_image_copy_to_buffer(byte_buffer, byte_buffer_size,
  129. (const uint8_t* const *)fr->data, (const int*) fr->linesize,
  130. ctx->pix_fmt, ctx->width, ctx->height, 1);
  131. if (number_of_written_bytes < 0) {
  132. av_log(NULL, AV_LOG_ERROR, "Can't copy image to buffer\n");
  133. av_frame_unref(fr);
  134. return number_of_written_bytes;
  135. }
  136. printf("%d, %s, %s, %8"PRId64", %8d, 0x%08"PRIx32"\n", video_stream,
  137. av_ts2str(fr->pts), av_ts2str(fr->pkt_dts), fr->pkt_duration,
  138. number_of_written_bytes, av_adler32_update(0, (const uint8_t*)byte_buffer, number_of_written_bytes));
  139. av_frame_unref(fr);
  140. }
  141. i++;
  142. }
  143. finish:
  144. av_packet_free(&pkt);
  145. av_frame_free(&fr);
  146. avformat_close_input(&fmt_ctx);
  147. avcodec_free_context(&ctx);
  148. av_freep(&byte_buffer);
  149. return 0;
  150. }
  151. int main(int argc, char **argv)
  152. {
  153. if (argc < 2)
  154. {
  155. av_log(NULL, AV_LOG_ERROR, "Incorrect input\n");
  156. return 1;
  157. }
  158. if (video_decode_example(argv[1]) != 0)
  159. return 1;
  160. return 0;
  161. }