Advertisement
Guest User

ffmpeg valgrind test case

a guest
Jul 31st, 2013
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.01 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include <unistd.h>
  5.  
  6. #include <libavcodec/avcodec.h>
  7. #include <libavformat/avformat.h>
  8. #include <libswscale/swscale.h>
  9.  
  10. #define CONTAINER_FORMAT "matroska"
  11. #define VIDEO_FILENAME   "test.mkv"
  12. #define WIDTH            640
  13. #define HEIGHT           480
  14. #define PIX_FMT_IN       PIX_FMT_YUV420P
  15. #define PIX_FMT_OUT      PIX_FMT_RGB24
  16.  
  17. int
  18. main(void)
  19. {
  20.   AVInputFormat   *format;
  21.   AVFormatContext *format_ctx;
  22.   AVCodec         *codec, *image_codec;
  23.   AVCodecContext  *codec_ctx, *image_codec_ctx;
  24.  
  25.   avcodec_register_all();
  26.   av_register_all();
  27.  
  28.   format = av_find_input_format(CONTAINER_FORMAT);
  29.   if (format == NULL) return 1;
  30.  
  31.   format_ctx = avformat_alloc_context();
  32.   if (avformat_open_input(&format_ctx, VIDEO_FILENAME, format, NULL) != 0) return 1;
  33.  
  34.   codec_ctx = format_ctx->streams[0]->codec;
  35.  
  36.   codec = avcodec_find_decoder(codec_ctx->codec_id);
  37.   if (codec == NULL) return 1;
  38.  
  39.   if (codec->capabilities & CODEC_CAP_TRUNCATED) {
  40.     codec_ctx->flags |= CODEC_FLAG_TRUNCATED;
  41.   }
  42.  
  43.   if (avcodec_open2(codec_ctx, codec, NULL) != 0) return 1;
  44.  
  45.   image_codec = avcodec_find_encoder(AV_CODEC_ID_PNG);
  46.   if (image_codec == NULL) return 1;
  47.  
  48.   image_codec_ctx = avcodec_alloc_context3(image_codec);
  49.   if (image_codec_ctx == NULL) return 1;
  50.   image_codec_ctx->pix_fmt = AV_PIX_FMT_RGB24;
  51.   image_codec_ctx->width   = WIDTH;
  52.   image_codec_ctx->height  = HEIGHT;
  53.  
  54.   if (avcodec_open2(image_codec_ctx, image_codec, NULL) != 0) return 1;
  55.  
  56.   struct SwsContext *img_convert_ctx = sws_getContext(
  57.     WIDTH, HEIGHT, PIX_FMT_IN,
  58.     WIDTH, HEIGHT, PIX_FMT_OUT,
  59.     SWS_FAST_BILINEAR, NULL, NULL, NULL
  60.   );
  61.  
  62.   AVFrame *frame = avcodec_alloc_frame();
  63.  
  64.   int got_frame, got_packet;
  65.   AVPacket packet;
  66.  
  67.   av_init_packet(&packet);
  68.   packet.data = NULL;
  69.   packet.size = 0;
  70.  
  71.   AVFrame *frame2 = avcodec_alloc_frame();
  72.   int buf_size = avpicture_get_size(PIX_FMT_OUT, WIDTH, HEIGHT);
  73.   uint8_t *buf = malloc(buf_size);
  74.   avpicture_fill(
  75.     (AVPicture *)frame2,
  76.     buf,
  77.     PIX_FMT_OUT,
  78.     WIDTH, HEIGHT
  79.   );
  80.  
  81.   while (av_read_frame(format_ctx, &packet) >= 0) {
  82.     if (avcodec_decode_video2(codec_ctx, frame, &got_frame, &packet) < 0) return 1;
  83.  
  84.     if (got_frame) {
  85.       sws_scale(
  86.         img_convert_ctx, (const uint8_t * const *)frame->data, frame->linesize,
  87.         0, HEIGHT,
  88.         frame2->data, frame2->linesize
  89.       );
  90.  
  91.       av_init_packet(&packet);
  92.       packet.data = NULL;
  93.       packet.size = 0;
  94.  
  95.       /* If this next block is commented out, valgrind shows no invalid reads
  96.        * or use of uninitialized values.
  97.        */
  98.       if (avcodec_encode_video2(image_codec_ctx, &packet, frame2, &got_packet) < 0) return 1;
  99.       printf("### packet size: %d\n", packet.size);
  100.       if (got_packet) {
  101.         int fd = open("frame.png", O_CREAT|O_WRONLY|O_TRUNC, 0600);
  102.         write(fd, packet.data, packet.size);
  103.         close(fd);
  104.       }
  105.  
  106.       break; /* just process one frame */
  107.     }
  108.   }
  109.  
  110.   return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement