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.

184 lines
6.3KB

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