api-h264-test.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 "libavutil/mem.h"
  27. #include "libavcodec/avcodec.h"
  28. #include "libavformat/avformat.h"
  29. #include "libavutil/imgutils.h"
  30. #include "libavutil/timestamp.h"
  31. static int video_decode_example(const char *input_filename)
  32. {
  33. const AVCodec *codec = NULL;
  34. AVCodecContext *ctx= NULL;
  35. AVCodecParameters *origin_par = NULL;
  36. AVFrame *fr = NULL;
  37. uint8_t *byte_buffer = NULL;
  38. AVPacket *pkt;
  39. AVFormatContext *fmt_ctx = NULL;
  40. int number_of_written_bytes;
  41. int video_stream;
  42. int byte_buffer_size;
  43. int i = 0;
  44. int result;
  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. pkt = av_packet_alloc();
  87. if (!pkt) {
  88. av_log(NULL, AV_LOG_ERROR, "Cannot allocate packet\n");
  89. return AVERROR(ENOMEM);
  90. }
  91. byte_buffer_size = av_image_get_buffer_size(ctx->pix_fmt, ctx->width, ctx->height, 16);
  92. byte_buffer = av_malloc(byte_buffer_size);
  93. if (!byte_buffer) {
  94. av_log(NULL, AV_LOG_ERROR, "Can't allocate buffer\n");
  95. return AVERROR(ENOMEM);
  96. }
  97. printf("#tb %d: %d/%d\n", video_stream, fmt_ctx->streams[video_stream]->time_base.num, fmt_ctx->streams[video_stream]->time_base.den);
  98. i = 0;
  99. result = 0;
  100. while (result >= 0) {
  101. result = av_read_frame(fmt_ctx, pkt);
  102. if (result >= 0 && pkt->stream_index != video_stream) {
  103. av_packet_unref(pkt);
  104. continue;
  105. }
  106. if (result < 0)
  107. result = avcodec_send_packet(ctx, NULL);
  108. else {
  109. if (pkt->pts == AV_NOPTS_VALUE)
  110. pkt->pts = pkt->dts = i;
  111. result = avcodec_send_packet(ctx, pkt);
  112. }
  113. av_packet_unref(pkt);
  114. if (result < 0) {
  115. av_log(NULL, AV_LOG_ERROR, "Error submitting a packet for decoding\n");
  116. return result;
  117. }
  118. while (result >= 0) {
  119. result = avcodec_receive_frame(ctx, fr);
  120. if (result == AVERROR_EOF)
  121. goto finish;
  122. else if (result == AVERROR(EAGAIN)) {
  123. result = 0;
  124. break;
  125. } else if (result < 0) {
  126. av_log(NULL, AV_LOG_ERROR, "Error decoding frame\n");
  127. return result;
  128. }
  129. number_of_written_bytes = av_image_copy_to_buffer(byte_buffer, byte_buffer_size,
  130. (const uint8_t* const *)fr->data, (const int*) fr->linesize,
  131. ctx->pix_fmt, ctx->width, ctx->height, 1);
  132. if (number_of_written_bytes < 0) {
  133. av_log(NULL, AV_LOG_ERROR, "Can't copy image to buffer\n");
  134. av_frame_unref(fr);
  135. return number_of_written_bytes;
  136. }
  137. printf("%d, %s, %s, %8"PRId64", %8d, 0x%08"PRIx32"\n", video_stream,
  138. av_ts2str(fr->pts), av_ts2str(fr->pkt_dts), fr->duration,
  139. number_of_written_bytes, av_adler32_update(0, (const uint8_t*)byte_buffer, number_of_written_bytes));
  140. av_frame_unref(fr);
  141. }
  142. i++;
  143. }
  144. finish:
  145. av_packet_free(&pkt);
  146. av_frame_free(&fr);
  147. avformat_close_input(&fmt_ctx);
  148. avcodec_free_context(&ctx);
  149. av_freep(&byte_buffer);
  150. return 0;
  151. }
  152. int main(int argc, char **argv)
  153. {
  154. if (argc < 2)
  155. {
  156. av_log(NULL, AV_LOG_ERROR, "Incorrect input\n");
  157. return 1;
  158. }
  159. if (video_decode_example(argv[1]) != 0)
  160. return 1;
  161. return 0;
  162. }