【FFMpeg SDK使用】4、调用FFmpeg SDK解析封装格式的视频为音频流和视频流

  • 音视频文件=(音频流 + 视频流) * 同步信息
  • 实现方式:音频和视频通过某种标准格式进行复用,生成某种封装格式
  • 封装格式:文件扩展名——mp4/avi/flv/mkv
  • 从底层考虑,我们可以使用的只有视频解码器、音频解码器,或者再加上一些附加的字幕解码等额外信息,却不存在所谓的mp4解码器或者avi解码器
  • 为了可以正确播放视频文件,必须将封装格式的视频文件分离出视频和音频信息分别进行解码和播放
  • FFMpeg专门定义了一个库来处理设计文件封装格式的功能,即libavformat

一、FFMpeg解复用-解码器所包含的结构

这一过程实际上包括了封装文件的解复用和音频/视频解码两个步骤,因此需要定义的结构体大致包括用于解码和解封装的部分。我们定义下面这样的一个结构体实现这个功能:

/*************************************************
Struct:         DemuxingVideoAudioContex
Description:    保存解复用器和解码器的上下文组件
*************************************************/
typedef struct
{
    AVFormatContext *fmt_ctx;
    AVCodecContext *video_dec_ctx, *audio_dec_ctx;
    AVStream *video_stream, *audio_stream;
    AVFrame *frame;
    AVPacket pkt;

    int video_stream_idx, audio_stream_idx;
    int width, height;

    uint8_t *video_dst_data[4];
    int video_dst_linesize[4];
    int video_dst_bufsize;
    enum AVPixelFormat pix_fmt;
} DemuxingVideoAudioContex;

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

  • AVFormatContext:用于处理音视频封装格式的上下文信息。
  • AVStream:表示音频或者视频流的结构。
  • AVPixelFormat:枚举类型,表示图像像素的格式,最常用的是AV_PIX_FMT_YUV420P

二、FFMpeg解复用-解码的过程

1、相关结构的初始化

与使用FFMpeg进行其他操作一样,首先需注册FFMpeg组件:

av_register_all();

随后,我们需要打开待处理的音视频文件。然而在此我们不使用打开文件的fopen函数,而是使用avformat_open_input函数。该函数不但会打开输入文件,而且可以根据输入文件读取相应的格式信息。该函数的声明如下:

int avformat_open_input(AVFormatContext **ps, const char *url, AVInputFormat *fmt, AVDictionary **options);

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

  • ps:根据输入文件接收与格式相关的句柄信息;可以指向NULL,那么AVFormatContext类型的实例将由该函数进行分配。
  • url:视频url或者文件路径;
  • fmt:强制输入格式,可设置为NULL以自动检测;
  • options:保存文件格式无法识别的信息;
  • 返回值:成功返回0,失败则返回负的错误码;

该函数的调用方式为:

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

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

int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)

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

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

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

int av_find_best_stream(AVFormatContext *ic,
                    enum AVMediaType type,
                    int wanted_stream_nb,
                    int related_stream,
                    AVCodec **decoder_ret,
                    int flags);

其中各个参数的意义:

  • ic:视频文件句柄;AVFormatContext *ic中会返回解码的AVCodecContext值,里面保存了码流相关的信息,宽、高、数据格式、当前流的数据格式等解码需要的东西
  • type:表示数据的类型,常用的有AVMEDIA_TYPE_VIDEO表示视频,AVMEDIA_TYPE_AUDIO表示音频等;
  • wanted_stream_nb:我们期望获取到的数据流的数量,设置为-1使用自动获取;
  • related_stream:获取相关的音视频流,如果没有则设为-1;
  • decoder_ret:返回这一路数据流的解码器;
  • flags:未定义;
  • 返回值:函数执行成功返回流的数量,失败则返回负的错误码;
    函数执行成功后,便可调用avcodec_find_decoder和avcodec_open2打开解码器准备解码音视频流。该部分的代码实现如:
static int open_codec_context(IOFileName &files, DemuxingVideoAudioContex &va_ctx, enum AVMediaType type)
{
    int ret, stream_index;
    AVStream *st;
    AVCodecContext *dec_ctx = NULL;
    AVCodec *dec = NULL;
    AVDictionary *opts = NULL;

    ret = av_find_best_stream(va_ctx.fmt_ctx, type, -1, -1, NULL, 0);
    if (ret < 0) 
    {
        fprintf(stderr, "Could not find %s stream in input file '%s'\n", av_get_media_type_string(type), files.src_filename);
        return ret;
    } 
    else 
    {
        stream_index = ret;
        st = va_ctx.fmt_ctx->streams[stream_index];

        /* find decoder for the stream */
        dec_ctx = st->codec;
        dec = avcodec_find_decoder(dec_ctx->codec_id);
        if (!dec) 
        {
            fprintf(stderr, "Failed to find %s codec\n", av_get_media_type_string(type));
            return AVERROR(EINVAL);
        }

        /* Init the decoders, with or without reference counting */
        av_dict_set(&opts, "refcounted_frames", files.refcount ? "1" : "0", 0);
        if ((ret = avcodec_open2(dec_ctx, dec, &opts)) < 0) 
        {
            fprintf(stderr, "Failed to open %s codec\n", av_get_media_type_string(type));
            return ret;
        }

        switch (type)
        {
        case AVMEDIA_TYPE_VIDEO:
            va_ctx.video_stream_idx = stream_index;
            va_ctx.video_stream = va_ctx.fmt_ctx->streams[stream_index];
            va_ctx.video_dec_ctx = va_ctx.video_stream->codec;
            break;
        case AVMEDIA_TYPE_AUDIO:
            va_ctx.audio_stream_idx = stream_index;
            va_ctx.audio_stream = va_ctx.fmt_ctx->streams[stream_index];
            va_ctx.audio_dec_ctx = va_ctx.audio_stream->codec;
            break;
        default:
            fprintf(stderr, "Error: unsupported MediaType: %s\n", av_get_media_type_string(type));
            return -1;
        }
    }

    return 0;
}

