annotate ffmpeg/libavfilter/lavfutils.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents 6840f77b83aa
children
rev   line source
yading@10 1 /*
yading@10 2 * Copyright 2012 Stefano Sabatini <stefasab gmail com>
yading@10 3 *
yading@10 4 * This file is part of FFmpeg.
yading@10 5 *
yading@10 6 * FFmpeg is free software; you can redistribute it and/or
yading@10 7 * modify it under the terms of the GNU Lesser General Public
yading@10 8 * License as published by the Free Software Foundation; either
yading@10 9 * version 2.1 of the License, or (at your option) any later version.
yading@10 10 *
yading@10 11 * FFmpeg is distributed in the hope that it will be useful,
yading@10 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 14 * Lesser General Public License for more details.
yading@10 15 *
yading@10 16 * You should have received a copy of the GNU Lesser General Public
yading@10 17 * License along with FFmpeg; if not, write to the Free Software
yading@10 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 19 */
yading@10 20
yading@10 21 #include "libavutil/imgutils.h"
yading@10 22 #include "lavfutils.h"
yading@10 23
yading@10 24 int ff_load_image(uint8_t *data[4], int linesize[4],
yading@10 25 int *w, int *h, enum AVPixelFormat *pix_fmt,
yading@10 26 const char *filename, void *log_ctx)
yading@10 27 {
yading@10 28 AVInputFormat *iformat = NULL;
yading@10 29 AVFormatContext *format_ctx = NULL;
yading@10 30 AVCodec *codec;
yading@10 31 AVCodecContext *codec_ctx;
yading@10 32 AVFrame *frame;
yading@10 33 int frame_decoded, ret = 0;
yading@10 34 AVPacket pkt;
yading@10 35
yading@10 36 av_register_all();
yading@10 37
yading@10 38 iformat = av_find_input_format("image2");
yading@10 39 if ((ret = avformat_open_input(&format_ctx, filename, iformat, NULL)) < 0) {
yading@10 40 av_log(log_ctx, AV_LOG_ERROR,
yading@10 41 "Failed to open input file '%s'\n", filename);
yading@10 42 return ret;
yading@10 43 }
yading@10 44
yading@10 45 codec_ctx = format_ctx->streams[0]->codec;
yading@10 46 codec = avcodec_find_decoder(codec_ctx->codec_id);
yading@10 47 if (!codec) {
yading@10 48 av_log(log_ctx, AV_LOG_ERROR, "Failed to find codec\n");
yading@10 49 ret = AVERROR(EINVAL);
yading@10 50 goto end;
yading@10 51 }
yading@10 52
yading@10 53 if ((ret = avcodec_open2(codec_ctx, codec, NULL)) < 0) {
yading@10 54 av_log(log_ctx, AV_LOG_ERROR, "Failed to open codec\n");
yading@10 55 goto end;
yading@10 56 }
yading@10 57
yading@10 58 if (!(frame = avcodec_alloc_frame()) ) {
yading@10 59 av_log(log_ctx, AV_LOG_ERROR, "Failed to alloc frame\n");
yading@10 60 ret = AVERROR(ENOMEM);
yading@10 61 goto end;
yading@10 62 }
yading@10 63
yading@10 64 ret = av_read_frame(format_ctx, &pkt);
yading@10 65 if (ret < 0) {
yading@10 66 av_log(log_ctx, AV_LOG_ERROR, "Failed to read frame from file\n");
yading@10 67 goto end;
yading@10 68 }
yading@10 69
yading@10 70 ret = avcodec_decode_video2(codec_ctx, frame, &frame_decoded, &pkt);
yading@10 71 if (ret < 0 || !frame_decoded) {
yading@10 72 av_log(log_ctx, AV_LOG_ERROR, "Failed to decode image from file\n");
yading@10 73 goto end;
yading@10 74 }
yading@10 75 ret = 0;
yading@10 76
yading@10 77 *w = frame->width;
yading@10 78 *h = frame->height;
yading@10 79 *pix_fmt = frame->format;
yading@10 80
yading@10 81 if ((ret = av_image_alloc(data, linesize, *w, *h, *pix_fmt, 16)) < 0)
yading@10 82 goto end;
yading@10 83 ret = 0;
yading@10 84
yading@10 85 av_image_copy(data, linesize, (const uint8_t **)frame->data, frame->linesize, *pix_fmt, *w, *h);
yading@10 86
yading@10 87 end:
yading@10 88 avcodec_close(codec_ctx);
yading@10 89 avformat_close_input(&format_ctx);
yading@10 90 av_freep(&frame);
yading@10 91
yading@10 92 if (ret < 0)
yading@10 93 av_log(log_ctx, AV_LOG_ERROR, "Error loading image file '%s'\n", filename);
yading@10 94 return ret;
yading@10 95 }