decode_simple.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. /* shared code for simple demux/decode tools */
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include "decode_simple.h"
  22. #include "libavformat/avformat.h"
  23. #include "libavcodec/avcodec.h"
  24. #include "libavcodec/packet.h"
  25. #include "libavutil/dict.h"
  26. #include "libavutil/error.h"
  27. #include "libavutil/frame.h"
  28. static int decode_read(DecodeContext *dc, int flush)
  29. {
  30. const int ret_done = flush ? AVERROR_EOF : AVERROR(EAGAIN);
  31. int ret = 0;
  32. while (ret >= 0 &&
  33. (dc->max_frames == 0 || dc->decoder->frame_num < dc->max_frames)) {
  34. ret = avcodec_receive_frame(dc->decoder, dc->frame);
  35. if (ret < 0) {
  36. if (ret == AVERROR_EOF) {
  37. int err = dc->process_frame(dc, NULL);
  38. if (err < 0)
  39. return err;
  40. }
  41. return (ret == ret_done) ? 0 : ret;
  42. }
  43. ret = dc->process_frame(dc, dc->frame);
  44. av_frame_unref(dc->frame);
  45. if (ret < 0)
  46. return ret;
  47. if (dc->max_frames && dc->decoder->frame_num == dc->max_frames)
  48. return 1;
  49. }
  50. return (dc->max_frames == 0 || dc->decoder->frame_num < dc->max_frames) ? 0 : 1;
  51. }
  52. int ds_run(DecodeContext *dc)
  53. {
  54. int ret;
  55. ret = avcodec_open2(dc->decoder, NULL, &dc->decoder_opts);
  56. if (ret < 0)
  57. return ret;
  58. while (ret >= 0) {
  59. ret = av_read_frame(dc->demuxer, dc->pkt);
  60. if (ret < 0)
  61. break;
  62. if (dc->pkt->stream_index != dc->stream->index) {
  63. av_packet_unref(dc->pkt);
  64. continue;
  65. }
  66. ret = avcodec_send_packet(dc->decoder, dc->pkt);
  67. if (ret < 0) {
  68. fprintf(stderr, "Error decoding: %d\n", ret);
  69. return ret;
  70. }
  71. av_packet_unref(dc->pkt);
  72. ret = decode_read(dc, 0);
  73. if (ret < 0) {
  74. fprintf(stderr, "Error decoding: %d\n", ret);
  75. return ret;
  76. } else if (ret > 0)
  77. goto finish;
  78. }
  79. ret = avcodec_send_packet(dc->decoder, NULL);
  80. if (ret >= 0)
  81. ret = decode_read(dc, 1);
  82. if (ret < 0) {
  83. fprintf(stderr, "Error flushing: %d\n", ret);
  84. return ret;
  85. }
  86. finish:
  87. return dc->process_frame(dc, NULL);
  88. }
  89. void ds_free(DecodeContext *dc)
  90. {
  91. av_dict_free(&dc->decoder_opts);
  92. av_frame_free(&dc->frame);
  93. av_packet_free(&dc->pkt);
  94. avcodec_free_context(&dc->decoder);
  95. avformat_close_input(&dc->demuxer);
  96. }
  97. int ds_open(DecodeContext *dc, const char *url, int stream_idx)
  98. {
  99. const AVCodec *codec;
  100. int ret;
  101. memset(dc, 0, sizeof(*dc));
  102. dc->pkt = av_packet_alloc();
  103. dc->frame = av_frame_alloc();
  104. if (!dc->pkt || !dc->frame) {
  105. ret = AVERROR(ENOMEM);
  106. goto fail;
  107. }
  108. ret = avformat_open_input(&dc->demuxer, url, NULL, NULL);
  109. if (ret < 0) {
  110. fprintf(stderr, "Error opening input file: %d\n", ret);
  111. return ret;
  112. }
  113. if (stream_idx < 0 || stream_idx >= dc->demuxer->nb_streams)
  114. return AVERROR(EINVAL);
  115. dc->stream = dc->demuxer->streams[stream_idx];
  116. codec = avcodec_find_decoder(dc->stream->codecpar->codec_id);
  117. if (!codec)
  118. return AVERROR_DECODER_NOT_FOUND;
  119. dc->decoder = avcodec_alloc_context3(codec);
  120. if (!dc->decoder)
  121. return AVERROR(ENOMEM);
  122. ret = avcodec_parameters_to_context(dc->decoder, dc->stream->codecpar);
  123. if (ret < 0)
  124. goto fail;
  125. return 0;
  126. fail:
  127. ds_free(dc);
  128. return ret;
  129. }