lavfutils.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 PixelFormat *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_register_all();
  34. iformat = av_find_input_format("image2");
  35. if ((ret = avformat_open_input(&format_ctx, filename, iformat, NULL)) < 0) {
  36. av_log(log_ctx, AV_LOG_ERROR,
  37. "Failed to open input file '%s'\n", filename);
  38. return ret;
  39. }
  40. codec_ctx = format_ctx->streams[0]->codec;
  41. codec = avcodec_find_decoder(codec_ctx->codec_id);
  42. if (!codec) {
  43. av_log(log_ctx, AV_LOG_ERROR, "Failed to find codec\n");
  44. ret = AVERROR(EINVAL);
  45. goto end;
  46. }
  47. if ((ret = avcodec_open2(codec_ctx, codec, NULL)) < 0) {
  48. av_log(log_ctx, AV_LOG_ERROR, "Failed to open codec\n");
  49. goto end;
  50. }
  51. if (!(frame = avcodec_alloc_frame()) ) {
  52. av_log(log_ctx, AV_LOG_ERROR, "Failed to alloc frame\n");
  53. ret = AVERROR(ENOMEM);
  54. goto end;
  55. }
  56. ret = av_read_frame(format_ctx, &pkt);
  57. if (ret < 0) {
  58. av_log(log_ctx, AV_LOG_ERROR, "Failed to read frame from file\n");
  59. goto end;
  60. }
  61. ret = avcodec_decode_video2(codec_ctx, frame, &frame_decoded, &pkt);
  62. if (ret < 0 || !frame_decoded) {
  63. av_log(log_ctx, AV_LOG_ERROR, "Failed to decode image from file\n");
  64. goto end;
  65. }
  66. ret = 0;
  67. *w = frame->width;
  68. *h = frame->height;
  69. *pix_fmt = frame->format;
  70. if ((ret = av_image_alloc(data, linesize, *w, *h, *pix_fmt, 16)) < 0)
  71. goto end;
  72. ret = 0;
  73. av_image_copy(data, linesize, frame->data, frame->linesize, *pix_fmt, *w, *h);
  74. end:
  75. if (codec_ctx)
  76. avcodec_close(codec_ctx);
  77. if (format_ctx)
  78. avformat_close_input(&format_ctx);
  79. av_freep(&frame);
  80. if (ret < 0)
  81. av_log(log_ctx, AV_LOG_ERROR, "Error loading image file '%s'\n", filename);
  82. return ret;
  83. }