[Libav-user] Newbie help with C Library and error message while reading video

David martin vilanew at gmail.com
Tue Jan 10 14:07:04 CET 2012

I'm trying to build a basic program to read in  a video and display it. 
So far i'm blocked in the reading part. Here is the code and at the end 
you will find the program output

  * streamvideo.c
  *  Created on: Jan 6, 2012
  *      Author:

#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
#include <stdio.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <SFML/Graphics/RenderWindow.h>
#include <SFML/Audio.h>
#include <SFML/Graphics.h>

#define INBUF_SIZE 512

int main( int argc, char* argv[] )

	sfWindowSettings Settings = {24, 8, 0};
	     sfVideoMode Mode = {800, 600, 32};
	     sfRenderWindow* App;
	     sfImage* Image;
	     sfSprite* Sprite;
	     sfFont* Font;
	     sfString* Text;
	     sfMusic* Music;
	     sfEvent Event;

     AVFormatContext *pFormatCtx = NULL;
     AVCodecContext *pCodecCtx = NULL;
     AVCodec *pCodec = NULL;
     AVPacket        packet ;

     AVFrame         *pFrame;
     AVFrame         *pFrameYUV;

     int             iFrameSize;
     struct SwsContext *img_convert_ctx;
     int frameCount=0;
     int             frameFinished;
     int i, numBytes;
     uint8_t *buffer;

     //char* input_filename = "rtsp://wm-live.sr.se/SR-P3-High/";
     char* input_filename = "video.mpeg";

     // Initialize the library by registering all file formats and codecs so
     // they can be used automatically.

     printf("Registered file formats and codecs\n");

      * Getting ready to read the input.
     // Open the video file. The last three arguments to open() are 
     // buffer size, and format options, we let libavformat to select these.

     printf("Input name: %s\n", input_filename);

     if(avformat_open_input(&pFormatCtx, input_filename,NULL,NULL) != 0)
         fprintf(stderr, "could not open input: %s!\n", input_filename);
         return -1;

     printf("opened input\n");

         // Get stream information and dump it
         if (avformat_find_stream_info(pFormatCtx,NULL) <0)
         		fprintf(stderr, "could not find stream information\n");
             return -1;

     av_dump_format(pFormatCtx, 0, input_filename, 0 );

     printf("dump format done\n");

     // Find the first video stream
     int videoStream=-1;
     for(i=0; i<pFormatCtx->nb_streams; i++)
       if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
     	 printf("Found a video stream\n");
     	 printf("Didn't find a video stream\n");
       return -1; // Didn't find a video stream

     // Get a pointer to the codec context for the video stream

      // Find the decoder for the video stream
     if(pCodec==NULL) {
       fprintf(stderr, "Unsupported codec!\n");
       return -1; // Codec not found
     int height = pCodecCtx->height;
     int width = pCodecCtx->width;

     printf("video width: %d\n", pCodecCtx->width);
     printf("video height: %d\n", pCodecCtx->height);

     iFrameSize = pCodecCtx->width * pCodecCtx->height * 3;

     pCodecCtx = avcodec_alloc_context3(pCodec);
   //     if(pCodec->capabilities & CODEC_CAP_TRUNCATED) 

     printf("Frame size %d\n",iFrameSize);

     // Open codec

       if(avcodec_open2(pCodecCtx, pCodec,NULL)<0)
       printf("Could not open Codec");
       return -1; // Could not open codec
       numBytes=avpicture_get_size(PIX_FMT_YUV420P, width, height);
       //numBytes=avpicture_get_size(PIX_FMT_RGB24, pCodecCtx->width, 
       buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t));

        avpicture_fill((AVPicture *)pFrameYUV, buffer, PIX_FMT_YUV420P, 
pCodecCtx->width, pCodecCtx->height);

//One more thing left to do: Allocate a video frame to store the decoded 
images in:

       // Allocate video frame

       // Allocate an AVFrame structure
         return -1;

       // Determine required buffer size and allocate buffer

pCodecCtx->width, pCodecCtx->height);
              buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t));

              avpicture_fill((AVPicture *)pFrameYUV, buffer, 
                            pCodecCtx->width, pCodecCtx->height);

             // Data = new sf::Uint8[pCodecCtx->width * 
pCodecCtx->height * 4];


         //end video initialization

* Display video *

              int len =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_video2(pCodecCtx, pFrame, 
&frameFinished,packet.data, packet.size);

                  //len = avcodec_decode_video2(pCodecCtx, pFrame, 
pFrameRGB, &packet);
                  len = avcodec_decode_video2(pCodecCtx, pFrame, 
&frameFinished, &packet);
                  // Convert the image from its native format to RGB
                  avpicture_fill((AVPicture *)pFrameYUV, buffer, 
                              if (len < 0) {
                                  fprintf(stderr, "Error while decoding\n");

     return 0;

#Program execution

 > ./videostreaming
Registered file formats and codecs
Input name: video.mpeg
opened input
[mpeg @ 0x9972040] max_analyze_duration 5000000 reached at 5000000
Input #0, mpeg, from 'video.mpeg':
   Duration: 00:02:15.23, start: 1.000000, bitrate: 632 kb/s
     Stream #0:0[0x1e0]: Video: mpeg1video, yuv420p, 640x360 [SAR 1:1 
DAR 16:9], 104857 kb/s, 25 fps, 25 tbr, 90k tbn, 25 tbc
     Stream #0:1[0x1c0]: Audio: mp2, 44100 Hz, stereo, s16, 128 kb/s
dump format done
Found a video stream
video width: 640
video height: 360
Frame size 691200
[IMGUTILS @ 0xbfd09a24] Picture size 0x0 is invalid
[IMGUTILS @ 0xbfd09a14] Picture size 0x0 is invalid
[IMGUTILS @ 0xbfd09a24] Picture size 0x0 is invalid
[mpeg1video @ 0x9cc45e0] Warning MVs not available

What is the Picture size message about ?? can you help me ?


More information about the Libav-user mailing list