1. configure
configure --prefix=/usr/local/ffmpeg \
--enable-version3 \
--enable-gpl --enable-pthreads\
--disable-postproc --enable-avfilter \
--enable-shared \
--disable-ffserver --disable-ffplay \
--enable-encoder=rawvideo \
--enable-decoder=rawvideo \
--disable-encoder=asv1 --disable-encoder=asv2 \
--disable-encoder=bmp --disable-encoder=dvvideo \
--disable-encoder=ffv1 --disable-encoder=ffvhuff \
--disable-encoder=ppm --disable-encoder=rv10 \
--disable-encoder=targa --disable-encoder=xvid \
--disable-encoder=zlib
2. compile
FFMPEG_ROOT=/usr/local/ffmpeg
FFMPEG_INC=${FFMPEG_ROOT}/include
FFMPEG_LIB=${FFMPEG_ROOT}/lib
gcc -o test.x test.c -I${FFMPEG_INC} -L${FFMPEG_LIB} -lavcodec -lavformat
3. ffmpeg decode raw h264 file to yuv (yuv420p)
#include <math.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <libavutil/opt.h>
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/common.h>
#include <libavutil/imgutils.h>
#include <libavutil/mathematics.h>
#include <libavutil/samplefmt.h>
void usage(char *prog)
{
fprintf(stderr, "Usage:\n");
fprintf(stderr, "%s <ifile> <ofile>\n\n");
}
int main(int argc, char **argv)
{
AVFormatContext *ic;
AVCodec *dcodec, *ecodec;
AVCodecContext *dec, *enc;
enum AVCodecID codec_id;
AVFrame *avframe;
AVPacket avpkt, pkt;
int err, got_frame = 0, got_packet = 0, fd;
char buffer[1<<16];
if(argc < 3) {
usage(argv[0]);
exit(-1);
}
av_register_all();
av_init_packet(&avpkt);
av_init_packet(&pkt);
pkt.data = NULL, pkt.size = 0;
ic = avformat_alloc_context();
if (!ic) {
fprintf(stderr, "ic not created.\n");
exit(-1);
}
err = avformat_open_input(&ic, argv[1], 0, 0);
if(err < 0) {
fprintf(stderr, "file [%s] not open.\n", argv[1]);
exit(-1);
}
// dec = avcodec_alloc_context3(dcodec);
dec = ic->streams[0]->codec;
if (!dec) {
fprintf(stderr, "Could not allocate video decoder context\n");
exit(-1);
}
/* find the H264 video decoder */
dcodec = avcodec_find_decoder(dec->codec_id);
if (!dcodec) {
fprintf(stderr, "dcodec not found\n");
exit(-1);
}
/* open dcodec */
if (avcodec_open2(dec, dcodec, NULL) < 0) {
fprintf(stderr, "Could not open dcodec\n");
exit(-1);
}
ic->probesize = (1<<20);
avformat_find_stream_info(ic, NULL);
/* reopen dcodec */
if (avcodec_open2(dec, dcodec, NULL) < 0) {
fprintf(stderr, "Could not open dcodec\n");
exit(-1);
}
ecodec = avcodec_find_encoder(AV_CODEC_ID_RAWVIDEO);
if (!ecodec) {
fprintf(stderr, "ecodec not found\n");
exit(-1);
}
enc = avcodec_alloc_context3(ecodec);
if (!enc) {
fprintf(stderr, "Could not allocate video encoder context\n");
exit(-1);
}
enc->width = dec->width, enc->height = dec->height;
enc->pix_fmt = dec->pix_fmt;
// enc->codec_id = ecodec->id;
// enc->codec = ecodec;
if (avcodec_open2(enc, ecodec, NULL) < 0) {
fprintf(stderr, "Could not open ecodec\n");
exit(-1);
}
avframe = avcodec_alloc_frame();
if(!avframe) {
fprintf(stderr, "Could not allocate video frame\n");
exit(-1);
}
fd = open(argv[2], O_CREAT|O_RDWR|O_TRUNC, 0666);
while(av_read_frame(ic, &avpkt) >= 0) {
got_frame = got_packet = 0;
err = avcodec_decode_video2(dec, avframe, &got_frame, &avpkt);
if(got_frame) {
avcodec_encode_video2(enc, &pkt, avframe, &got_packet);
write(fd, pkt.data, pkt.size);
av_destruct_packet(&pkt);
}
av_init_packet(&avpkt);
}
fprintf(stderr, "finished.\n");
// av_free(avframe);
close(fd);
}
dec->width = 720, dec->height = 480;
dec->pix_fmt = AV_PIX_FMT_YUVJ420P;
dec->pix_fmt = AV_PIX_FMT_YUV420P;
enc->width = dec->width, enc->height = dec->height;
enc->width = txtwidth, enc->height = txtheight;
enc->pix_fmt = dec->pix_fmt;
// enc->pix_fmt = AV_PIX_FMT_YUV420P;
txtwidth = enc->width, txtheight = enc->height;