Home/News | About | Download | Documentation | Forum | Bug Reports | Contact | Donations | Consulting | Projects | Legal | Security | FATE


This forum has not been maintained for a long time and will probably get deleted in the near future.
For faster responses to your questions, please use StackOverflow instead and tag your questions with "FFmpeg".
If you need a backup of the posts from this forum, please contact me directly.

Error while encoding H264 videos from RTSP video

Who doesn't love them :) But for faster response on your Bug Reports, please visit: http://www.ffmpeg.org/bugreports.html
Forum rules
First of all, please remember that all the developers working on FFmpeg are volunteers, we are not paid for analyzing, debugging, fixing and answering bug reports. While we try our best to create high quality software and be responsive, if you should not get an answer, please consider the above and be aware of the fact that we receive many bug reports and the amount of time it takes to deal with them can be substantial. Nevertheless most issues get resolved, just please understand that there are no guarantees nor promises.

Error while encoding H264 videos from RTSP video

Postby Nabil » Fri May 15, 2015 11:00 am

Dear all,

I am using the C API to capture rtsp stream. I keep getting this error while playing the video
error while decoding MB 25 34, bytestream
I am using opencv to display the frames on the screen.

Code: Select all
#include <stdio.h>
#include "opencv2/opencv.hpp"

extern "C"
{
  #include <libavcodec/avcodec.h>
  #include <libavformat/avformat.h>
  #include <libswscale/swscale.h>
}

using namespace cv;

int main(int argc, char *argv[]) {
  Mat frame_cv;
  AVFormatContext *pFormatCtx = NULL;
  int             i, videoStream;
  AVCodecContext  *pCodecCtx = NULL;
  AVCodec         *pCodec = NULL;
  AVFrame         *pFrame = NULL;
  AVFrame         *pFrameRGB = NULL;
  AVPacket        packet;
  int             frameFinished;
  int             numBytes;
  uint8_t         *buffer = NULL;

  AVDictionary    *optionsDict = NULL;
  struct SwsContext      *sws_ctx = NULL;
 
  if(argc < 2) {
    printf("Please provide a movie file\n");
    return -1;
  }
  // Register all formats and codecs
  av_register_all();
 
  // Open video file
  if(avformat_open_input(&pFormatCtx, argv[1], NULL, NULL)!=0)
    return -1; // Couldn't open file
 
  // Retrieve stream information
  if(avformat_find_stream_info(pFormatCtx, NULL)<0)
    return -1; // Couldn't find stream information
 
  // Dump information about file onto standard error
  av_dump_format(pFormatCtx, 0, argv[1], 0);
 
  // Find the first video stream
  videoStream=-1;
  for(i=0; i<pFormatCtx->nb_streams; i++)
    if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
      videoStream=i;
      break;
    }
  if(videoStream==-1)
    return -1; // Didn't find a video stream
 
  // Get a pointer to the codec context for the video stream
  pCodecCtx=pFormatCtx->streams[videoStream]->codec;
 
  // Find the decoder for the video stream
  pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
  if(pCodec==NULL) {
    fprintf(stderr, "Unsupported codec!\n");
    return -1; // Codec not found
  }
  // Open codec
  if(avcodec_open2(pCodecCtx, pCodec, &optionsDict)<0)
    return -1; // Could not open codec
 
  // Allocate video frame
  pFrame=avcodec_alloc_frame();
 
  // Allocate an AVFrame structure
  pFrameRGB=avcodec_alloc_frame();
  if(pFrameRGB==NULL)
    return -1;
 
  // Determine required buffer size and allocate buffer
  numBytes=avpicture_get_size(PIX_FMT_BGR24, pCodecCtx->width,
               pCodecCtx->height);
  buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t));

  sws_ctx =
    sws_getContext
    (
        pCodecCtx->width,
        pCodecCtx->height,
        pCodecCtx->pix_fmt,
        pCodecCtx->width,
        pCodecCtx->height,
        PIX_FMT_BGR24,
        SWS_BILINEAR,
        NULL,
        NULL,
        NULL
    );
 
  // Assign appropriate parts of buffer to image planes in pFrameRGB
  // Note that pFrameRGB is an AVFrame, but AVFrame is a superset
  // of AVPicture
  avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_BGR24,
       pCodecCtx->width, pCodecCtx->height);
  frame_cv.create(pCodecCtx->height, pCodecCtx->width, CV_8UC(3));
  // Read frames and save first five frames to disk
  i=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);
     
      // Did we get a video frame?
      if(frameFinished) {
   // Convert the image from its native format to RGB
        sws_scale
        (
            sws_ctx,
            (uint8_t const * const *)pFrame->data,
            pFrame->linesize,
            0,
            pCodecCtx->height,
            pFrameRGB->data,
            pFrameRGB->linesize
        );
        /***** Populate the OpenCV frame *****/
        for (int y=0; y<pCodecCtx->height; y++)
        {
          for (int x=0; x<pCodecCtx->width; x++)
          {
            frame_cv.at<Vec3b>(y,x)[0] = pFrameRGB->data[0][y * pFrameRGB->linesize[0] + x * 3 + 0];
            frame_cv.at<Vec3b>(y,x)[1] = pFrameRGB->data[0][y * pFrameRGB->linesize[0] + x * 3 + 1];
            frame_cv.at<Vec3b>(y,x)[2] = pFrameRGB->data[0][y * pFrameRGB->linesize[0] + x * 3 + 2];
          }
        }

      }
    }
    imshow("CAM1", frame_cv);
    if (waitKey(15) >= 10) break;
    // Free the packet that was allocated by av_read_frame
    av_free_packet(&packet);
  }
 
  // Free the RGB image
  av_free(buffer);
  av_free(pFrameRGB);
 
  // Free the YUV frame
  av_free(pFrame);
 
  // Close the codec
  avcodec_close(pCodecCtx);
 
  // Close the video file
  avformat_close_input(&pFormatCtx);
 
  return 0;
}


Thank you for your help
Nabil
 
Posts: 1
Joined: Fri May 15, 2015 10:34 am

Re: Error while encoding H264 videos from RTSP video

Postby llogan » Fri May 15, 2015 7:59 pm

You should ask the libav-user mailing list. It is the mailing list for FFmpeg library usage questions.
Get an answer more quickly by providing your actual ffmpeg command and the complete console output. Use the code button to format your command and output or your question will be ignored.
llogan
 
Posts: 2360
Joined: Fri Jan 25, 2013 9:47 pm
Location: Alaska


Return to Bugs, bugs, bugs :)

Who is online

Users browsing this forum: No registered users and 0 guests