1. 我们平常最常用的音视频文件通常不是单独的音频信号和视频信号,而是一个整体的文件。这个文件会在其中包含音频流和视频流,并通过某种方式进行同步播放。通常,文件的音频和视频通过某种标准格式进行复用,生成某种封装格式,而封装的标志就是文件的扩展名,常用的有mp4/avi/flv/mkv等。
  2. 从底层考虑,我们可以使用的只有视频解码器、音频解码器,或者再加上一些附加的字幕解码等额外信息,却不存在所谓的mp4解码器或者avi解码器。所以,为了可以正确播放视频文件,必须将封装格式的视频文件分离出视频和音频信息分别进行解码和播放。
  3. 事实上,无论是mp4还是avi等文件格式,都有不同的标准格式,对于不同的格式并没有一种通用的解析方法。因此,FFMpeg专门定义了一个库来处理设计文件封装格式的功能,即libavformat。涉及文件的封装、解封装的问题,都可以通过调用libavformatAPI实现。这里我们实现一个demo来处理音视频文件的解复用与解码的功能。

FFmpeg解复用-解码器所包含的结构

  1. 这一过程实际上包括了封装文件的解复用和音频/视频解码两个步骤,因此需要定义的结构体大致包括用于解码和解封装的部分。我们定义下面这样的一个结构体实现这个功能:
  1. /*************************************************
  2. Struct: DemuxingVideoAudioContex
  3. Description: 保存解复用器和解码器的上下文组件
  4. *************************************************/
  5. typedef struct
  6. {
  7. AVFormatContext*fmt_ctx;
  8. AVCodecContext*video_dec_ctx, *audio_dec_ctx;
  9. AVStream*video_stream, *audio_stream;
  10. AVFrame *frame;
  11. AVPacket pkt;
  12. intvideo_stream_idx, audio_stream_idx;
  13. int width,height;
  14. uint8_t*video_dst_data[4];
  15. intvideo_dst_linesize[4];
  16. intvideo_dst_bufsize;
  17. enumAVPixelFormat pix_fmt;
  18. } DemuxingVideoAudioContex;

这个结构体中的大部分数据类型我们在前面做编码/解码等功能时已经见到过,另外几个是涉及到视频文件的复用的,其中有:

  • AVFormatContext:用于处理音视频封装格式的上下文信息。

  • AVStream:表示音频或者视频流的结构。

  • AVPixelFormat:枚举类型,表示图像像素的格式,最常用的是AV_PIX_FMT_YUV420P

FFmpeg解复用-解码的过程

1).相关结构的初始化
与使用FFMpeg进行其他操作一样,首先需注册FFMpeg组件:

  1. av_register_all();
  1. 随后,我们需要打开待处理的音视频文件。然而在此我们不使用打开文件的fopen函数,而是使用avformat_open_input函数。该函数不但会打开输入文件,而且可以根据输入文件读取相应的格式信息。该函数的声明如下:
  1. int avformat_open_input(AVFormatContext **ps, const char*url, AVInputFormat *fmt, AVDictionary **options);

该函数的各个参数的作用为:

  • ps:根据输入文件接收与格式相关的句柄信息;可以指向NULL,那么AVFormatContext类型的实例将由该函数进行分配。

  • url:视频url或者文件路径;

  • fmt:强制输入格式,可设置为NULL以自动检测;

  • options:保存文件格式无法识别的信息;

  • 返回值:成功返回0,失败则返回负的错误码;

该函数的调用方式为:

  1. if (avformat_open_input(&(va_ctx.fmt_ctx),files.src_filename, NULL, NULL) < 0){
  2. fprintf(stderr,"Could not open source file %s\n", files.src_filename);
  3. return -1;
  4. }

打开文件后,调用avformat_find_stream_info函数获取文件中的流信息。该函数的声明为:

  1. int avformat_find_stream_info(AVFormatContext *ic,AVDictionary **options);

该函数的第一个参数即前面的文件句柄,第二个参数也是用于保存无法识别的信息的AVDictionary的结构,通常可设为NULL。调用方式如:

  1. /* retrieve stream information */
  2. if (avformat_find_stream_info(va_ctx.fmt_ctx, NULL) <0){
  3. fprintf(stderr,"Could not find stream information\n");
  4. return -1;
  5. }

