d盤有一個test.264文件,我們需要從這個h264文件中提取出一幀一幀的數據,所以直接採用ffmpeg來做。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#ifdef __cplusplus
extern "C" {
#endif
#include <libswscale/swscale.h>
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/opt.h>
#include <libavutil/time.h>
#include <libavutil/avutil.h>
#include <libavutil/imgutils.h>
#include <libavutil/hwcontext.h>
#include <libavutil/error.h>
#ifdef __cplusplus
}
#endif
int main(int argc, char *argv[])
{
AVFormatContext *input_ctx = NULL;
AVCodecContext *decoder_ctx = NULL;
AVCodec *decoder = NULL;
enum AVHWDeviceType type;
const char *dec_name = "h264_qsv";
const char *in_file = "d://test.264";
int video_stream = 0, ret;
AVPacket packet;
/* open the input file */
if (avformat_open_input(&input_ctx, in_file, NULL, NULL) != 0) {
fprintf(stderr, "Cannot open input file '%s'\n", argv[2]);
return -1;
}
/* find the video stream information */
ret = av_find_best_stream(input_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
if (ret < 0) {
fprintf(stderr, "Cannot find a video stream in the input file\n");
return -1;
}
while (ret >= 0)
{
if ( ( ret = av_read_frame(input_ctx,&packet) ) < 0 )
{
break;
}
if (video_stream == packet.stream_index)
{
printf( " frame size:%d \n" , packet.size );
}
av_packet_unref(&packet);
}
avformat_close_input(&input_ctx);
}
注意這裏提取的h264 packet是包含0x00 00 00 01頭部的一個完整的幀.