lavfutils.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright 2012 Stefano Sabatini <stefasab gmail com>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/imgutils.h"
  21. #include "lavfutils.h"
  22. int ff_load_image(uint8_t *data[4], int linesize[4],
  23. int *w, int *h, enum AVPixelFormat *pix_fmt,
  24. const char *filename, void *log_ctx)
  25. {
  26. AVInputFormat *iformat = NULL;
  27. AVFormatContext *format_ctx = NULL;
  28. AVCodec *codec;
  29. AVCodecContext *codec_ctx;
  30. AVFrame *frame;
  31. int frame_decoded, ret = 0;
  32. AVPacket pkt;
  33. av_init_packet(&pkt);
  34. av_register_all();
  35. iformat = av_find_input_format("image2");
  36. if ((ret = avformat_open_input(&format_ctx, filename, iformat, NULL)) < 0) {
  37. av_log(log_ctx, AV_LOG_ERROR,
  38. "Failed to open input file '%s'\n", filename);
  39. return ret;
  40. }
  41. if ((ret = avformat_find_stream_info(format_ctx, NULL)) < 0) {
  42. av_log(log_ctx, AV_LOG_ERROR, "Find stream info failed\n");
  43. return ret;
  44. }
  45. codec_ctx = format_ctx->streams[0]->codec;
  46. codec = avcodec_find_decoder(codec_ctx->codec_id);
  47. if (!codec) {
  48. av_log(log_ctx, AV_LOG_ERROR, "Failed to find codec\n");
  49. ret = AVERROR(EINVAL);
  50. goto end;
  51. }
  52. if ((ret = avcodec_open2(codec_ctx, codec, NULL)) < 0) {
  53. av_log(log_ctx, AV_LOG_ERROR, "Failed to open codec\n");
  54. goto end;
  55. }
  56. if (!(frame = av_frame_alloc()) ) {
  57. av_log(log_ctx, AV_LOG_ERROR, "Failed to alloc frame\n");
  58. ret = AVERROR(ENOMEM);
  59. goto end;
  60. }
  61. ret = av_read_frame(format_ctx, &pkt);
  62. if (ret < 0) {
  63. av_log(log_ctx, AV_LOG_ERROR, "Failed to read frame from file\n");
  64. goto end;
  65. }
  66. ret = avcodec_decode_video2(codec_ctx, frame, &frame_decoded, &pkt);
  67. if (ret < 0 || !frame_decoded) {
  68. av_log(log_ctx, AV_LOG_ERROR, "Failed to decode image from file\n");
  69. if (ret >= 0)
  70. ret = -1;
  71. goto end;
  72. }
  73. *w = frame->width;
  74. *h = frame->height;
  75. *pix_fmt = frame->format;
  76. if ((ret = av_image_alloc(data, linesize, *w, *h, *pix_fmt, 16)) < 0)
  77. goto end;
  78. ret = 0;
  79. av_image_copy(data, linesize, (const uint8_t **)frame->data, frame->linesize, *pix_fmt, *w, *h);
  80. end:
  81. av_free_packet(&pkt);
  82. avcodec_close(codec_ctx);
  83. avformat_close_input(&format_ctx);
  84. av_freep(&frame);
  85. if (ret < 0)
  86. av_log(log_ctx, AV_LOG_ERROR, "Error loading image file '%s'\n", filename);
  87. return ret;
  88. }