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