获取文件中的流信息后,下一步则是获取文件中的音频和视频流,并准备对音频和视频信息进行解码。获取文件中的流使用av_find_best_stream函数,其声明如:

  1. int av_find_best_stream(AVFormatContext *ic,
  2. enum AVMediaType type,
  3. int wanted_stream_nb,
  4. int related_stream,
  5. AVCodec **decoder_ret,
  6. int flags);

其中各个参数的意义:

  • ic:视频文件句柄;

  • type:表示数据的类型,常用的有AVMEDIA_TYPE_VIDEO表示视频,AVMEDIA_TYPE_AUDIO表示音频等;

  • wanted_stream_nb:我们期望获取到的数据流的数量,设置为-1使用自动获取;

  • related_stream:获取相关的音视频流,如果没有则设为-1;

  • decoder_ret:返回这一路数据流的解码器;

  • flags:未定义;

  • 返回值:函数执行成功返回流的数量,失败则返回负的错误码;

在函数执行成功后,便可调用avcodec_find_decoder和avcodec_open2打开解码器准备解码音视频流。该部分的代码实现如:

  1. static int open_codec_context(IOFileName &files,DemuxingVideoAudioContex &va_ctx, enum AVMediaType type)
  2. {
  3. int ret,stream_index;
  4. AVStream *st;
  5. AVCodecContext*dec_ctx = NULL;
  6. AVCodec *dec =NULL;
  7. AVDictionary*opts = NULL;
  8. ret =av_find_best_stream(va_ctx.fmt_ctx, type, -1, -1, NULL, 0);
  9. if (ret < 0){
  10. fprintf(stderr, "Could not find %s stream in input file'%s'\n", av_get_media_type_string(type), files.src_filename);
  11. return ret;
  12. }
  13. else{
  14. stream_index = ret;
  15. st =va_ctx.fmt_ctx->streams[stream_index];
  16. /* finddecoder for the stream */
  17. dec_ctx =st->codec;
  18. dec =avcodec_find_decoder(dec_ctx->codec_id);
  19. if (!dec){
  20. fprintf(stderr,"Failed to find %s codec\n", av_get_media_type_string(type));
  21. returnAVERROR(EINVAL);
  22. }
  23. /* Init thedecoders, with or without reference counting */
  24. av_dict_set(&opts, "refcounted_frames", files.refcount ?"1" : "0", 0);
  25. if ((ret =avcodec_open2(dec_ctx, dec, &opts)) < 0) {
  26. fprintf(stderr, "Failed to open %s codec\n",av_get_media_type_string(type));
  27. returnret;
  28. }
  29. switch (type){
  30. caseAVMEDIA_TYPE_VIDEO:
  31. va_ctx.video_stream_idx = stream_index;
  32. va_ctx.video_stream = va_ctx.fmt_ctx->streams[stream_index];
  33. va_ctx.video_dec_ctx = va_ctx.video_stream->codec;
  34. break;
  35. caseAVMEDIA_TYPE_AUDIO:
  36. va_ctx.audio_stream_idx = stream_index;
  37. va_ctx.audio_stream = va_ctx.fmt_ctx->streams[stream_index];
  38. va_ctx.audio_dec_ctx = va_ctx.audio_stream->codec;
  39. break;
  40. default:
  41. fprintf(stderr, "Error: unsupported MediaType: %s\n",av_get_media_type_string(type));
  42. return-1;
  43. }
  44. }
  45. return 0;
  46. }

