Advertisement
Guest User

linux v4l ffmpeg

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