decode_simple.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. goto flush;
  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. return 0;
  78. }
  79. flush:
  80. avcodec_send_packet(dc->decoder, NULL);
  81. ret = decode_read(dc, 1);
  82. if (ret < 0) {
  83. fprintf(stderr, "Error flushing: %d\n", ret);
  84. return ret;
  85. }
  86. return 0;
  87. }
  88. void ds_free(DecodeContext *dc)
  89. {
  90. av_dict_free(&dc->decoder_opts);
  91. av_frame_free(&dc->frame);
  92. av_packet_free(&dc->pkt);
  93. avcodec_free_context(&dc->decoder);
  94. avformat_close_input(&dc->demuxer);
  95. }
  96. int ds_open(DecodeContext *dc, const char *url, int stream_idx)
  97. {
  98. const AVCodec *codec;
  99. int ret;
  100. memset(dc, 0, sizeof(*dc));
  101. dc->pkt = av_packet_alloc();
  102. dc->frame = av_frame_alloc();
  103. if (!dc->pkt || !dc->frame) {
  104. ret = AVERROR(ENOMEM);
  105. goto fail;
  106. }
  107. ret = avformat_open_input(&dc->demuxer, url, NULL, NULL);
  108. if (ret < 0) {
  109. fprintf(stderr, "Error opening input file: %d\n", ret);
  110. return ret;
  111. }
  112. if (stream_idx < 0 || stream_idx >= dc->demuxer->nb_streams)
  113. return AVERROR(EINVAL);
  114. dc->stream = dc->demuxer->streams[stream_idx];
  115. codec = avcodec_find_decoder(dc->stream->codecpar->codec_id);
  116. if (!codec)
  117. return AVERROR_DECODER_NOT_FOUND;
  118. dc->decoder = avcodec_alloc_context3(codec);
  119. if (!dc->decoder)
  120. return AVERROR(ENOMEM);
  121. return 0;
  122. fail:
  123. ds_free(dc);
  124. return ret;
  125. }