You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
5.4KB

  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 *origin_ctx = NULL, *ctx= NULL;
  33. AVFrame *fr = NULL;
  34. uint8_t *byte_buffer = NULL;
  35. AVPacket pkt;
  36. AVFormatContext *fmt_ctx = NULL;
  37. int number_of_written_bytes;
  38. int video_stream;
  39. int got_frame = 0;
  40. int byte_buffer_size;
  41. int i = 0;
  42. int result;
  43. int end_of_stream = 0;
  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_ctx = fmt_ctx->streams[video_stream]->codec;
  60. codec = avcodec_find_decoder(origin_ctx->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_copy_context(ctx, origin_ctx);
  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. byte_buffer_size = av_image_get_buffer_size(ctx->pix_fmt, ctx->width, ctx->height, 16);
  86. byte_buffer = av_malloc(byte_buffer_size);
  87. if (!byte_buffer) {
  88. av_log(NULL, AV_LOG_ERROR, "Can't allocate buffer\n");
  89. return AVERROR(ENOMEM);
  90. }
  91. printf("#tb %d: %d/%d\n", video_stream, fmt_ctx->streams[video_stream]->time_base.num, fmt_ctx->streams[video_stream]->time_base.den);
  92. i = 0;
  93. av_init_packet(&pkt);
  94. do {
  95. if (!end_of_stream)
  96. if (av_read_frame(fmt_ctx, &pkt) < 0)
  97. end_of_stream = 1;
  98. if (end_of_stream) {
  99. pkt.data = NULL;
  100. pkt.size = 0;
  101. }
  102. if (pkt.stream_index == video_stream || end_of_stream) {
  103. got_frame = 0;
  104. if (pkt.pts == AV_NOPTS_VALUE)
  105. pkt.pts = pkt.dts = i;
  106. result = avcodec_decode_video2(ctx, fr, &got_frame, &pkt);
  107. if (result < 0) {
  108. av_log(NULL, AV_LOG_ERROR, "Error decoding frame\n");
  109. return result;
  110. }
  111. if (got_frame) {
  112. number_of_written_bytes = av_image_copy_to_buffer(byte_buffer, byte_buffer_size,
  113. (const uint8_t* const *)fr->data, (const int*) fr->linesize,
  114. ctx->pix_fmt, ctx->width, ctx->height, 1);
  115. if (number_of_written_bytes < 0) {
  116. av_log(NULL, AV_LOG_ERROR, "Can't copy image to buffer\n");
  117. return number_of_written_bytes;
  118. }
  119. printf("%d, %10"PRId64", %10"PRId64", %8"PRId64", %8d, 0x%08lx\n", video_stream,
  120. fr->pkt_pts, fr->pkt_dts, av_frame_get_pkt_duration(fr),
  121. number_of_written_bytes, av_adler32_update(0, (const uint8_t*)byte_buffer, number_of_written_bytes));
  122. }
  123. av_free_packet(&pkt);
  124. av_init_packet(&pkt);
  125. }
  126. i++;
  127. } while (!end_of_stream || got_frame);
  128. av_free_packet(&pkt);
  129. av_frame_free(&fr);
  130. avcodec_close(ctx);
  131. avformat_close_input(&fmt_ctx);
  132. avcodec_free_context(&ctx);
  133. av_freep(&byte_buffer);
  134. return 0;
  135. }
  136. int main(int argc, char **argv)
  137. {
  138. if (argc < 2)
  139. {
  140. av_log(NULL, AV_LOG_ERROR, "Incorrect input\n");
  141. return 1;
  142. }
  143. av_register_all();
  144. if (video_decode_example(argv[1]) != 0)
  145. return 1;
  146. return 0;
  147. }