之后打印码流解析出的信息:

void av_dump_format(AVFormatContext *ic,
                    int index,
                    const char *url,
                    int is_output);

实际使用:

	av_dump_format(va_ctx.fmt_ctx, 0, files.src_filename, 0);

打印在串口:(打印出视频和音频的一些码流信息:编码格式、比特率、宽高、帧、创建时间等)
在这里插入图片描述

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

int InitDemuxContext(IOFileName &files, DemuxingVideoAudioContex &va_ctx)
{
    int ret = 0, width, height;

    /* register all formats and codecs */
    av_register_all();

    /* open input file, and allocate format context */
    if (avformat_open_input(&(va_ctx.fmt_ctx), files.src_filename, NULL, NULL) < 0)
    {
        fprintf(stderr, "Could not open source file %s\n", files.src_filename);
        return -1;
    }

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

    if (open_codec_context(files, va_ctx, AVMEDIA_TYPE_VIDEO) >= 0) 
    {
        files.video_dst_file = fopen(files.video_dst_filename, "wb");
        if (!files.video_dst_file) 
        {
            fprintf(stderr, "Could not open destination file %s\n", files.video_dst_filename);
            return -1;
        }

        /* allocate image where the decoded image will be put */
        va_ctx.width = va_ctx.video_dec_ctx->width;
        va_ctx.height = va_ctx.video_dec_ctx->height;
        va_ctx.pix_fmt = va_ctx.video_dec_ctx->pix_fmt;
        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);
        if (ret < 0) 
        {
            fprintf(stderr, "Could not allocate raw video buffer\n");
            return -1;
        }
        va_ctx.video_dst_bufsize = ret;
    }

    if (open_codec_context(files, va_ctx, AVMEDIA_TYPE_AUDIO) >= 0) 
    {
        files.audio_dst_file = fopen(files.audio_dst_filename, "wb");
        if (!files.audio_dst_file) 
        {
            fprintf(stderr, "Could not open destination file %s\n", files.audio_dst_filename);
            return -1;
        }
    }

    if (va_ctx.video_stream)
    {
        printf("Demuxing video from file '%s' into '%s'\n", files.src_filename, files.video_dst_filename);
    }

    if (va_ctx.audio_stream)
    {
        printf("Demuxing audio from file '%s' into '%s'\n", files.src_filename, files.audio_dst_filename);
    }

    /* dump input information to stderr */
    av_dump_format(va_ctx.fmt_ctx, 0, files.src_filename, 0);

    if (!va_ctx.audio_stream && !va_ctx.video_stream) 
    {
        fprintf(stderr, "Could not find audio or video stream in the input, aborting\n");
        return -1;
    }

    return 0;
}

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

va_ctx.frame = av_frame_alloc();            //分配AVFrame结构对象
if (!va_ctx.frame)
{
    fprintf(stderr, "Could not allocate frame\n");
    ret = AVERROR(ENOMEM);
    goto end;
}

/* initialize packet, set data to NULL, let the demuxer fill it */
av_init_packet(&va_ctx.pkt);                //初始化AVPacket对象
va_ctx.pkt.data = NULL;
va_ctx.pkt.size = 0;

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

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

/* read frames from the file */
while (av_read_frame(va_ctx.fmt_ctx, &va_ctx.pkt) >= 0)     //从输入程序中读取一个包的数据
{
    AVPacket orig_pkt = va_ctx.pkt;
    do 
    {
        ret = Decode_packet(files, va_ctx, &got_frame, 0);  //解码这个包
        if (ret < 0)
            break;
        va_ctx.pkt.data += ret;
        va_ctx.pkt.size -= ret;
    } while (va_ctx.pkt.size > 0);
    av_packet_unref(&orig_pkt);
}

这部分代码逻辑上非常简单,首先调用av_read_frame函数,从文件中读取一个packet的数据,并实现了一个Decode_packet对这个packet进行解码。Decode_packet函数的实现如下:

