FFmpeg SDK开发模型之一:解码器

简介

本例讲解了如何使用ffmpeg SDK解码媒体文件;

参考源码是ffmpeg 自带的apiexample.c


一、源代码
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

#ifdef HAVE_AV_CONFIG_H
#undef HAVE_AV_CONFIG_H
#endif

#include "libavcodec/avcodec.h"
#define INBUF_SIZE 4096

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);
  for(i=0;i<ysize;i++)
  fwrite(buf + i * wrap,1,xsize,f);
  fclose(f);
}

/*
 * Video decoding
 */
void video_decode_example(const char *outfilename, const char *filename)
{
  AVCodec *codec;
  AVCodecContext *c= NULL;
  int frame, size, got_picture, len;
  FILE *f;
  AVFrame *picture;
  uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE], *inbuf_ptr;
  char buf[1024];

  uint8_t *pkt_ptr, pkt_len;

  /* set end of buffer to 0 (this ensures that no overreading happens for damaged mpeg streams) */
  memset(inbuf + INBUF_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);

  printf("Video decoding\n");

  /* find the mpeg1 video decoder */
  //codec = avcodec_find_decoder(CODEC_ID_H264);
  codec = avcodec_find_decoder(CODEC_ID_MPEG2VIDEO);
  if (!codec) {
    fprintf(stderr, "codec not found\n");
    exit(1);
  }

  c= avcodec_alloc_context();
  picture= avcodec_alloc_frame();

  if(codec->capabilities&CODEC_CAP_TRUNCATED)
  c->flags|= CODEC_FLAG_TRUNCATED; /* we dont send complete frames */

  /* for some codecs, such as msmpeg4 and mpeg4, width and height
     MUST be initialized there because these info are not available
     in the bitstream */

  /* open it */
  if (avcodec_open(c, codec) < 0) {
    fprintf(stderr, "could not open codec\n");
    exit(1);
  }

  /* the codec gives us the frame size, in samples */
  f = fopen(filename, "rb");
  if (!f) {
    fprintf(stderr, "could not open %s\n", filename);
    exit(1);
  }
  frame = 0;
  for(;;) {
    AVPacket pkt;

    if (av_read_frame(c, &pkt) < 0) {
      continue;
    }

    //        size = fread(inbuf, 1, INBUF_SIZE, f);
    //        if (size == 0)
    //            break;

    /* NOTE1: some codecs are stream based (mpegvideo, mpegaudio)
       and this is the only method to use them because you cannot
       know the compressed data size before analysing it.

       BUT some other codecs (msmpeg4, mpeg4) are inherently frame
       based, so you must call them with all the data for one
       frame exactly. You must also initialize 'width' and
       'height' before initializing them. */

    /* NOTE2: some codecs allow the raw parameters (frame size,
       sample rate) to be changed at any frame. We handle this, so
       you should also take care of it */

    /* here, we use a stream based decoder (mpeg1video), so we
       feed decoder and see if it could decode a frame */
    pkt_len = pkt.size;
    pkt_ptr = pkt.data;

    //  inbuf_ptr = inbuf;
    while (pkt_len > 0) {
      len = avcodec_decode_video2(c, picture, &got_picture,
            &pkt);
      if (len < 0) {
        fprintf(stderr, "Error while decoding frame %d\n", frame);
        exit(1);
      }
      if (got_picture) {
        printf("saving frame %3d\n", frame);
        fflush(stdout);

        /* the picture is allocated by the decoder. no need to
           free it */
        snprintf(buf, sizeof(buf), outfilename, frame);
        pgm_save(picture->data[0], picture->linesize[0],
              c->width, c->height, buf);
        frame++;
      }
      size -= len;
      inbuf_ptr += len;
    }
    av_free_packet(&pkt);
  }

  /* some codecs, such as MPEG, transmit the I and P frame with a
     latency of one frame. You must do the following to have a
     chance to get the last frame of the video */
  len = avcodec_decode_video2(c, picture, &got_picture,
        NULL);
  if (got_picture) {
    printf("saving last frame %3d\n", frame);
    fflush(stdout);

    /* the picture is allocated by the decoder. no need to
       free it */
    snprintf(buf, sizeof(buf), outfilename, frame);
    pgm_save(picture->data[0], picture->linesize[0],
          c->width, c->height, buf);
    frame++;
  }

  fclose(f);

  avcodec_close(c);
  av_free(c);
  av_free(picture);
  printf("\n");
}

