Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2644544
  • 博文数量: 416
  • 博客积分: 10220
  • 博客等级: 上将
  • 技术积分: 4193
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-15 09:47
文章分类

全部博文(416)

文章存档

2022年(1)

2021年(1)

2020年(1)

2019年(5)

2018年(7)

2017年(6)

2016年(7)

2015年(11)

2014年(1)

2012年(5)

2011年(7)

2010年(35)

2009年(64)

2008年(48)

2007年(177)

2006年(40)

我的朋友

分类: C/C++

2009-12-03 17:44:42

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; inb_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;
}

 
  1. /****************************************/
    #include     
  2. #include     
  3. #include     
  4. #include     
  5.    
  6. #ifdef HAVE_AV_CONFIG_H    
  7. #undef HAVE_AV_CONFIG_H    
  8. #endif    
  9.    
  10. #include "ffmpeg\avcodec.h"    
  11. #include "Logger.h"    
  12.    
  13. #define INBUF_SIZE 4096    
  14.    
  15. /*  
  16. * Audio encoding example  
  17. */   
  18. void audio_encode_example(const char *filename)   
  19. {   
  20.     AVCodec *codec;   
  21.     AVCodecContext *c= NULL;   
  22.     int frame_size, i, j, out_size, outbuf_size;   
  23.     FILE *f;   
  24.     short *samples;   
  25.     float t, tincr;   
  26.     uint8_t *outbuf;   
  27.    
  28.     __TRACE("audio_encode_example.log""Audio encoding\n");   
  29.    
  30.     /* find the MP2 encoder */   
  31.     codec = avcodec_find_encoder(CODEC_ID_MP2);   
  32.     if (!codec) {   
  33.         __TRACE("audio_encode_example.log""codec not found\n");   
  34.         exit(1);   
  35.     }   
  36.    
  37.     c= avcodec_alloc_context();   
  38.    
  39.     /* put sample parameters */   
  40.     c->bit_rate = 64000;   
  41.     c->sample_rate = 44100;   
  42.     c->channels = 2;   
  43.    
  44.     /* open it */   
  45.     if (avcodec_open(c, codec) < 0) {   
  46.         __TRACE("audio_encode_example.log""could not open codec\n");   
  47.         exit(1);   
  48.     }   
  49.    
  50.     /* the codec gives us the frame size, in samples */   
  51.     frame_size = c->frame_size;   
  52.     samples = (short*)malloc(frame_size * 2 * c->channels);   
  53.     outbuf_size = 10000;   
  54.     outbuf = (uint8_t*)malloc(outbuf_size);   
  55.    
  56.     f = fopen(filename, "wb");   
  57.     if (!f) {   
  58.         __TRACE("audio_encode_example.log""could not open %s\n", filename);   
  59.         exit(1);   
  60.     }   
  61.    
  62.     /* encode a single tone sound */   
  63.     t = 0;   
  64.     tincr = 2 * M_PI * 440.0 / c->sample_rate;   
  65.     for(i=0;i<200;i++) {   
  66.         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) {   
  67.             len = avcodec_decode_audio(c, (short *)outbuf, &out_size,   
  68.                 inbuf_ptr, size);   
  69.             if (len < 0) {   
  70.                 fprintf(stderr, "Error while decoding\n");   
  71.                 exit(1);   
  72.             }   
  73.             if (out_size > 0) {   
  74.                 /* if a frame has been decoded, output it */   
  75.                 fwrite(outbuf, 1, out_size, outfile);   
  76.             }   
  77.             size -= len;   
  78.             inbuf_ptr += len;   
  79.         }   
  80.     }   
  81.    
  82.     fclose(outfile);   
  83.     fclose(f);   
  84.     free(outbuf);   
  85.    
  86.     avcodec_close(c);   
  87.     av_free(c);   
  88. }   
  89.    
  90. /*  
  91. * Video encoding example  
  92. */   
  93. void video_encode_example(const char *filename)   
  94. {   
  95.     AVCodec *codec;   
  96.     AVCodecContext *c= NULL;   
  97.     int i, out_size, size, x, y, outbuf_size;   
  98.     FILE *f;   
  99.     AVFrame *picture;   
  100.     uint8_t *outbuf, *picture_buf;   
  101.    
  102.     __TRACE("video_encode_example.log""Video encoding\n");   
  103.    
  104.     /* find the mpeg1 video encoder */   
  105.     codec = avcodec_find_encoder(CODEC_ID_MPEG1VIDEO);   
  106.     if (!codec) {   
  107.         fprintf(stderr, "codec not found\n");   
  108.         exit(1);   
  109.     }   
  110.    
  111.     c= avcodec_alloc_context();   
  112.     picture= avcodec_alloc_frame();   
  113.    
  114.     /* put sample parameters */   
  115.     c->bit_rate = 400000;   
  116.     /* resolution must be a multiple of two */   
  117.     c->width = 352;   
  118.     c->height = 288;   
  119.     /* frames per second */   
  120.     //c->time_base= (AVRational){1,25};    
  121.     c->time_base.num = 1;   
  122.     c->time_base.den = 25;   
  123.    
  124.     c->gop_size = 10; /* emit one intra frame every ten frames */   
  125.     c->max_b_frames=1;   
  126.     c->pix_fmt = PIX_FMT_YUV420P;   
  127.    
  128.     /* open it */   
  129.     if (avcodec_open(c, codec) < 0) {   
  130.         __TRACE("video_encode_example.log""could not open codec\n");   
  131.         exit(1);   
  132.     }   
  133.    
  134.     /* the codec gives us the frame size, in samples */   
  135.    
  136.     f = fopen(filename, "wb");   
  137.     if (!f) {   
  138.         __TRACE("video_encode_example.log""could not open %s\n", filename);   
  139.         exit(1);   
  140.     }   
  141.    
  142.     /* alloc image and output buffer */   
  143.     outbuf_size = 100000;   
  144.     outbuf = (uint8_t*)malloc(outbuf_size);   
  145.     size = c->width * c->height;   
  146.     picture_buf = (uint8_t*)malloc((size * 3) / 2); /* size for YUV 420 */   
  147.    
  148.     picture->data[0] = picture_buf;   
  149.     picture->data[1] = picture->data[0] + size;   
  150.     picture->data[2] = picture->data[1] + size / 4;   
  151.     picture->linesize[0] = c->width;   
  152.     picture->linesize[1] = c->width / 2;   
  153.     picture->linesize[2] = c->width / 2;   
  154.    
  155.     /* encode 1 second of video */   
  156.     for(i=0;i<25;i++) {   
  157.         fflush(stdout);   
  158.         /* prepare a dummy image */   
  159.         /* Y */   
  160.         for(y=0;yheight;y++) {   
  161.             for(x=0;xwidth;x++) {   
  162.                 picture->data[0][y * picture->linesize[0] + x] = x + y + i * 3;   
  163.             }   
  164.         }   
  165.    
  166.         /* Cb and Cr */   
  167.         for(y=0;yheight/2;y++) {   
  168.             for(x=0;xwidth/2;x++) {   
  169.                 picture->data[1][y * picture->linesize[1] + x] = 128 + y + i * 2;   
  170.                 picture->data[2][y * picture->linesize[2] + x] = 64 + x + i * 5;   
  171.             }   
  172.         }   
  173.    
  174.         /* encode the image */   
  175.         out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture);   
  176.         __TRACE("video_encode_example.log""encoding frame %3d (size=%5d)\n", i, out_size);   
  177.         fwrite(outbuf, 1, out_size, f);   
  178.     }   
  179.    
  180.     /* get the delayed frames */   
  181.     for(; out_size; i++) {   
  182.         fflush(stdout);   
  183.    
  184.         out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);   
  185.         __TRACE("video_encode_example.log""write frame %3d (size=%5d)\n", i, out_size);   
  186.         fwrite(outbuf, 1, out_size, f);   
  187.     }   
  188.    
  189.     /* add sequence end code to have a real mpeg file */   
  190.     outbuf[0] = 0x00;   
  191.     outbuf[1] = 0x00;   
  192.     outbuf[2] = 0x01;   
  193.     outbuf[3] = 0xb7;   
  194.     fwrite(outbuf, 1, 4, f);   
  195.     fclose(f);   
  196.     free(picture_buf);   
  197.     free(outbuf);   
  198.    
  199.     avcodec_close(c);   
  200.     av_free(c);   
  201.     av_free(picture);   
  202.     __TRACE("video_encode_example.log""\n");   
  203. }   
  204.    
  205. /*  
  206. * Video decoding example  
  207. */   
  208.    
  209. void pgm_save(unsigned char *buf,int wrap, int xsize,int ysize,char *filename)   
  210. {   
  211.     FILE *f;   
  212.     int i;   
  213.    
  214.     f=fopen(filename,"w");   
  215.     fprintf(f,"P5\n%d %d\n%d\n",xsize,ysize,255);   
  216.     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)   
  217.         c->flags|= CODEC_FLAG_TRUNCATED; /* we dont send complete frames */   
  218.    
  219.     /* for some codecs, such as msmpeg4 and mpeg4, width and height  
  220.     MUST be initialized there because these info are not available  
  221.     in the bitstream */   
  222.    
  223.     /* open it */   
  224.     if (avcodec_open(c, codec) < 0) {   
  225.         __TRACE("pgm_save.log""could not open codec\n");   
  226.         exit(1);   
  227.     }   
  228.    
  229.     /* the codec gives us the frame size, in samples */   
  230.    
  231.     f = fopen(filename, "rb");   
  232.     if (!f) {   
  233.         __TRACE("pgm_save.log""could not open %s\n", filename);   
  234.         exit(1);   
  235.     }   
  236.    
  237.     frame = 0;   
  238.     for(;;) {   
  239.         size = fread(inbuf, 1, INBUF_SIZE, f);   
  240.         if (size == 0)   
  241.             break;   
  242.    
  243.         /* NOTE1: some codecs are stream based (mpegvideo, mpegaudio)  
  244.         and this is the only method to use them because you cannot  
  245.         know the compressed data size before analysing it.  
  246.   
  247.         BUT some other codecs (msmpeg4, mpeg4) are inherently frame  
  248.         based, so you must call them with all the data for one  
  249.         frame exactly. You must also initialize 'width' and  
  250.         'height' before initializing them. */   
  251.    
  252.         /* NOTE2: some codecs allow the raw parameters (frame size,  
  253.         sample rate) to be changed at any frame. We handle this, so  
  254.         you should also take care of it */   
  255.    
  256.         /* here, we use a stream based decoder (mpeg1video), so we  
  257.         feed decoder and see if it could decode a frame */   
  258.         inbuf_ptr = inbuf;   
  259.         while (size > 0) {   
  260.             len = avcodec_decode_video(c, picture, &got_picture,   
  261.                 inbuf_ptr, size);   
  262.             if (len < 0) {   
  263.                 __TRACE("pgm_save.log""Error while decoding frame %d\n", frame);   
  264.                 exit(1);   
  265.             }   
  266.             if (got_picture) {   
  267.                 __TRACE("pgm_save.log""saving frame %3d\n", frame);   
  268.    
  269.                 /* the picture is allocated by the decoder. no need to  
  270.                 free it */   
  271.                 _snprintf(buf, sizeof(buf), outfilename, frame);   
  272.                 pgm_save(picture->data[0], picture->linesize[0],   
  273.                     c->width, c->height, buf);   
  274.                 frame++;   
  275.             }   
  276.             size -= len;   
  277.             inbuf_ptr += len;   
  278.         }   
  279.     }   
  280.    
  281.     /* some codecs, such as MPEG, transmit the I and P frame with a  
  282.     latency of one frame. You must do the following to have a  
  283.     chance to get the last frame of the video */   
  284.     len = avcodec_decode_video(c, picture, &got_picture,   
  285.         NULL, 0);   
  286.     if (got_picture) {   
  287.         __TRACE("pgm_save.log""saving last frame %3d\n", frame);   
  288.    
  289.         /* the picture is allocated by the decoder. no need to  
  290.         free it */   
  291.         _snprintf(buf, sizeof(buf), outfilename, frame);   
  292.         pgm_save(picture->data[0], picture->linesize[0],   
  293.             c->width, c->height, buf);   
  294.         frame++;   
  295.     }   
  296.    
  297.     fclose(f);   
  298.    
  299.     avcodec_close(c);   
  300.     av_free(c);   
  301.     av_free(picture);   
  302.     __TRACE("pgm_save.log""\n");   
  303. }   
  304.    
  305. int dump4(int argc, char **argv)   
  306. {   
  307.     const char *filename;   
  308.    
  309.     /* must be called before using avcodec lib */   
  310.     avcodec_init();   
  311.    
  312.     /* register all the codecs (you can also register only the codec  
  313.     you wish to have smaller code */   
  314.     avcodec_register_all();   
  315.    
  316.     if (argc <= 1) {   
  317.         audio_encode_example("/tmp/test.mp2");   
  318.         audio_decode_example("/tmp/test.sw""/tmp/test.mp2");   
  319.    
  320.         video_encode_example("/tmp/test.mpg");   
  321.         filename = "/tmp/test.mpg";   
  322.     } else {   
  323.         filename = argv[1];   
  324.     }   
  325.    
  326.     //    audio_decode_example("/tmp/test.sw", filename);    
  327.     video_decode_example("/tmp/test%d.pgm", filename);   
  328.    
  329.     return 0;   
  330. }   
阅读(1952) | 评论(1) | 转发(0) |
0

上一篇:健康知识

下一篇:编译libdvdnav 的menus.c

给主人留下些什么吧!~~

chinaunix网友2009-12-09 00:14:04

http://ubuntu-ky.ubuntuforums.org/showthread.php?t=1304737