整体初始化的函数代码为:

  1. int InitDemuxContext(IOFileName &files,DemuxingVideoAudioContex &va_ctx)
  2. {
  3. int ret = 0,width, height;
  4. /* register allformats and codecs */
  5. av_register_all();
  6. /* open inputfile, and allocate format context */
  7. if(avformat_open_input(&(va_ctx.fmt_ctx), files.src_filename, NULL, NULL)< 0) {
  8. fprintf(stderr, "Could not opensource file %s\n", files.src_filename);
  9. return -1;
  10. }
  11. /* retrievestream information */
  12. if(avformat_find_stream_info(va_ctx.fmt_ctx, NULL) < 0){
  13. fprintf(stderr, "Could not find stream information\n");
  14. return -1;
  15. }
  16. if(open_codec_context(files, va_ctx, AVMEDIA_TYPE_VIDEO) >= 0){
  17. files.video_dst_file = fopen(files.video_dst_filename, "wb");
  18. if(!files.video_dst_file){
  19. fprintf(stderr, "Could not open destination file %s\n",files.video_dst_filename);
  20. return-1;
  21. }
  22. /* allocateimage where the decoded image will be put */
  23. va_ctx.width = va_ctx.video_dec_ctx->width;
  24. va_ctx.height = va_ctx.video_dec_ctx->height;
  25. va_ctx.pix_fmt = va_ctx.video_dec_ctx->pix_fmt;
  26. ret =av_image_alloc(va_ctx.video_dst_data, va_ctx.video_dst_linesize, va_ctx.width,va_ctx.height, va_ctx.pix_fmt, 1);
  27. if (ret < 0) {
  28. fprintf(stderr, "Could not allocate raw video buffer\n");
  29. return-1;
  30. }
  31. va_ctx.video_dst_bufsize = ret;
  32. }
  33. if(open_codec_context(files, va_ctx, AVMEDIA_TYPE_AUDIO) >= 0) {
  34. files.audio_dst_file = fopen(files.audio_dst_filename, "wb");
  35. if(!files.audio_dst_file){
  36. fprintf(stderr, "Could not open destination file %s\n",files.audio_dst_filename);
  37. return-1;
  38. }
  39. }
  40. if(va_ctx.video_stream){
  41. printf("Demuxing video from file '%s' into '%s'\n",files.src_filename, files.video_dst_filename);
  42. }
  43. if(va_ctx.audio_stream){
  44. printf("Demuxing audio from file '%s' into '%s'\n",files.src_filename, files.audio_dst_filename);
  45. }
  46. /* dump inputinformation to stderr */
  47. av_dump_format(va_ctx.fmt_ctx, 0, files.src_filename, 0);
  48. if(!va_ctx.audio_stream && !va_ctx.video_stream){
  49. fprintf(stderr, "Could not find audio or video stream in the input,aborting\n");
  50. return -1;
  51. }
  52. return 0;
  53. }

随后要做的,是分配AVFrame和初始化AVPacket对象:

  1. va_ctx.frame = av_frame_alloc(); //分配AVFrame结构对象
  2. if (!va_ctx.frame){
  3. fprintf(stderr,"Could not allocate frame\n");
  4. ret =AVERROR(ENOMEM);
  5. goto end;
  6. }
  7. /* initialize packet, set data to NULL, let the demuxerfill it */
  8. av_init_packet(&va_ctx.pkt); //初始化AVPacket对象
  9. va_ctx.pkt.data = NULL;
  10. va_ctx.pkt.size = 0;

2).循环解析视频文件的包数据

