FFmpeg再学习 -- FFmpeg解码知识
继续看雷霄骅的 课程资料 - 基于FFmpeg+SDL的视频播放器的制作
前面用了五个篇幅来讲 FFmpeg,其主要目的是为实现将图片转视频的功能。
总的来说,对于 FFmepg 多少有一些了解了。但是源码部分还是一点都不清楚。接下来简单的梳理一下 FFmpeg 源码结构。毕竟现在从事的工作,不太偏重这个。等以后有机会再系统的研究吧。
一、FFmpeg 简介
其包含的库有:
libavformat 实现流协议,容器格式和基本I / O访问。
libavutil 包括哈希尔,解压缩器和杂项效用函数。
libavfilter 提供了通过一系列过滤器来改变已解码的音频和视频的意思。
libavdevice 提供了访问捕获和播放设备的抽象。
libswresample 实现音频混合和重采样程序。
libswscale 实现颜色转换和缩放程序。
工具
ffmpeg 是用于操纵,转换和流式传输多媒体内容的命令行工具箱。
ffplay 是一个简约的多媒体播放器。
ffprobe 是一种检查多媒体内容的简单分析工具。
ffserver 是一种多媒体流媒体服务器,用于直播。
其他小工具,如 aviocat,ismindex 和 qt-faststart。
二、FFmpeg 解码函数
FFmpeg解码函数简介
avformat_open_input() 打开输入视频文件。
avformat_find_stream_info() 获取视频文件信息。
avcodec_find_decoder() 查找解码器。
avcodec_open2() 打开解码器。
av_read_frame() 从输入文件读取一帧压缩数据。
avcodec_decode_video2() 解码一帧压缩数据。
avcodec_close() 关闭解码器。
avformat_close_input() 关闭输入视频文件。
FFmpeg解码的流程图如下所示
源码解析
【架构图】
【通用】
FFmpeg 源代码简单分析:av_register_all()
FFmpeg 源代码简单分析:avcodec_register_all()
FFmpeg 源代码简单分析:内存的分配和释放(av_malloc()、av_free()等)
FFmpeg 源代码简单分析:常见结构体的初始化和销毁(AVFormatContext,AVFrame等)
FFmpeg 源代码简单分析:av_find_decoder()和av_find_encoder()
FFmpeg 源代码简单分析:avcodec_open2()
FFmpeg 源代码简单分析:avcodec_close()
【解码】
图解FFMPEG打开媒体的函数avformat_open_input
FFmpeg 源代码简单分析:avformat_open_input()
FFmpeg 源代码简单分析:avformat_find_stream_info()
FFmpeg 源代码简单分析:av_read_frame()
FFmpeg 源代码简单分析:avcodec_decode_video2()
FFmpeg 源代码简单分析:avformat_close_input()
三、FFFmpeg解码的数据结构
FFmpeg解码的数据结构如下所示
FFmpeg数据结构简介
AVFormatContext
封装格式上下文结构体,也是统领全局的结构体,保存了视频文件封装格式相关信息。
iformat:输入视频的AVInputFormat nb_streams :输入视频的AVStream 个数 streams :输入视频的AVStream []数组 duration :输入视频的时长(以微秒为单位) bit_rate :输入视频的码率
每种封装格式(例如FLV, MKV, MP4, AVI)对应一个该结构体。
name:封装格式名称 long_name:封装格式的长名称 extensions:封装格式的扩展名 id:封装格式ID 一些封装格式处理的接口函数
视频文件中每个视频(音频)流对应一个该结构体。
id:序号 codec:该流对应的AVCodecContext time_base:该流的时基 r_frame_rate: 该流的帧率
编码器上下文结构体,保存了视频(音频)编解码相关信息。
codec:编解码器的AVCodec width, height:图像的宽高(只针对视频) pix_fmt:像素格式(只针对视频) sample_rate:采样率( 只针对音频) channels:声道数(只针对音频) sample_fmt:采样格式(只针对音频)
每种视频(音频)编解码器(例如H.264解码器)对应一个该结构体。
name:编解码器名称 long_name:编解码器长名称 type:编解码器类型 id:编解码器ID 一些编解码的接口函数
存储一帧压缩编码数据。
pts:显示时间戳 dts :解码时间戳 data :压缩编码数据 size :压缩编码数据大小 stream_index :所属的AVStream
存储一帧解码后像素(采样)数据。
data:解码后的图像像素数据(音频采样数据)。 linesize:对视频来说是图像中一行像素的大小;对音频来说是整个音频帧的大小。 width, height:图像的宽高(只针对视频)。 key_frame:是否为关键帧(只针对视频) 。 pict_type:帧类型(只针对视频) 。例如I, P, B。
四、解码示例
(1)源代码如下:
/** * 最简单的基于FFmpeg的解码器 * Simplest FFmpeg Decoder * * 雷霄骅 Lei Xiaohua * leixiaohua1020@126.com * 中国传媒大学/数字电视技术 * Communication University of China / Digital TV Technology * http://blog.csdn.net/leixiaohua1020 * * 本程序实现了视频文件的解码(支持HEVC,H.264,MPEG2等)。 * 是最简单的FFmpeg视频解码方面的教程。 * 通过学习本例子可以了解FFmpeg的解码流程。 * This software is a simplest video decoder based on FFmpeg. * Suitable for beginner of FFmpeg. * */ #include <stdio.h> #include "stdafx.h" #define __STDC_CONSTANT_MACROS #ifdef _WIN32 //Windows extern "C" { #include "libavcodec/avcodec.h" #include "libavformat/avformat.h" #include "libswscale/swscale.h" }; #else //Linux... #ifdef __cplusplus extern "C" { #endif #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libswscale/swscale.h> #ifdef __cplusplus }; #endif #endif int main(int argc, char* argv[]) { AVFormatContext *pFormatCtx; int i, videoindex; AVCodecContext *pCodecCtx; AVCodec *pCodec; AVFrame *pFrame, *pFrameYUV; uint8_t *out_buffer; AVPacket *packet; int y_size; int ret, got_picture; struct SwsContext *img_convert_ctx; //输入文件路径 char filepath[] = "Titanic.ts"; //创建两个解码后的输出文件 FILE *fp_yuv = fopen("output.yuv", "wb+"); FILE *fp_h264 = fopen("output.h264", "wb+"); av_register_all();//注册所有组件 avformat_network_init();//初始化网络 pFormatCtx = avformat_alloc_context();//初始化一个AVFormatContext if (avformat_open_input(&pFormatCtx, filepath, NULL, NULL) != 0) {//打开输入的视频文件 printf("Couldn't open input stream.\n"); return -1; } if (avformat_find_stream_info(pFormatCtx, NULL)<0) {//获取视频文件信息 printf("Couldn't find stream information.\n"); return -1; } videoindex = -1; for (i = 0; i < pFormatCtx->nb_streams; i++) { if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) { videoindex = i; break; } } if (videoindex == -1) { printf("Didn't find a video stream.\n"); return -1; } pCodecCtx = pFormatCtx->streams[videoindex]->codec; pCodec = avcodec_find_decoder(pCodecCtx->codec_id);//查找解码器 if (pCodec == NULL) { printf("Codec not found.\n"); return -1; } if (avcodec_open2(pCodecCtx, pCodec, NULL)<0) {//打开解码器 printf("Could not open codec.\n"); return -1; } /* * 在此处添加输出视频信息的代码 * 取自于pFormatCtx,使用fprintf() */ FILE *fp = fopen("info.txt", "wb+"); fprintf(fp,"时长:%d\n",pFormatCtx->duration); fprintf(fp,"封装格式:%s\n",pFormatCtx->iformat->long_name); fprintf(fp,"宽高:%d*%d\n",pFormatCtx->streams[videoindex]->codec->width, pFormatCtx->streams[videoindex]->codec->height); pFrame = av_frame_alloc(); pFrameYUV = av_frame_alloc(); out_buffer = (uint8_t *)av_malloc(avpicture_get_size(PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height)); avpicture_fill((AVPicture *)pFrameYUV, out_buffer, PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height); packet = (AVPacket *)av_malloc(sizeof(AVPacket)); //Output Info----------------------------- printf("--------------- File Information ----------------\n"); av_dump_format(pFormatCtx, 0, filepath, 0); printf("-------------------------------------------------\n"); img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL); int frame_cnt=0; while (av_read_frame(pFormatCtx, packet) >= 0) {//读取一帧压缩数据 if (packet->stream_index == videoindex) { /* * 在此处添加输出H264码流的代码 * 取自于packet,使用fwrite() */ fwrite(packet->data, 1, packet->size, fp_h264); //把H264数据写入fp_h264文件 ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);//解码一帧压缩数据 if (ret < 0) { printf("Decode Error.\n"); return -1; } if (got_picture) { sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize); y_size = pCodecCtx->width*pCodecCtx->height; /* * 在此处添加输出YUV的代码 * 取自于pFrameYUV,使用fwrite() */ printf("Decoded frame index: %d\n", frame_cnt); fwrite(pFrameYUV->data[0], 1, y_size, fp_yuv); //Y fwrite(pFrameYUV->data[1], 1, y_size / 4, fp_yuv); //U fwrite(pFrameYUV->data[2], 1, y_size / 4, fp_yuv); //V frame_cnt++; } } av_free_packet(packet); } //flush decoder //FIX: Flush Frames remained in Codec int frame_cnt1 = 0; while (1) { ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet); if (ret < 0) break; if (!got_picture) break; sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize); int y_size = pCodecCtx->width*pCodecCtx->height; printf("Flush Decoder: %d\n", frame_cnt1); fwrite(pFrameYUV->data[0], 1, y_size, fp_yuv); //Y fwrite(pFrameYUV->data[1], 1, y_size / 4, fp_yuv); //U fwrite(pFrameYUV->data[2], 1, y_size / 4, fp_yuv); //V frame_cnt1++; } sws_freeContext(img_convert_ctx); //关闭文件以及释放内存 fclose(fp_yuv); fclose(fp_h264); av_frame_free(&pFrameYUV); av_frame_free(&pFrame); avcodec_close(pCodecCtx); avformat_close_input(&pFormatCtx); return 0; }
(2)项目下载
(3)项目说明
使用 MediaInfo 软件查看 Titanic.ts 视频信息
查看调式生成 info.txt 可看出对应的视频信息
通过 YUV播放器 和 CyberLink PowerDVD 14 查看生成的视频。
五、后续总结
(1)flush_decoder作用
(2)解码后的数据为什么要经过sws_scale()函数处理?
(3)源码示例
源码中也是有示例的,查看 FFmpeg/doc/examples/
查看 decode_video.c 源码:
/* * 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 <stdio.h> #include <stdlib.h> #include <string.h> #include <libavcodec/avcodec.h> #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); for (i = 0; i < ysize; i++) fwrite(buf + i * wrap, 1, xsize, f); fclose(f); } static int decode_write_frame(const char *outfilename, AVCodecContext *avctx, AVFrame *frame, int *frame_count, AVPacket *pkt, int last) { int len, got_frame; char buf[1024]; len = avcodec_decode_video2(avctx, frame, &got_frame, pkt); if (len < 0) { fprintf(stderr, "Error while decoding frame %d\n", *frame_count); return len; } if (got_frame) { printf("Saving %sframe %3d\n", last ? "last " : "", *frame_count); fflush(stdout); /* the picture is allocated by the decoder, no need to free it */ snprintf(buf, sizeof(buf), "%s-%d", outfilename, *frame_count); pgm_save(frame->data[0], frame->linesize[0], frame->width, frame->height, buf); (*frame_count)++; } if (pkt->data) { pkt->size -= len; pkt->data += len; } return 0; } int main(int argc, char **argv) { const char *filename, *outfilename; const AVCodec *codec; AVCodecContext *c= NULL; int frame_count; FILE *f; AVFrame *frame; uint8_t inbuf[INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE]; AVPacket avpkt; if (argc <= 2) { fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]); exit(0); } filename = argv[1]; outfilename = argv[2]; avcodec_register_all(); av_init_packet(&avpkt); /* set end of buffer to 0 (this ensures that no overreading happens for damaged MPEG streams) */ memset(inbuf + INBUF_SIZE, 0, AV_INPUT_BUFFER_PADDING_SIZE); /* find the MPEG-1 video decoder */ codec = avcodec_find_decoder(AV_CODEC_ID_MPEG1VIDEO); if (!codec) { fprintf(stderr, "Codec not found\n"); exit(1); } c = avcodec_alloc_context3(codec); if (!c) { fprintf(stderr, "Could not allocate video codec context\n"); exit(1); } if (codec->capabilities & AV_CODEC_CAP_TRUNCATED) c->flags |= AV_CODEC_FLAG_TRUNCATED; // we do not send complete frames /* For some codecs, such as msmpeg4 and mpeg4, width and height MUST be initialized there because this information is not available in the bitstream. */ /* open it */ if (avcodec_open2(c, codec, NULL) < 0) { fprintf(stderr, "Could not open codec\n"); exit(1); } f = fopen(filename, "rb"); if (!f) { fprintf(stderr, "Could not open %s\n", filename); exit(1); } frame = av_frame_alloc(); if (!frame) { fprintf(stderr, "Could not allocate video frame\n"); exit(1); } frame_count = 0; for (;;) { avpkt.size = fread(inbuf, 1, INBUF_SIZE, f); if (avpkt.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 */ avpkt.data = inbuf; while (avpkt.size > 0) if (decode_write_frame(outfilename, c, frame, &frame_count, &avpkt, 0) < 0) exit(1); } /* 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. */ avpkt.data = NULL; avpkt.size = 0; decode_write_frame(outfilename, c, frame, &frame_count, &avpkt, 1); fclose(f); avcodec_free_context(&c); av_frame_free(&frame); return 0; }
编译:
Package libavdevice was not found in the pkg-config search path. Perhaps you should add the directory containing `libavdevice.pc' to the PKG_CONFIG_PATH environment variable No package 'libavdevice' found
Method 1: build the installed examples in a generic read/write user directory Copy to a read/write user directory and just use "make", it will link to the libraries on your system, assuming the PKG_CONFIG_PATH is correctly configured.
在 /etc/profile 最后添加: export PKG_CONFIG_PATH=/usr/local/ffmpeg/lib/pkgconfig:$PKG_CONFIG_PATH 执行: source /etc/profile 立即生效
然后再 make 编译,生成文件:
FFmpeg再学习 -- FFmpeg解码知识的更多相关文章
- FFmpeg再学习 -- FFmpeg+SDL+MFC实现图形界面视频播放器
继续看雷霄骅的 课程资料 - 基于FFmpeg+SDL的视频播放器的制作最后一篇,主要是想学一下 MFC 创建和配置. 一.创建 MFC 工程 文件->新建->项目->Visual ...
- FFmpeg再学习 -- SDL 环境搭建和视频显示
继续看雷霄骅的 课程资料 - 基于FFmpeg+SDL的视频播放器的制作 一.SDL 简介 参看:WIKI -- Simple DirectMedia Layer 参看:最简单的视音频播放示例9:SD ...
- FFmpeg再学习 -- 硬件加速编解码
为了搞硬件加速编解码,用了一周时间来看 CUDA,接下来开始加以总结. 一.什么是 CUDA (1)首先需要了解一下,什么是 CUDA. 参看:百度百科 -- CUDA 参看:CUDA基础介绍 参看: ...
- FFmpeg再学习 -- 视音频基础知识
最近一直在看雷霄骅 FFmpeg 系列视频,然后将自己的理解总结一下. 参看:<基于 FFmpeg + SDL 的视频播放器的制作>课程的视频 一.视频播放器原理 自己理解: 比如一个 M ...
- 【转】学习FFmpeg API – 解码视频
ffmpeg是编解码的利器,用了很久,以前看过dranger 的教程,非常精彩,受益颇多,是学习ffmpeg api很好的材料.可惜的是其针对的ffmpeg版本已经比较老了,而ffmpeg的更新又很快 ...
- [转]FFMPEG视音频编解码零基础学习方法
在CSDN上的这一段日子,接触到了很多同行业的人,尤其是使用FFMPEG进行视音频编解码的人,有的已经是有多年经验的“大神”,有的是刚开始学习的初学者.在和大家探讨的过程中,我忽然发现了一个问题:在“ ...
- [总结]FFMPEG视音频编解码零基础学习方法--转
ffmpeg编解码学习 目录(?)[-] ffmpeg程序的使用ffmpegexeffplayexeffprobeexe 1 ffmpegexe 2 ffplayexe 3 ffprobeexe ...
- FFMPEG视音频编解码零基础学习方法
在CSDN上的这一段日子,接触到了很多同行业的人,尤其是使用FFMPEG进行视音频编解码的人,有的已经是有多年经验的“大神”,有的是刚开始学习的初学者.在和大家探讨的过程中,我忽然发现了一个问题:在“ ...
- FFMPEG视音频编解码零基础学习方法-b
感谢大神分享,虽然现在还看不懂,留着大家一起看啦 PS:有不少人不清楚“FFmpeg”应该怎么读.它读作“ef ef em peg” 0. 背景知识 本章主要介绍一下FFMPEG都用在了哪里(在这里仅 ...
随机推荐
- mfc配置CAN通信
配置:把kerneldlls文件夹.ControlCAN.dll.ControlCAN.lib放在工程下面(debug和Release下面,最后需要exe和这些文件在一起):右键工程属性,链接器-&g ...
- Juniper SRX 简单命令二
--------------------------Juniper SRX 用户管理--------------------------- Juniper的命令,其实是比较形象的,英文稍微好一点,基本 ...
- L1-3 宇宙无敌加法器 - 令人激动的一道题目
L1-3 宇宙无敌加法器 - 令人激动的一道题目 感觉好久没有这么认真的做一道题了,今天看到一句话, 说是编程是一个工程型的工作,想要学好,"无他,唯手熟尔" 之前觉得自己笨,怀疑 ...
- ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/usr/local/mysql/tmp/mysql.sock'
查看是否开启服务 # ps -ef | grep mysql root 5605 5457 0 11:45 pts/2 00:00:00 grep mysql 查看my.cnf # cat /etc/ ...
- 【转发】Linux中设置服务自启动的三种方式
有时候我们需要Linux系统在开机的时候自动加载某些脚本或系统服务 主要用三种方式进行这一操作: ln -s 在/etc/rc.d/rc*.d目录中建立/e ...
- 使用unity2017.3 vuforia7摄像头放大的问题
最近项目需要用到vuforia并且运行环境是Win10,所幸vuforia7刚好出来了,特此记录下开发中遇到的坑 1.从assets store下载示例,运行找不到vuforia命名空间 很多人说的解 ...
- angular directive restrict 的用法
E 表示该指令是一个element; A 表示该指令是attribute; C 表示该指令是class; M 表示该指令是注视 实例如下: 原帖:www.thinkster.io/angularjs/ ...
- 你真的掌握 LVS、Nginx 及 HAProxy 的工作原理吗
你真的掌握 LVS.Nginx 及 HAProxy 的工作原理吗 当前大多数的互联网系统都使用了服务器集群技术,集群是将相同服务部署在多台服务器上构成一个集群整体对外提供服务,这些集群可以是 Web ...
- 17.并发容器之ThreadLocal
1. ThreadLocal的简介 在多线程编程中通常解决线程安全的问题我们会利用synchronzed或者lock控制线程对临界区资源的同步顺序从而解决线程安全的问题,但是这种加锁的方式会让未获取到 ...
- HDU-2196-树形dp/计算树上固定起点的最长路
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...