lavfutils.c 3.3 KB

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