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.

122 lines
3.7KB

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