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.

96 lines
2.9KB

  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. #include "libavutil/imgutils.h"
  19. #include "lavfutils.h"
  20. int ff_load_image(uint8_t *data[4], int linesize[4],
  21. int *w, int *h, enum PixelFormat *pix_fmt,
  22. const char *filename, void *log_ctx)
  23. {
  24. AVInputFormat *iformat = NULL;
  25. AVFormatContext *format_ctx;
  26. AVCodec *codec;
  27. AVCodecContext *codec_ctx;
  28. AVFrame *frame;
  29. int frame_decoded, ret = 0;
  30. AVPacket pkt;
  31. av_register_all();
  32. iformat = av_find_input_format("image2");
  33. if ((ret = avformat_open_input(&format_ctx, filename, iformat, NULL)) < 0) {
  34. av_log(log_ctx, AV_LOG_ERROR,
  35. "Failed to open input file '%s'\n", filename);
  36. return ret;
  37. }
  38. codec_ctx = format_ctx->streams[0]->codec;
  39. codec = avcodec_find_decoder(codec_ctx->codec_id);
  40. if (!codec) {
  41. av_log(log_ctx, AV_LOG_ERROR, "Failed to find codec\n");
  42. ret = AVERROR(EINVAL);
  43. goto end;
  44. }
  45. if ((ret = avcodec_open2(codec_ctx, codec, NULL)) < 0) {
  46. av_log(log_ctx, AV_LOG_ERROR, "Failed to open codec\n");
  47. goto end;
  48. }
  49. if (!(frame = avcodec_alloc_frame()) ) {
  50. av_log(log_ctx, AV_LOG_ERROR, "Failed to alloc frame\n");
  51. ret = AVERROR(ENOMEM);
  52. goto end;
  53. }
  54. ret = av_read_frame(format_ctx, &pkt);
  55. if (ret < 0) {
  56. av_log(log_ctx, AV_LOG_ERROR, "Failed to read frame from file\n");
  57. goto end;
  58. }
  59. ret = avcodec_decode_video2(codec_ctx, frame, &frame_decoded, &pkt);
  60. if (ret < 0 || !frame_decoded) {
  61. av_log(log_ctx, AV_LOG_ERROR, "Failed to decode image from file\n");
  62. goto end;
  63. }
  64. ret = 0;
  65. *w = frame->width;
  66. *h = frame->height;
  67. *pix_fmt = frame->format;
  68. if ((ret = av_image_alloc(data, linesize, *w, *h, *pix_fmt, 16)) < 0)
  69. goto end;
  70. ret = 0;
  71. av_image_copy(data, linesize, frame->data, frame->linesize, *pix_fmt, *w, *h);
  72. end:
  73. if (codec_ctx)
  74. avcodec_close(codec_ctx);
  75. if (format_ctx)
  76. avformat_close_input(&format_ctx);
  77. av_freep(&frame);
  78. if (ret < 0)
  79. av_log(log_ctx, AV_LOG_ERROR, "Error loading image file '%s'\n", filename);
  80. return ret;
  81. }