int Decode_packet(IOFileName &files, DemuxingVideoAudioContex &va_ctx, int *got_frame, int cached)
{
	int ret = 0;
	int decoded = va_ctx.pkt.size;
	static int video_frame_count = 0;
	static int audio_frame_count = 0;

	*got_frame = 0;

	/* 根绝stream id获得当前读到的是音频数据还是视频数据 */
	if (va_ctx.pkt.stream_index == va_ctx.video_stream_idx)
	{
		printf("Video packet pts: %d\n", va_ctx.pkt.pts);
		/* decode video frame */
		ret = avcodec_decode_video2(va_ctx.video_dec_ctx, va_ctx.frame, got_frame, &va_ctx.pkt);
		if (ret < 0)
		{
			printf("Error decoding video frame (%d)\n", ret);
			return ret;
		}

		/* 判断是否为完整一帧 */
		if (*got_frame)
		{
			if (va_ctx.frame->width != va_ctx.width || va_ctx.frame->height != va_ctx.height ||
				va_ctx.frame->format != va_ctx.pix_fmt)
			{
				/* To handle this change, one could call av_image_alloc again and
				* decode the following frames into another rawvideo file. */
				printf("Error: Width, height and pixel format have to be "
					"constant in a rawvideo file, but the width, height or "
					"pixel format of the input video changed:\n"
					"old: width = %d, height = %d, format = %s\n"
					"new: width = %d, height = %d, format = %s\n",
					va_ctx.width, va_ctx.height, av_get_pix_fmt_name((AVPixelFormat)(va_ctx.pix_fmt)),
					va_ctx.frame->width, va_ctx.frame->height,
					av_get_pix_fmt_name((AVPixelFormat)va_ctx.frame->format));
				return -1;
			}

			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);

			/* copy decoded frame to destination buffer:
			* this is required since rawvideo expects non aligned data */
			av_image_copy(va_ctx.video_dst_data, va_ctx.video_dst_linesize,
				(const uint8_t **)(va_ctx.frame->data), va_ctx.frame->linesize,
				va_ctx.pix_fmt, va_ctx.width, va_ctx.height);	/* 其实就相当于C语言的memcpy,只不过这个会自动将颜色分量等分别进行拷贝 */

			/* write to rawvideo file */
			fwrite(va_ctx.video_dst_data[0], 1, va_ctx.video_dst_bufsize, files.video_dst_file);
		}
	}
	else if (va_ctx.pkt.stream_index == va_ctx.audio_stream_idx)
	{
		/* decode audio frame */
		ret = avcodec_decode_audio4(va_ctx.audio_dec_ctx, va_ctx.frame, got_frame, &va_ctx.pkt);
		if (ret < 0)
		{
			printf("Error decoding audio frame (%s)\n", ret);
			return ret;
		}
		/* Some audio decoders decode only part of the packet, and have to be
		* called again with the remainder of the packet data.
		* Sample: fate-suite/lossless-audio/luckynight-partial.shn
		* Also, some decoders might over-read the packet. */
		decoded = FFMIN(ret, va_ctx.pkt.size);

		if (*got_frame)
		{
			size_t unpadded_linesize = va_ctx.frame->nb_samples * av_get_bytes_per_sample((AVSampleFormat)va_ctx.frame->format);
//			printf("audio_frame%s n:%d nb_samples:%d pts:%s\n",	cached ? "(cached)" : "", audio_frame_count++, va_ctx.frame->nb_samples, va_ctx.frame->pts);

			/* Write the raw audio data samples of the first plane. This works
			* fine for packed formats (e.g. AV_SAMPLE_FMT_S16). However,
			* most audio decoders output planar audio, which uses a separate
			* plane of audio samples for each channel (e.g. AV_SAMPLE_FMT_S16P).
			* In other words, this code will write only the first audio channel
			* in these cases.
			* You should use libswresample or libavfilter to convert the frame
			* to packed data. */
			fwrite(va_ctx.frame->extended_data[0], 1, unpadded_linesize, files.audio_dst_file);
		}
	}

	/* If we use frame reference counting, we own the data and need
	* to de-reference it when we don't use it anymore */
	if (*got_frame && files.refcount)
		av_frame_unref(va_ctx.frame);

	return decoded;
}

在该函数中,首先对读取到的packet中的stream_index分别于先前获取的音频和视频的stream_index进行对比来确定是音频还是视频流。而后分别调用相应的解码函数进行解码,以视频流为例,判断当前stream为视频流后,调用avcodec_decode_video2函数将流数据解码为像素数据,并在获取完整的一帧之后,将其写出到输出文件中。

三、总结

相对于前文讲述过的解码H.264格式裸码流,解封装+解码过程看似多了一个步骤,然而在实现起来实际上并无过多差别。这主要是由于FFMpeg中的多个API已经很好地实现了封装文件的解析和读取过程,如打开文件我们使用avformat_open_input代替fopen,读取数据包使用av_read_frame代替fread,其他方面只需要多一步判断封装文件中数据流的类型即可,剩余部分与裸码流的解码并无太多差别。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值