lavfutils.c 3.7 KB

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