解析视频文件的循环代码段为:

  1. /* read frames from the file */
  2. while (av_read_frame(va_ctx.fmt_ctx, &va_ctx.pkt)>= 0) //从输入程序中读取一个包的数据
  3. {
  4. AVPacketorig_pkt = va_ctx.pkt;
  5. do{
  6. ret =Decode_packet(files, va_ctx, &got_frame, 0); //解码这个包
  7. if (ret< 0)
  8. break;
  9. va_ctx.pkt.data += ret;
  10. va_ctx.pkt.size -= ret;
  11. } while(va_ctx.pkt.size > 0);
  12. av_packet_unref(&orig_pkt);
  13. }
  1. 这部分代码逻辑上非常简单,首先调用av_read_frame函数,从文件中读取一个packet的数据,并实现了一个Decode_packet对这个packet进行解码。Decode_packet函数的实现如下:
  1. int Decode_packet(IOFileName &files,DemuxingVideoAudioContex &va_ctx, int *got_frame, int cached)
  2. {
  3. int ret = 0;
  4. int decoded =va_ctx.pkt.size;
  5. static intvideo_frame_count = 0;
  6. static intaudio_frame_count = 0;
  7. *got_frame = 0;
  8. if(va_ctx.pkt.stream_index == va_ctx.video_stream_idx){
  9. /* decodevideo frame */
  10. ret =avcodec_decode_video2(va_ctx.video_dec_ctx, va_ctx.frame, got_frame,&va_ctx.pkt);
  11. if (ret< 0) {
  12. printf("Error decoding video frame(%d)\n", ret);
  13. return ret;
  14. }
  15. if(*got_frame){
  16. if(va_ctx.frame->width != va_ctx.width || va_ctx.frame->height !=va_ctx.height || va_ctx.frame->format != va_ctx.pix_fmt){
  17. /*To handle this change, one could call av_image_alloc again and
  18. *decode the following frames into another rawvideo file. */
  19. printf("Error: Width, height and pixel format have to be "
  20. "constant in a rawvideo file, but the width, height or "
  21. "pixel format of the input video changed:\n"
  22. "old: width = %d, height = %d, format = %s\n"
  23. "new: width = %d, height = %d, format = %s\n",
  24. va_ctx.width, va_ctx.height,av_get_pix_fmt_name((AVPixelFormat)(va_ctx.pix_fmt)),
  25. va_ctx.frame->width, va_ctx.frame->height,
  26. av_get_pix_fmt_name((AVPixelFormat)va_ctx.frame->format));
  27. return -1;
  28. }
  29. printf("video_frame%s n:%d coded_n:%d pts:%s\n", cached ?"(cached)" : "", video_frame_count++,va_ctx.frame->coded_picture_number, va_ctx.frame->pts);
  30. /* copy decoded frame to destination buffer:
  31. * this is required since rawvideo expects non aligned data */
  32. av_image_copy(va_ctx.video_dst_data, va_ctx.video_dst_linesize,
  33. (const uint8_t **)(va_ctx.frame->data), va_ctx.frame->linesize,
  34. va_ctx.pix_fmt, va_ctx.width, va_ctx.height);
  35. /*write to rawvideo file */
  36. fwrite(va_ctx.video_dst_data[0],va_ctx.video_dst_bufsize,files.video_dst_file);
  37. }
  38. }
  39. else if (va_ctx.pkt.stream_index ==va_ctx.audio_stream_idx){
  40. /* decodeaudio frame */
  41. ret =avcodec_decode_audio4(va_ctx.audio_dec_ctx, va_ctx.frame, got_frame,&va_ctx.pkt);
  42. if (ret< 0) {
  43. printf("Error decoding audio frame (%s)\n", ret);
  44. return ret;
  45. }
  46. /* Someaudio decoders decode only part of the packet, and have to be
  47. * calledagain with the remainder of the packet data.
  48. * Sample:fate-suite/lossless-audio/luckynight-partial.shn
  49. * Also,some decoders might over-read the packet. */
  50. decoded =FFMIN(ret, va_ctx.pkt.size);
  51. if(*got_frame){
  52. size_tunpadded_linesize = va_ctx.frame->nb_samples * av_get_bytes_per_sample((AVSampleFormat)va_ctx.frame->format);
  53. printf("audio_frame%s n:%d nb_samples:%d pts:%s\n",
  54. cached ? "(cached)" : "",
  55. audio_frame_count++, va_ctx.frame->nb_samples,
  56. va_ctx.frame->pts);
  57. /*Write the raw audio data samples of the first plane. This works
  58. * finefor packed formats (e.g. AV_SAMPLE_FMT_S16). However,
  59. * mostaudio decoders output planar audio, which uses a separate
  60. * planeof audio samples for each channel (e.g. AV_SAMPLE_FMT_S16P).
  61. * Inother words, this code will write only the first audio channel
  62. * inthese cases.
  63. * Youshould use libswresample or libavfilter to convert the frame
  64. * topacked data. */
  65. fwrite(va_ctx.frame->extended_data[0], 1, unpadded_linesize,files.audio_dst_file);
  66. }
  67. }
  68. /* If weuse frame reference counting, we own the data and need
  69. * tode-reference it when we don't use it anymore */
  70. if(*got_frame && files.refcount)
  71. av_frame_unref(va_ctx.frame);
  72. return decoded;
  73. }
  1. 在该函数中,首先对读取到的packet中的stream_index分别于先前获取的音频和视频的stream_index进行对比来确定是音频还是视频流。而后分别调用相应的解码函数进行解码,以视频流为例,判断当前stream为视频流后,调用avcodec_decode_video2函数将流数据解码为像素数据,并在获取完整的一帧之后,将其写出到输出文件中。

调用FFmpeg SDK解析封装格式的视频为音频流和视频流 - 图1