FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/lavfutils.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 0 65 0.0%
Functions: 0 1 0.0%
Branches: 0 24 0.0%

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