int main(int argc, char **argv)
{
  const char *filename;

  /* must be called before using avcodec lib */
  avcodec_init();

  /* register all the codecs (you can also register only the codec
     you wish to have smaller code */
  avcodec_register_all();

  if (argc <= 1) {
    //        audio_encode_example("/tmp/test.mp2");
    //        audio_decode_example("/tmp/test.sw", "/tmp/test.mp2");

    video_encode_example("/tmp/test.mpg");
    filename = "/tmp/test.mpg";
  } else {
    filename = argv[1];
  }

  //    audio_decode_example("/tmp/test.sw", filename);
  video_decode_example("/tmp/test%d.pgm", filename);

  return 0;
}

二、代码分析
01  avcodec_init(), 初始化libavcodec
02  avcodec_register_all(), 注册所有编解码器和格式

03  codec = avcodec_find_decoder(CODEC_ID_MPEG2VIDEO), 查找mpeg2 video解码器
04  c     = avcodec_alloc_context(), 分配AVCodecContext空间并初始化
05  picture = avcodec_alloc_frame(), 分配AVFrame的空间
06  avcodec_open(c, codec),  使用codec来初始化c

07  FOR循环 {
      07.1 av_read_frame(c, &pkt), 从输入文件中读取一个包
      07.2 pkt_len = pkt.size;
           pkt_ptr = pkt.data;
      07.3 while(pkt_len > 0) {
             07.3.1 avcodec_decode_video2(c, picture, &got_picture, &pkt), 解码当前包
             07.3.2 if (got_picture) {
                       pgm_save(picture->data[0], ...), 对解码后的YUV数据进行处理;
                       frame++;
                    }
             07.3.3 size -= len;
           }
      07.4 av_free_packet(&pkt), 释放当前包
    }
08  len = avcodec_decode_video2(c, picture, &got_picture, NULL); 解码最后的帧;
09  if (got_picture) { ...}, 处理最后帧的YUV数据

10  avcodec_close(c);
11  av_free(c)
12  av_free(picture);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: FFmpeg是一款功能强大的开源多媒体处理库,能够处理音频和视频的编码、解码、转码等操作。FFmpeg SDK开发手册是一本介绍如何使用FFmpeg库进行开发的技术指南,可以帮助开发人员了解和掌握FFmpeg的基本原理和使用方法。 FFmpeg SDK开发手册的内容涵盖了FFmpeg库的安装和配置、基本编程接口的使用、音视频处理流程等方面。手册会详细介绍FFmpeg的各个模块和功能,包括音视频封装和解封装、音视频编解码、滤镜和特效处理、媒体的剪辑和合并等操作。通过学习手册,开发人员可以了解FFmpeg库的内部实现原理,掌握相关API的使用方法,并能够根据需要进行二次开发和定制。 手册还包含了大量的代码示例和实践案例,可以帮助开发人员更好地理解和应用所学知识。通过实际操作和编程练习,开发人员可以掌握FFmpeg库的使用技巧和注意事项,提高开发效率和质量。 总之,FFmpeg SDK开发手册是一本对于使用FFmpeg进行音视频处理开发开发人员来说非常有价值的参考书,通过学习手册可以更好地理解和掌握FFmpeg库的功能和使用方法,从而实现各种音视频的处理需求。 ### 回答2: ffmpeg是一个开源的跨平台音视频处理工具,由于其功能强大且免费,被广泛应用于音视频处理领域。ffmpeg提供了丰富的命令行工具,可以对音视频文件进行编码、解码、转换、剪辑等各种操作。 除了命令行工具,ffmpeg还提供了一套开发库,称为ffmpeg sdkffmpeg sdk提供了丰富的函数和接口,便于开发者使用ffmpeg的核心功能。 对于开发者而言,ffmpeg sdk开发手册是非常重要的参考资料。这本手册详细介绍了ffmpeg sdk的各个模块、函数、数据结构以及使用方法,并给出了一些示例代码。通过学习手册,开发者可以了解ffmpeg sdk的整体架构,掌握基本的使用方法和技巧。 在手册中,首先介绍了ffmpeg的安装和环境配置,包括如何下载、编译和安装ffmpeg sdk,并提供了相关工具和库的参考链接。然后,手册逐一介绍了ffmpeg sdk的各个模块,如音频处理模块、视频处理模块、封装格式处理模块等,并详细讲解了各个函数和接口的使用方法和参数含义。 此外,手册还提供了一些常见问题的解答和技巧,帮助开发者更好地理解和使用ffmpeg sdk。通过手册的学习,开发者可以快速入门,并在实际项目中灵活运用ffmpeg sdk进行音视频处理。 总之,ffmpeg sdk开发手册是一本非常有价值的参考资料,通过学习手册,开发者可以系统地了解ffmpeg sdk的使用方法,提高音视频处理的能力。如果你对音视频处理有兴趣,建议你下载并阅读这本手册。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北雨南萍

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值