Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2012
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.40 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <libavformat/avformat.h>
  4. #include <libavcodec/avcodec.h>
  5. #include <libavutil/avutil.h>
  6. #include <libavutil/mem.h>
  7. #include <libswscale/swscale.h>
  8.  
  9. int main()
  10. {
  11.     AVFormatContext *avfc;
  12.     AVCodecContext *codecctx;
  13.     struct SwsContext *swsctx;
  14.     AVCodec *codec;
  15.     AVFrame *pFrame;
  16.     AVFrame *pFrameRGB;
  17.     uint8_t *picbuf;
  18.     int numbytes;
  19.     int res = 0;
  20.     int frameFinished = 0;
  21.     int gotpic;
  22.     AVPacket pkt;
  23.     int i = 0;
  24.  
  25.     printf("Hello world!\n");
  26.  
  27.     char fname[100] = "out.avi";
  28.  
  29.     av_log_set_level(AV_LOG_DEBUG);
  30.     // Register
  31.     av_register_all();
  32.     // Alloc formatcontext and open input file
  33.     avfc = avformat_alloc_context();
  34.     res = avformat_open_input(&avfc, fname, NULL, NULL);
  35.     printf("%i\n", res);
  36.     // Populate streams (not always necessary
  37.     res = avformat_find_stream_info(avfc, NULL);
  38.     printf("%i\n", res);
  39.     // Open codec described the first stream (should search for first video stream though)
  40.     codecctx = avfc->streams[0]->codec;
  41.     codec = avcodec_find_decoder(codecctx->codec_id);
  42.     res = avcodec_open2(codecctx, codec, NULL);
  43.     printf("%i\n", res);
  44.  
  45.     av_dump_format(avfc, 0, fname, 0);
  46.     // Allocate frame and get next frame
  47.     pFrame = avcodec_alloc_frame();
  48.     pFrameRGB = avcodec_alloc_frame();
  49.     pFrameRGB->format = PIX_FMT_RGBA;
  50.     pFrameRGB->width = 512;
  51.     pFrameRGB->height = 512;
  52.     numbytes = avpicture_get_size(pFrameRGB->format, pFrameRGB->width, pFrameRGB->height);
  53.     picbuf = av_malloc(numbytes*sizeof(uint8_t));
  54.     avpicture_fill((AVPicture *) pFrameRGB, picbuf, pFrameRGB->format, pFrameRGB->width, pFrameRGB->height);
  55.  
  56.     av_init_packet(&pkt);
  57.  
  58.     int framenum = 0;
  59.     while (av_read_frame(avfc, &pkt)>= 0) {
  60.         pkt.flags = pkt.flags&AV_PKT_FLAG_KEY;
  61.         avcodec_decode_video2(codecctx, pFrame, &gotpic, &pkt);
  62.         printf("%d\n", gotpic);
  63.         if (gotpic) {
  64.             printf("Got frame.\n");
  65.             swsctx = sws_getContext(pFrame->width, pFrame->height, pFrame->format, pFrameRGB->width, pFrameRGB->height, pFrameRGB->format, SWS_BICUBIC, NULL, NULL, NULL);
  66.             sws_scale(swsctx, (const uint8_t * const*) pFrame->data, pFrame->linesize, 0, pFrame->height, pFrameRGB->data, pFrameRGB->linesize);
  67.  
  68.             AVCodec *encodec = avcodec_find_encoder(CODEC_ID_PNG);
  69.             AVCodecContext *avencctx = avcodec_alloc_context3(encodec);
  70.             avcodec_get_context_defaults3(avencctx, encodec);
  71.             avencctx->pix_fmt = PIX_FMT_RGBA;
  72.             avencctx->width = pFrameRGB->width;
  73.             avencctx->height = pFrameRGB->height;
  74.             int resenc = avcodec_open2(avencctx, avcodec_find_encoder(CODEC_ID_PNG), NULL);
  75.             AVPacket avpenc;
  76.             avpenc.data = NULL;
  77.             int gotpkt = 0;
  78.             if (avcodec_encode_video2(avencctx, &avpenc, pFrameRGB, &gotpkt) != 0) printf("Failed to encode.\n");
  79.             else {FILE *file = NULL;
  80.                 char fname[10];
  81.                 sprintf(fname, "%04d.raw", framenum);
  82.                 file = fopen(fname, "wb");
  83.                 if (file != NULL) {
  84.                     fwrite(avpenc.data, avpenc.size, 1, file);
  85.                 } else printf("Can't access file.\n");
  86.                 fclose(file);
  87.             }
  88.             framenum++;
  89.         }
  90.         av_free_packet(&pkt);
  91.  
  92.     }
  93.  
  94.     return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement