| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | |||
| 21 | #include <stdint.h> | ||
| 22 | |||
| 23 | #include "libavutil/frame.h" | ||
| 24 | #include "libavutil/log.h" | ||
| 25 | |||
| 26 | #include "libavformat/avformat.h" | ||
| 27 | |||
| 28 | #include "libavcodec/avcodec.h" | ||
| 29 | |||
| 30 | #include "lavfutils.h" | ||
| 31 | |||
| 32 | ✗ | int ff_load_image(struct AVFrame **outframe, | |
| 33 | const char *filename, void *log_ctx) | ||
| 34 | { | ||
| 35 | ✗ | const AVInputFormat *iformat = NULL; | |
| 36 | ✗ | AVFormatContext *format_ctx = NULL; | |
| 37 | const AVCodec *codec; | ||
| 38 | ✗ | AVCodecContext *codec_ctx = NULL; | |
| 39 | AVCodecParameters *par; | ||
| 40 | ✗ | AVFrame *frame = NULL; | |
| 41 | ✗ | int ret = 0; | |
| 42 | AVPacket pkt; | ||
| 43 | |||
| 44 | ✗ | *outframe = NULL; | |
| 45 | |||
| 46 | ✗ | iformat = av_find_input_format("image2pipe"); | |
| 47 | ✗ | if ((ret = avformat_open_input(&format_ctx, filename, iformat, NULL)) < 0) { | |
| 48 | ✗ | av_log(log_ctx, AV_LOG_ERROR, | |
| 49 | "Failed to open input file '%s'\n", filename); | ||
| 50 | ✗ | return ret; | |
| 51 | } | ||
| 52 | |||
| 53 | ✗ | if ((ret = avformat_find_stream_info(format_ctx, NULL)) < 0) { | |
| 54 | ✗ | av_log(log_ctx, AV_LOG_ERROR, "Find stream info failed\n"); | |
| 55 | ✗ | goto end; | |
| 56 | } | ||
| 57 | |||
| 58 | ✗ | par = format_ctx->streams[0]->codecpar; | |
| 59 | ✗ | codec = avcodec_find_decoder(par->codec_id); | |
| 60 | ✗ | if (!codec) { | |
| 61 | ✗ | av_log(log_ctx, AV_LOG_ERROR, "Failed to find codec\n"); | |
| 62 | ✗ | ret = AVERROR(EINVAL); | |
| 63 | ✗ | goto end; | |
| 64 | } | ||
| 65 | |||
| 66 | ✗ | codec_ctx = avcodec_alloc_context3(codec); | |
| 67 | ✗ | if (!codec_ctx) { | |
| 68 | ✗ | av_log(log_ctx, AV_LOG_ERROR, "Failed to alloc video decoder context\n"); | |
| 69 | ✗ | ret = AVERROR(ENOMEM); | |
| 70 | ✗ | goto end; | |
| 71 | } | ||
| 72 | |||
| 73 | ✗ | ret = avcodec_parameters_to_context(codec_ctx, par); | |
| 74 | ✗ | if (ret < 0) { | |
| 75 | ✗ | av_log(log_ctx, AV_LOG_ERROR, "Failed to copy codec parameters to decoder context\n"); | |
| 76 | ✗ | goto end; | |
| 77 | } | ||
| 78 | |||
| 79 | ✗ | codec_ctx->thread_type = FF_THREAD_SLICE; | |
| 80 | ✗ | if ((ret = avcodec_open2(codec_ctx, codec, NULL)) < 0) { | |
| 81 | ✗ | av_log(log_ctx, AV_LOG_ERROR, "Failed to open codec\n"); | |
| 82 | ✗ | goto end; | |
| 83 | } | ||
| 84 | |||
| 85 | ✗ | ret = av_read_frame(format_ctx, &pkt); | |
| 86 | ✗ | if (ret < 0) { | |
| 87 | ✗ | av_log(log_ctx, AV_LOG_ERROR, "Failed to read frame from file\n"); | |
| 88 | ✗ | goto end; | |
| 89 | } | ||
| 90 | |||
| 91 | ✗ | ret = avcodec_send_packet(codec_ctx, &pkt); | |
| 92 | ✗ | av_packet_unref(&pkt); | |
| 93 | ✗ | if (ret < 0) { | |
| 94 | ✗ | av_log(log_ctx, AV_LOG_ERROR, "Error submitting a packet to decoder\n"); | |
| 95 | ✗ | goto end; | |
| 96 | } | ||
| 97 | |||
| 98 | ✗ | if (!(frame = av_frame_alloc()) ) { | |
| 99 | ✗ | av_log(log_ctx, AV_LOG_ERROR, "Failed to alloc frame\n"); | |
| 100 | ✗ | ret = AVERROR(ENOMEM); | |
| 101 | ✗ | goto end; | |
| 102 | } | ||
| 103 | |||
| 104 | ✗ | ret = avcodec_receive_frame(codec_ctx, frame); | |
| 105 | ✗ | if (ret < 0) { | |
| 106 | ✗ | av_frame_free(&frame); | |
| 107 | ✗ | av_log(log_ctx, AV_LOG_ERROR, "Failed to decode image from file\n"); | |
| 108 | ✗ | goto end; | |
| 109 | } | ||
| 110 | ✗ | *outframe = frame; | |
| 111 | |||
| 112 | ✗ | end: | |
| 113 | ✗ | avcodec_free_context(&codec_ctx); | |
| 114 | ✗ | avformat_close_input(&format_ctx); | |
| 115 | |||
| 116 | ✗ | if (ret < 0) | |
| 117 | ✗ | av_log(log_ctx, AV_LOG_ERROR, "Error loading image file '%s'\n", filename); | |
| 118 | ✗ | return ret; | |
| 119 | } | ||
| 120 |