Advertisement
Guest User

Untitled

a guest
Jul 1st, 2011
450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.55 KB | None | 0 0
  1. /*
  2.  * open a v4l2 device and save one frame to test.jpeg
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <libavformat/avformat.h>
  7. #include <libavcodec/avcodec.h>
  8. #include <libavdevice/avdevice.h>
  9. #include <libavfilter/avcodec.h>
  10. #include <libavfilter/avfilter.h>
  11.  
  12. int main(int argc, char argv[])
  13. {
  14. // General
  15.     int bRet, ix;
  16. // Input
  17.     const char *sFile = "/dev/video6";
  18.     AVFormatContext *pIFormatCtx;
  19.     AVCodecContext *pICodecCtx;
  20.     AVCodec *pICodec;
  21.     AVFrame *pFrame;
  22.     int ixInputStream = -1;
  23.     AVFormatParameters oFormatParams;
  24.     AVInputFormat *pIFormat;
  25.     AVPacket oPacket;
  26.     int fFrame = 0;
  27. // Output
  28.     AVCodecContext *pOCodecCtx;
  29.     AVCodec *pOCodec;
  30.     uint8_t *pBuffer;
  31.     int szBuffer;
  32.     int szBufferActual;
  33.     int bImgFormat = PIX_FMT_YUVJ420P;
  34.     int bQuality = 3;
  35.     FILE *fdJPEG;
  36.  
  37. // Prepare ffmpeg library
  38.     av_register_all();
  39.     avdevice_register_all();
  40.     avfilter_register_all();
  41.  
  42. // Open video stream
  43.     memset(&oFormatParams,0,sizeof(AVFormatParameters));
  44.     oFormatParams.standard = "ntsc";
  45.     pIFormat = av_find_input_format("video4linux2");
  46.  
  47.     bRet = av_open_input_file(&pIFormatCtx, sFile, pIFormat, 0, &oFormatParams);
  48.  
  49.     printf("Abertura retornou %d\n",bRet);
  50.  
  51.     if (bRet != 0) {
  52.         fprintf(stderr, "Could not open file\n");
  53.         return 1;
  54.     }
  55.  
  56.     /* Retrieve stream information */
  57.     if (av_find_stream_info(pIFormatCtx) < 0) {
  58.         fprintf(stderr, "No stream info\n");
  59.         return 1;
  60.     }
  61.  
  62.     /* Find the first video stream */
  63.     ixInputStream = -1;
  64.     for (ix = 0; ix < pIFormatCtx->nb_streams; ix++) {
  65.         if (pIFormatCtx->streams[ix]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  66.             ixInputStream = ix;
  67.             break;
  68.         }
  69.     }
  70.     if (ixInputStream == -1) {
  71.         fprintf(stderr, "No video stream in file\n");
  72.         return 1;
  73.     }
  74.  
  75.     /* Get a pointer to the codec context for the video stream */
  76.     pICodecCtx = pIFormatCtx->streams[ixInputStream]->codec;
  77.  
  78.     /* Find the decoder for the video stream */
  79.     pICodec = avcodec_find_decoder(pICodecCtx->codec_id);
  80.     if (!pICodec) {
  81.         fprintf(stderr, "Codec not found\n");
  82.         return 1;
  83.     }
  84.  
  85.     /* Open input codec */
  86.     if (avcodec_open(pICodecCtx, pICodec) < 0) {
  87.         fprintf(stderr, "Could not open codec\n");
  88.         return 1;
  89.     }
  90.  
  91.     /* Allocate video frame */
  92.     pFrame = avcodec_alloc_frame();
  93.  
  94.     /* Determine required buffer size and allocate buffer */
  95.     szBuffer = avpicture_get_size(bImgFormat, pICodecCtx->width, pICodecCtx->height);
  96.     pBuffer = av_mallocz(szBuffer);
  97.  
  98.     /* Allocate Output Codec */
  99.     pOCodecCtx = avcodec_alloc_context();
  100.     if (!pOCodecCtx) {
  101.         fprintf(stderr, "Could not allocate codec\n");
  102.         return 1;
  103.     }
  104.  
  105.     /* Initialize picture size and other format parameters */
  106.     pOCodecCtx->bit_rate = pICodecCtx->bit_rate;
  107.     pOCodecCtx->width = pICodecCtx->width;
  108.     pOCodecCtx->height = pICodecCtx->height;
  109.     pOCodecCtx->pix_fmt = bImgFormat;
  110.     pOCodecCtx->codec_id = CODEC_ID_MJPEG;
  111.     pOCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
  112.     pOCodecCtx->time_base.num = pICodecCtx->time_base.num;
  113.     pOCodecCtx->time_base.den = pICodecCtx->time_base.den;
  114.  
  115.     /* Allocate codec for JPEG */
  116.     pOCodec = avcodec_find_encoder(pOCodecCtx->codec_id);
  117.     if (!pOCodec) {
  118.         fprintf(stderr, "Codec not found\n");
  119.         return 1;
  120.     }
  121.     if (avcodec_open(pOCodecCtx, pOCodec) < 0) {
  122.         fprintf(stderr, "Could not open codec\n");
  123.         return 1;
  124.     }
  125.  
  126.     /* Initialize all VBR settings */
  127.     pOCodecCtx->qmin = pOCodecCtx->qmax = bQuality;
  128.     pOCodecCtx->mb_lmin = pOCodecCtx->lmin = pOCodecCtx->qmin * FF_QP2LAMBDA;
  129.     pOCodecCtx->mb_lmax = pOCodecCtx->lmax = pOCodecCtx->qmax * FF_QP2LAMBDA;
  130.     pOCodecCtx->flags |= CODEC_FLAG_QSCALE;
  131.     pOCodecCtx->global_quality = pOCodecCtx->qmin * FF_QP2LAMBDA;
  132.  
  133.     /* Get 1 frame */
  134.     bRet = av_read_frame(pIFormatCtx, &oPacket);
  135.     if (bRet < 0) {
  136.         fprintf(stderr, "Could not read frame\n");
  137.         return 1;
  138.     }
  139.  
  140.     if (oPacket.stream_index != ixInputStream) {
  141.         fprintf(stderr, "Packet is not for our stream\n");
  142.         return 1;
  143.     }
  144.  
  145.     /* Decode video frame */
  146.     avcodec_decode_video2(pICodecCtx, pFrame, &fFrame,&oPacket);
  147.  
  148.     av_free_packet(&oPacket);
  149.     if (!fFrame) {
  150.         fprintf(stderr, "Error reading frame\n");
  151.         return 1;
  152.     }
  153.    
  154.     /* Filtro que coloca o tempo na imagem */
  155.     // AVFilter *filter = avfilter_get_by_name("drawtext");
  156.    
  157.    
  158.    
  159.     /* Encode the frame as a JPEG using certain quality settings */
  160.     pFrame->pts = 1;
  161.     pFrame->quality = pOCodecCtx->global_quality;
  162.     szBufferActual = avcodec_encode_video(pOCodecCtx, pBuffer, szBuffer, pFrame);
  163.  
  164.     /* Write JPEG to file */
  165.     fdJPEG = fopen("test.jpg", "wb");
  166.     bRet = fwrite(pBuffer, sizeof(uint8_t), szBufferActual, fdJPEG);
  167.     fclose(fdJPEG);
  168.     if (bRet != szBufferActual) {
  169.         fprintf(stderr, "Error writing jpeg file\n");
  170.         return 1;
  171.     }
  172.  
  173.     /* Cleanup */
  174.     if (pBuffer) {
  175.         av_freep(&pBuffer);
  176.         pBuffer = NULL;
  177.         szBuffer = 0;
  178.     }
  179.  
  180.     if (pFrame) {
  181.         av_freep(&pFrame);
  182.         pFrame = NULL;
  183.     }
  184.  
  185.     if (pICodecCtx) {
  186.         avcodec_close(pICodecCtx);
  187.         pICodecCtx = NULL;
  188.     }
  189.  
  190.     if (pOCodecCtx) {
  191.         avcodec_close(pOCodecCtx);
  192.         pOCodecCtx = NULL;
  193.     }
  194.  
  195.     if (pIFormatCtx) {
  196.         av_close_input_file(pIFormatCtx);
  197.         pIFormatCtx = NULL;
  198.     }
  199.  
  200.     return 0;
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement