资源简介

基于ffmpeg源码中example稍作修改,可以保存完整YUV数据。添加Makefile,便于编译。解码MPEG视频,并将解码后的数据保存成一帧帧的图片。包含演示使用的视频文件。

资源截图

代码片段和文件信息

/*
 * Copyright (c) 2001 Fabrice Bellard
 *
 * Permission is hereby granted free of charge to any person obtaining a copy
 * of this software and associated documentation files (the “Software“) to deal
 * in the Software without restriction including without limitation the rights
 * to use copy modify merge publish distribute sublicense and/or sell
 * copies of the Software and to permit persons to whom the Software is
 * furnished to do so subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED “AS IS“ WITHOUT WARRANTY OF ANY KIND EXPRESS OR
 * IMPLIED INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM DAMAGES OR OTHER
 * LIABILITY WHETHER IN AN ACTION OF CONTRACT TORT OR OTHERWISE ARISING FROM
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

/**
 * @file
 * video decoding with libavcodec API example
 *
 * @example decode_video.c
 */

#include 
#include 
#include 

#include 

#define INBUF_SIZE 4096

static void pgm_save(unsigned char *buf[] int wrap[] int xsize int ysize
                     char *filename)
{
    FILE *f;
    int i;

    f = fopen(filename“w“);
    fprintf(f “P5\n%d %d\n%d\n“ xsize ysize 255);

/* Save Y data */
    for (i = 0; (wrap[0]) && (i < ysize); i++)
        fwrite(buf[0] + i * wrap[0] 1 xsize f);

/* Save U(Cb) data */
    for (i = 0; (wrap[1]) && (i < ysize/2); i++)
        fwrite(buf[1] + i * wrap[1] 1 xsize / 2 f);

/* Save V(Cr) data */
    for (i = 0; (wrap[2]) && (i < ysize/2); i++)
        fwrite(buf[2] + i * wrap[2] 1 xsize / 2 f);

    fclose(f);
}

static void decode(AVCodecContext *dec_ctx AVframe *frame AVPacket *pkt
                   const char *filename)
{
    char buf[1024];
    int ret;

    ret = avcodec_send_packet(dec_ctx pkt);
    if (ret < 0) {
        fprintf(stderr “Error sending a packet for decoding\n“);
        exit(1);
    }

    while (ret >= 0) {
        ret = avcodec_receive_frame(dec_ctx frame);
        if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
            return;
        else if (ret < 0) {
            fprintf(stderr “Error during decoding\n“);
            exit(1);
        }

        printf(“saving frame %3d\n“ dec_ctx->frame_number);
        fflush(stdout);

printf(“+++++ FLC-DBG: format:%d linesize[0]:%d linesize[1]:%d linesize[2]:%d +++++\n\n“
frame->format frame->linesize[0] frame->linesize[1] frame->linesize[2]);

        /* the picture is allocated by the decoder. no need to
           free it */
        snprintf(buf sizeof(buf) “%s-%d“ filename dec_ctx->frame_number);
        pgm_save(frame->data frame->linesize
   

评论

共有 条评论