1. test.c
//gcc -g -o test.exe test.c -l./libavcodec -l./libavformat -l./libavdevice -l./libavutil
//gcc -o test test.c -llibavutil -llibavformat -llibavcodec -lz -llibavutil -lm
/*
gcc -o test test.c -I/d/clinux/ffmpeg -I./ -L/usr/local/lib -lavformat -lavcodec -lavutil -lavdevice -lswscale -lz -lm
libavcodec\avcodec.h
#include "libavutil/avutil.h"
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavdevice/avdevice.h"
#include "libavcodec/opt.h"
#include "libavcodec/audioconvert.h"
#include "libavcodec/colorspace.h"
#include "libavutil/fifo.h"
#include "libavutil/avstring.h"
#include "libavformat/os_support.h"
*/
extern "C"
{
#include
#include
#include
#include
}
void SaveFrame(AVFrame *pFrame, int width, int height, int iFrame) {
FILE *pFile;
char szFilename[32];
int y;
// Open file
sprintf(szFilename, "frame%d.ppm", iFrame);
pFile=fopen(szFilename, "wb");
if(pFile==NULL)
return;
// Write header
fprintf(pFile, "P6\n%d %d\n255\n", width, height);
// Write pixel data
for(y=0; y fwrite(pFrame->data[0]+y*pFrame->linesize[0], 1, width*3, pFile);
// Close file
fclose(pFile);
}
int main(int argc, char *argv[]) {
AVFormatContext *pFormatCtx;
int i, videoStream;
AVCodecContext *pCodecCtx;
AVCodec *pCodec;
AVFrame *pFrame;
AVFrame *pFrameRGB;
AVPacket packet;
int frameFinished;
int numBytes;
uint8_t *buffer;
if(argc < 2) {
printf("Please provide a movie file\n");
return -1;
}
// Register all formats and codecs
/*########################################
#[1]
########################################*/
av_register_all();
// Open video file
/*########################################
[2]
########################################*/
if(av_open_input_file(&pFormatCtx, argv[1], NULL, 0, NULL)!=0)
return -1; // Couldn't open file
// Retrieve stream information
/*########################################
[3]
########################################*/
if(av_find_stream_info(pFormatCtx)<0)
return -1; // Couldn't find stream information
// Dump information about file onto standard error
dump_format(pFormatCtx, 0, argv[1], 0);
// Find the first video stream
videoStream=-1;
for(i=0; i
nb_streams; i++)
if(pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO) {
videoStream=i;
break;
}
if(videoStream==-1)
return -1; // Didn't find a video stream
// Get a pointer to the codec context for the video stream
pCodecCtx=pFormatCtx->streams[videoStream]->codec;
// Find the decoder for the video stream
pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
if(pCodec==NULL) {
fprintf(stderr, "Unsupported codec!\n");
return -1; // Codec not found
}
// Open codec
if(avcodec_open(pCodecCtx, pCodec)<0)
return -1; // Could not open codec
// Allocate video frame
pFrame=avcodec_alloc_frame();
// Allocate an AVFrame structure
pFrameRGB=avcodec_alloc_frame();
if(pFrameRGB==NULL)
return -1;
// Determine required buffer size and allocate buffer
numBytes=avpicture_get_size(PIX_FMT_RGB24, pCodecCtx->width,
pCodecCtx->height);
buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t));
// Assign appropriate parts of buffer to image planes in pFrameRGB
// Note that pFrameRGB is an AVFrame, but AVFrame is a superset
// of AVPicture
avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24,
pCodecCtx->width, pCodecCtx->height);
// Read frames and save first five frames to disk
/*########################################
[4]
########################################*/
i=0;
while(av_read_frame(pFormatCtx, &packet)>=0) {
// Is this a packet from the video stream?
if(packet.stream_index==videoStream) {
// Decode video frame
avcodec_decode_video(pCodecCtx, pFrame, &frameFinished,
packet.data, packet.size);
// Did we get a video frame?
if(frameFinished) {
// Convert the image from its native format to RGB
img_convert((AVPicture *)pFrameRGB, PIX_FMT_RGB24,
(AVPicture*)pFrame, pCodecCtx->pix_fmt,
pCodecCtx->width,
pCodecCtx->height);
// Save the frame to disk
if(++i<=5)
SaveFrame(pFrameRGB, pCodecCtx->width, pCodecCtx->height,
i);
}
}
// Free the packet that was allocated by av_read_frame
av_free_packet(&packet);
}
// Free the RGB image
av_free(buffer);
av_free(pFrameRGB);
// Free the YUV frame
av_free(pFrame);
// Close the codec
avcodec_close(pCodecCtx);
// Close the video file
av_close_input_file(pFormatCtx);
return 0;
}
/*********************************************************/
2. ffread.c
#include
#include
#include "libavutil/avstring.h"
#include "libavformat/avformat.h"
#include "libavdevice/avdevice.h"
#include "libswscale/swscale.h"
#include "libavcodec/audioconvert.h"
#include "libavcodec/colorspace.h"
#include "libavcodec/opt.h"
int main(int argc, const char *argv[]) {
const char *fname;
AVFormatContext *ic;
AVStream *is, *tis;
AVCodec *codec;
AVFormatParameters params, *ap = ¶ms;
AVPacket pkt;
AVFrame frame;
AVPicture *pic;
int got_picture;
int i, r;
av_register_all();
if(argc != 2) {
fprintf(stderr, "Usage: %s \n", argv[0]);
return 1;
}
fname = argv[1];
fprintf(stderr, "Input file: %s \n", fname);
memset(ap, 0, sizeof(AVFormatParameters));
ap->video_codec_id = CODEC_ID_NONE;
printf("codec id %X\n", ap->video_codec_id);
r = av_open_input_file(&ic, fname, NULL, 0, ap);
if(r != 0 || ic == NULL) {
fprintf(stderr, "can not open input file %s\n", fname);
return 1;
}
av_find_stream_info(ic);
is = NULL;
for(i = 0; i < ic->nb_streams; i++) {
tis = ic->streams[i];
if(tis->codec->codec_type == CODEC_TYPE_VIDEO) {
printf("channel %d of %d\n", i, ic->nb_streams);
is = tis;
}
}
codec = avcodec_find_decoder(is->codec->codec_id);
if(codec == NULL) {
fprintf(stderr, "can not find codec %s\n", is->codec->codec_name);
return 1;
}
r = avcodec_open(is->codec, codec);
if(r != 0) {
fprintf(stderr, "can not initialize a AVCodecContext for codec %s\n",
codec->name);
return 1;
}
printf("Codec %s (%d x %d)\n", codec->name, is->codec->width,
is->codec->height);
for(i = 0; i < 10;) {
r = av_read_frame(ic, &pkt);
if(r != 0) {
fprintf(stderr, "no more frame\n");
return 1;
}
if(pkt.stream_index != is->index)
continue;
if(pkt.pts != AV_NOPTS_VALUE)
printf("Frame : pts=%lld, dts=%lld, size=%d, data=%x\n",
i, pkt.stream_index, pkt.pts, pkt.dts, pkt.size, pkt.data);
else
printf("Frame : pts=N/A, dts=%lld, size=%d, data=%x\n",
i, pkt.stream_index, pkt.dts, pkt.size, pkt.data);
av_pkt_dump(stdout, &pkt, 0);
avcodec_get_frame_defaults(&frame);
r = avcodec_decode_video(is->codec, &frame, &got_picture,
pkt.data, pkt.size);
if(r < 0) {
printf("decoding error\n");
return 1;
}
if(got_picture) {
printf("\tlinesize[4]={%d %d %d %d}, data[4]={%x %x %x %x)}\n",
frame.linesize[0], frame.linesize[1],
frame.linesize[2], frame.linesize[3],
frame.data[0], frame.data[1],
frame.data[2], frame.data[3]);
}
av_free_packet(&pkt);
i++;
}
avcodec_close(is->codec);
return 0;
}
- /****************************************/
#include
- #include
- #include
- #include
-
- #ifdef HAVE_AV_CONFIG_H
- #undef HAVE_AV_CONFIG_H
- #endif
-
- #include "ffmpeg\avcodec.h"
- #include "Logger.h"
-
- #define INBUF_SIZE 4096
-
-
-
-
- void audio_encode_example(const char *filename)
- {
- AVCodec *codec;
- AVCodecContext *c= NULL;
- int frame_size, i, j, out_size, outbuf_size;
- FILE *f;
- short *samples;
- float t, tincr;
- uint8_t *outbuf;
-
- __TRACE("audio_encode_example.log", "Audio encoding\n");
-
-
- codec = avcodec_find_encoder(CODEC_ID_MP2);
- if (!codec) {
- __TRACE("audio_encode_example.log", "codec not found\n");
- exit(1);
- }
-
- c= avcodec_alloc_context();
-
-
- c->bit_rate = 64000;
- c->sample_rate = 44100;
- c->channels = 2;
-
-
- if (avcodec_open(c, codec) < 0) {
- __TRACE("audio_encode_example.log", "could not open codec\n");
- exit(1);
- }
-
-
- frame_size = c->frame_size;
- samples = (short*)malloc(frame_size * 2 * c->channels);
- outbuf_size = 10000;
- outbuf = (uint8_t*)malloc(outbuf_size);
-
- f = fopen(filename, "wb");
- if (!f) {
- __TRACE("audio_encode_example.log", "could not open %s\n", filename);
- exit(1);
- }
-
-
- t = 0;
- tincr = 2 * M_PI * 440.0 / c->sample_rate;
- for(i=0;i<200;i++) {
- for(j=0;j"=" while inbuf_ptr="inbuf;" break; 0) if f); INBUF_SIZE, 1, size="fread(inbuf," { for(;;) * eof until decode } exit(1); av_free(c); (!outfile) ?wb?); outfile="fopen(outfilename," filename); %s\n?, open not ?could __TRACE(?audio_decode_example.log?, (!f) ?rb?); f="fopen(filename," outbuf="(uint8_t*)malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);" codec\n?); < codec) (avcodec_open(c, it c="avcodec_alloc_context();" found\n?); ?codec (!codec) codec="avcodec_find_decoder(CODEC_ID_MP2);" decoder audio mpeg the find FF_INPUT_BUFFER_PADDING_SIZE); 0, +="tincr;" memset(inbuf streams) damaged for happens overreading no that ensures (this 0 to buffer of end set decoding\n?); printf(?Audio *inbuf_ptr; FF_INPUT_BUFFER_PADDING_SIZE], inbuf[INBUF_SIZE uint8_t *outbuf; *outfile; *f, FILE len; size, out_size, int *c="NULL;" AVCodecContext *codec; AVCodec *filename) char const *outfilename, audio_decode_example(const void decoding. Audio avcodec_close(c); free(samples); free(outbuf); fclose(f); fwrite(outbuf, samples); outbuf_size, outbuf, out_size="avcodec_encode_audio(c," samples encode t samples[2*j+1]="samples[2*j];" 10000); samples[2*j]="(int)(sin(t)"> 0) {
- len = avcodec_decode_audio(c, (short *)outbuf, &out_size,
- inbuf_ptr, size);
- if (len < 0) {
- fprintf(stderr, "Error while decoding\n");
- exit(1);
- }
- if (out_size > 0) {
-
- fwrite(outbuf, 1, out_size, outfile);
- }
- size -= len;
- inbuf_ptr += len;
- }
- }
-
- fclose(outfile);
- fclose(f);
- free(outbuf);
-
- avcodec_close(c);
- av_free(c);
- }
-
-
-
-
- void video_encode_example(const char *filename)
- {
- AVCodec *codec;
- AVCodecContext *c= NULL;
- int i, out_size, size, x, y, outbuf_size;
- FILE *f;
- AVFrame *picture;
- uint8_t *outbuf, *picture_buf;
-
- __TRACE("video_encode_example.log", "Video encoding\n");
-
-
- codec = avcodec_find_encoder(CODEC_ID_MPEG1VIDEO);
- if (!codec) {
- fprintf(stderr, "codec not found\n");
- exit(1);
- }
-
- c= avcodec_alloc_context();
- picture= avcodec_alloc_frame();
-
-
- c->bit_rate = 400000;
-
- c->width = 352;
- c->height = 288;
-
-
- c->time_base.num = 1;
- c->time_base.den = 25;
-
- c->gop_size = 10;
- c->max_b_frames=1;
- c->pix_fmt = PIX_FMT_YUV420P;
-
-
- if (avcodec_open(c, codec) < 0) {
- __TRACE("video_encode_example.log", "could not open codec\n");
- exit(1);
- }
-
-
-
- f = fopen(filename, "wb");
- if (!f) {
- __TRACE("video_encode_example.log", "could not open %s\n", filename);
- exit(1);
- }
-
-
- outbuf_size = 100000;
- outbuf = (uint8_t*)malloc(outbuf_size);
- size = c->width * c->height;
- picture_buf = (uint8_t*)malloc((size * 3) / 2);
-
- picture->data[0] = picture_buf;
- picture->data[1] = picture->data[0] + size;
- picture->data[2] = picture->data[1] + size / 4;
- picture->linesize[0] = c->width;
- picture->linesize[1] = c->width / 2;
- picture->linesize[2] = c->width / 2;
-
-
- for(i=0;i<25;i++) {
- fflush(stdout);
-
-
- for(y=0;yheight;y++) {
- for(x=0;xwidth;x++) {
- picture->data[0][y * picture->linesize[0] + x] = x + y + i * 3;
- }
- }
-
-
- for(y=0;yheight/2;y++) {
- for(x=0;xwidth/2;x++) {
- picture->data[1][y * picture->linesize[1] + x] = 128 + y + i * 2;
- picture->data[2][y * picture->linesize[2] + x] = 64 + x + i * 5;
- }
- }
-
-
- out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture);
- __TRACE("video_encode_example.log", "encoding frame %3d (size=%5d)\n", i, out_size);
- fwrite(outbuf, 1, out_size, f);
- }
-
-
- for(; out_size; i++) {
- fflush(stdout);
-
- out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);
- __TRACE("video_encode_example.log", "write frame %3d (size=%5d)\n", i, out_size);
- fwrite(outbuf, 1, out_size, f);
- }
-
-
- outbuf[0] = 0x00;
- outbuf[1] = 0x00;
- outbuf[2] = 0x01;
- outbuf[3] = 0xb7;
- fwrite(outbuf, 1, 4, f);
- fclose(f);
- free(picture_buf);
- free(outbuf);
-
- avcodec_close(c);
- av_free(c);
- av_free(picture);
- __TRACE("video_encode_example.log", "\n");
- }
-
-
-
-
-
- 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;iif INBUF_SIZE, { * } exit(1); not c="avcodec_alloc_context();" found\n?); ?codec (!codec) codec="avcodec_find_decoder(CODEC_ID_MPEG1VIDEO);" decoder mpeg the find FF_INPUT_BUFFER_PADDING_SIZE); 0, + memset(inbuf streams) damaged for happens overreading no that ensures (this 0 to buffer of end set decoding\n?); *inbuf_ptr; FF_INPUT_BUFFER_PADDING_SIZE], inbuf[INBUF_SIZE uint8_t FILE len; size, int *c="NULL;" AVCodecContext *codec; AVCodec *filename) char const *outfilename, void fclose(f); if(codec- picture="avcodec_alloc_frame();" __TRACE(?pgm_save.log?, video mpeg1 ?Video buf[1024]; *picture; AVFrame *f; got_picture, frame, video_decode_example(const wrap,1,xsize,f); i fwrite(buf>capabilities&CODEC_CAP_TRUNCATED)
- c->flags|= CODEC_FLAG_TRUNCATED;
-
-
-
-
-
-
- if (avcodec_open(c, codec) < 0) {
- __TRACE("pgm_save.log", "could not open codec\n");
- exit(1);
- }
-
-
-
- f = fopen(filename, "rb");
- if (!f) {
- __TRACE("pgm_save.log", "could not open %s\n", filename);
- exit(1);
- }
-
- frame = 0;
- for(;;) {
- size = fread(inbuf, 1, INBUF_SIZE, f);
- if (size == 0)
- break;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- inbuf_ptr = inbuf;
- while (size > 0) {
- len = avcodec_decode_video(c, picture, &got_picture,
- inbuf_ptr, size);
- if (len < 0) {
- __TRACE("pgm_save.log", "Error while decoding frame %d\n", frame);
- exit(1);
- }
- if (got_picture) {
- __TRACE("pgm_save.log", "saving frame %3d\n", frame);
-
-
-
- _snprintf(buf, sizeof(buf), outfilename, frame);
- pgm_save(picture->data[0], picture->linesize[0],
- c->width, c->height, buf);
- frame++;
- }
- size -= len;
- inbuf_ptr += len;
- }
- }
-
-
-
-
- len = avcodec_decode_video(c, picture, &got_picture,
- NULL, 0);
- if (got_picture) {
- __TRACE("pgm_save.log", "saving last frame %3d\n", frame);
-
-
-
- _snprintf(buf, sizeof(buf), outfilename, frame);
- pgm_save(picture->data[0], picture->linesize[0],
- c->width, c->height, buf);
- frame++;
- }
-
- fclose(f);
-
- avcodec_close(c);
- av_free(c);
- av_free(picture);
- __TRACE("pgm_save.log", "\n");
- }
-
- int dump4(int argc, char **argv)
- {
- const char *filename;
-
-
- avcodec_init();
-
-
-
- avcodec_register_all();
-
- if (argc <= 1) {
- audio_encode_example("/tmp/test.mp2");
- audio_decode_example("/tmp/test.sw", "/tmp/test.mp2");
-
- video_encode_example("/tmp/test.mpg");
- filename = "/tmp/test.mpg";
- } else {
- filename = argv[1];
- }
-
-
- video_decode_example("/tmp/test%d.pgm", filename);
-
- return 0;
- }