lavfutils.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. codec_ctx = format_ctx->streams[0]->codec;
  42. codec = avcodec_find_decoder(codec_ctx->codec_id);
  43. if (!codec) {
  44. av_log(log_ctx, AV_LOG_ERROR, "Failed to find codec\n");
  45. ret = AVERROR(EINVAL);
  46. goto end;
  47. }
  48. if ((ret = avcodec_open2(codec_ctx, codec, NULL)) < 0) {
  49. av_log(log_ctx, AV_LOG_ERROR, "Failed to open codec\n");
  50. goto end;
  51. }
  52. if (!(frame = av_frame_alloc()) ) {
  53. av_log(log_ctx, AV_LOG_ERROR, "Failed to alloc frame\n");
  54. ret = AVERROR(ENOMEM);
  55. goto end;
  56. }
  57. ret = av_read_frame(format_ctx, &pkt);
  58. if (ret < 0) {
  59. av_log(log_ctx, AV_LOG_ERROR, "Failed to read frame from file\n");
  60. goto end;
  61. }
  62. ret = avcodec_decode_video2(codec_ctx, frame, &frame_decoded, &pkt);
  63. if (ret < 0 || !frame_decoded) {
  64. av_log(log_ctx, AV_LOG_ERROR, "Failed to decode image from file\n");
  65. goto end;
  66. }
  67. ret = 0;
  68. *w = frame->width;
  69. *h = frame->height;
  70. *pix_fmt = frame->format;
  71. if ((ret = av_image_alloc(data, linesize, *w, *h, *pix_fmt, 16)) < 0)
  72. goto end;
  73. ret = 0;
  74. av_image_copy(data, linesize, (const uint8_t **)frame->data, frame->linesize, *pix_fmt, *w, *h);
  75. end:
  76. av_free_packet(&pkt);
  77. avcodec_close(codec_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. }