LCOV - code coverage report
Current view: top level - libavfilter - lavfutils.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 63 0.0 %
Date: 2017-12-17 16:07:53 Functions: 0 1 0.0 %

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

Generated by: LCOV version 1.13