Opened 6 years ago

#7152 new defect

avformat_find_stream_info() doesn't work as expected

Reported by: whs Owned by:
Priority: normal Component: undetermined
Version: unspecified Keywords:
Cc: Blocked By:
Blocking: Reproduced by developer: no
Analyzed by developer: no

Description

I downloaded the snapshot of ffmpeg.
I compiled and installed it.

Then, I download the example on http://dranger.com/ffmpeg/tutorial01.html
With some change the code as follow

// tutorial01.c
// Code based on a tutorial by Martin Bohme (boehme@inb.uni-luebeckREMOVETHIS.de)
// Tested on Gentoo, CVS version 5/01/07 compiled with GCC 4.1.1
// With updates from https://github.com/chelyaev/ffmpeg-tutorial
// Updates tested on:
// LAVC 54.59.100, LAVF 54.29.104, LSWS 2.1.101 
// on GCC 4.7.2 in Debian February 2015

// A small sample program that shows how to use libavformat and libavcodec to
// read video from a file.
//
// Use
//
// gcc -o tutorial01 tutorial01.c -lavformat -lavcodec -lswscale -lz
//
// to build (assuming libavformat and libavcodec are correctly installed
// your system).
//
// Run using
//
// tutorial01 myvideofile.mpg
//
// to write the first five frames from "myvideofile.mpg" to disk in PPM
// format.

#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>

#include <stdio.h>

// compatibility with newer API
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55,28,1)
#define av_frame_alloc avcodec_alloc_frame
#define av_frame_free avcodec_free_frame
#endif

void SaveFrame(AVFrame *pFrame, int width, int height, int iFrame) {
  FILE *pFile;
  char szFilename[32];
  int  y;
  
  // Open file
  sprintf(szFilename, "frame%d.ppm", iFrame);
  pFile=fopen(szFilename, "wb");
  if(pFile==NULL)
    return;
  
  // Write header
  fprintf(pFile, "P6\n%d %d\n255\n", width, height);
  
  // Write pixel data
  for(y=0; y<height; y++)
    fwrite(pFrame->data[0]+y*pFrame->linesize[0], 1, width*3, pFile);
  
  // Close file
  fclose(pFile);
}

int main(int argc, char *argv[]) {
  // Initalizing these to NULL prevents segfaults!
  AVFormatContext   *pFormatCtx = NULL;
  int               i, videoStream;
  AVCodecContext    *pCodecCtxOrig = NULL;
  AVCodecContext    *pCodecCtx = NULL;
  AVCodec           *pCodec = NULL;
  AVFrame           *pFrame = NULL;
  AVFrame           *pFrameRGB = NULL;
  AVPacket          packet;
  int               frameFinished;
  int               numBytes;
  uint8_t           *buffer = 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
  pCodecCtxOrig=pFormatCtx->streams[videoStream]->codecpar;
  // Find the decoder for the video stream
  pCodec=avcodec_find_decoder(pCodecCtxOrig->codec_id);
  if(pCodec==NULL) {
    fprintf(stderr, "Unsupported codec!\n");
    return -1; // Codec not found
  }
  // Copy context
  pCodecCtx = avcodec_alloc_context3(pCodec);
  if(avcodec_copy_context(pCodecCtx, pCodecCtxOrig) != 0) {
    fprintf(stderr, "Couldn't copy codec context");
    return -1; // Error copying codec context
  }

  // Open codec
  if(avcodec_open2(pCodecCtx, pCodec, NULL)<0)
    return -1; // Could not open codec
  
  // Allocate video frame
  pFrame=av_frame_alloc();
  
  // Allocate an AVFrame structure
  pFrameRGB=av_frame_alloc();
  if(pFrameRGB==NULL)
    return -1;

  // Determine required buffer size and allocate buffer
  numBytes=avpicture_get_size(AV_PIX_FMT_RGB24, pCodecCtx->width,
			      pCodecCtx->height);
  buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t));
  
  // 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, AV_PIX_FMT_RGB24,
		 pCodecCtx->width, pCodecCtx->height);
  
  // initialize SWS context for software scaling
  sws_ctx = sws_getContext(pCodecCtx->width,
			   pCodecCtx->height,
			   pCodecCtx->pix_fmt,
			   pCodecCtx->width,
			   pCodecCtx->height,
			   AV_PIX_FMT_RGB24,
			   SWS_BILINEAR,
			   NULL,
			   NULL,
			   NULL
			   );

  // 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);
	
	// Save the frame to disk
	if(++i<=5)
	  SaveFrame(pFrameRGB, pCodecCtx->width, pCodecCtx->height, 
		    i);
      }
    }
    
    // Free the packet that was allocated by av_read_frame
    av_free_packet(&packet);
  }
  
  // Free the RGB image
  av_free(buffer);
  av_frame_free(&pFrameRGB);
  
  // Free the YUV frame
  av_frame_free(&pFrame);
  
  // Close the codecs
  avcodec_close(pCodecCtx);
  avcodec_close(pCodecCtxOrig);

  // Close the video file
  avformat_close_input(&pFormatCtx);
  
  return 0;
}

I then compile it with

gcc tutorial01.c -lavformat -lavcodec -lavutil -lswscale

When I run the

./a.out '/home/whs/Videos/The_Last_Jedi/The_Last_Jedi_t00.mkv'

I got the error

[matroska,webm @ 0xc0aaa0] Could not find codec parameters for stream 4 (Subtitle: hdmv_pgs_subtitle (pgssub)): unspecified size
Consider increasing the value for the 'analyzeduration' and 'probesize' options
[matroska,webm @ 0xc0aaa0] Could not find codec parameters for stream 5 (Subtitle: hdmv_pgs_subtitle (pgssub)): unspecified size
Consider increasing the value for the 'analyzeduration' and 'probesize' options
[matroska,webm @ 0xc0aaa0] Could not find codec parameters for stream 6 (Subtitle: hdmv_pgs_subtitle (pgssub)): unspecified size
Consider increasing the value for the 'analyzeduration' and 'probesize' options
[matroska,webm @ 0xc0aaa0] Could not find codec parameters for stream 7 (Subtitle: hdmv_pgs_subtitle (pgssub)): unspecified size
Consider increasing the value for the 'analyzeduration' and 'probesize' options
[matroska,webm @ 0xc0aaa0] Could not find codec parameters for stream 8 (Subtitle: hdmv_pgs_subtitle (pgssub)): unspecified size
Consider increasing the value for the 'analyzeduration' and 'probesize' options
[matroska,webm @ 0xc0aaa0] Could not find codec parameters for stream 9 (Subtitle: hdmv_pgs_subtitle (pgssub)): unspecified size
Consider increasing the value for the 'analyzeduration' and 'probesize' options
Input #0, matroska,webm, from '/home/whs/Videos/The_Last_Jedi/The_Last_Jedi_t00.mkv':
  Metadata:
    title           : The Last Jedi
    encoder         : libmakemkv v1.12.0 (1.3.5/1.4.7) x86_64-linux-gnu
    creation_time   : 2018-04-19 12:36:00
  Duration: 02:31:51.60, start: 0.000000, bitrate: 33756 kb/s
    Chapter #0:0: start 0.000000, end 18.643625
    Metadata:
      title           : Chapter 01
    Chapter #0:1: start 18.643625, end 105.730625
    Metadata:
      title           : Chapter 02
    Chapter #0:2: start 105.730625, end 191.816625
    Metadata:
      title           : Chapter 03
    Chapter #0:3: start 191.816625, end 463.296167
    Metadata:
      title           : Chapter 04
    Chapter #0:4: start 463.296167, end 768.434333
    Metadata:
      title           : Chapter 05
    Chapter #0:5: start 768.434333, end 824.573750
    Metadata:
      title           : Chapter 06
    Chapter #0:6: start 824.573750, end 989.279958
    Metadata:
      title           : Chapter 07
    Chapter #0:7: start 989.279958, end 1199.281417
    Metadata:
      title           : Chapter 08
    Chapter #0:8: start 1199.281417, end 1371.370000
    Metadata:
      title           : Chapter 09
    Chapter #0:9: start 1371.370000, end 1564.980083
    Metadata:
      title           : Chapter 10
    Chapter #0:10: start 1564.980083, end 1642.515875
    Metadata:
      title           : Chapter 11
    Chapter #0:11: start 1642.515875, end 1884.757875
    Metadata:
      title           : Chapter 12
    Chapter #0:12: start 1884.757875, end 1974.681042
    Metadata:
      title           : Chapter 13
    Chapter #0:13: start 1974.681042, end 2127.667208
    Metadata:
      title           : Chapter 14
    Chapter #0:14: start 2127.667208, end 2291.497542
    Metadata:
      title           : Chapter 15
    Chapter #0:15: start 2291.497542, end 2513.427583
    Metadata:
      title           : Chapter 16
    Chapter #0:16: start 2513.427583, end 2669.541875
    Metadata:
      title           : Chapter 17
    Chapter #0:17: start 2669.541875, end 2811.475333
    Metadata:
      title           : Chapter 18
    Chapter #0:18: start 2811.475333, end 3101.932167
    Metadata:
      title           : Chapter 19
    Chapter #0:19: start 3101.932167, end 3230.060167
    Metadata:
      title           : Chapter 20
    Chapter #0:20: start 3230.060167, end 3341.504833
    Metadata:
      title           : Chapter 21
    Chapter #0:21: start 3341.504833, end 3459.581125
    Metadata:
      title           : Chapter 22
    Chapter #0:22: start 3459.581125, end 3556.636417
    Metadata:
      title           : Chapter 23
    Chapter #0:23: start 3556.636417, end 3725.847125
    Metadata:
      title           : Chapter 24
    Chapter #0:24: start 3725.847125, end 3787.700583
    Metadata:
      title           : Chapter 25
    Chapter #0:25: start 3787.700583, end 3941.729458
    Metadata:
      title           : Chapter 26
    Chapter #0:26: start 3941.729458, end 4023.477792
    Metadata:
      title           : Chapter 27
    Chapter #0:27: start 4023.477792, end 4225.721500
    Metadata:
      title           : Chapter 28
    Chapter #0:28: start 4225.721500, end 4410.322583
    Metadata:
      title           : Chapter 29
    Chapter #0:29: start 4410.322583, end 4663.450458
    Metadata:
      title           : Chapter 30
    Chapter #0:30: start 4663.450458, end 4853.056542
    Metadata:
      title           : Chapter 31
    Chapter #0:31: start 4853.056542, end 5057.969583
    Metadata:
      title           : Chapter 32
    Chapter #0:32: start 5057.969583, end 5183.219708
    Metadata:
      title           : Chapter 33
    Chapter #0:33: start 5183.219708, end 5279.524250
    Metadata:
      title           : Chapter 34
    Chapter #0:34: start 5279.524250, end 5449.360583
    Metadata:
      title           : Chapter 35
    Chapter #0:35: start 5449.360583, end 5574.569000
    Metadata:
      title           : Chapter 36
    Chapter #0:36: start 5574.569000, end 5753.706292
    Metadata:
      title           : Chapter 37
    Chapter #0:37: start 5753.706292, end 5830.616458
    Metadata:
      title           : Chapter 38
    Chapter #0:38: start 5830.616458, end 5952.863583
    Metadata:
      title           : Chapter 39
    Chapter #0:39: start 5952.863583, end 6127.663208
    Metadata:
      title           : Chapter 40
    Chapter #0:40: start 6127.663208, end 6303.714083
    Metadata:
      title           : Chapter 41
    Chapter #0:41: start 6303.714083, end 6575.694125
    Metadata:
      title           : Chapter 42
    Chapter #0:42: start 6575.694125, end 6740.608875
    Metadata:
      title           : Chapter 43
    Chapter #0:43: start 6740.608875, end 6964.749458
    Metadata:
      title           : Chapter 44
    Chapter #0:44: start 6964.749458, end 7132.083292
    Metadata:
      title           : Chapter 45
    Chapter #0:45: start 7132.083292, end 7391.133750
    Metadata:
      title           : Chapter 46
    Chapter #0:46: start 7391.133750, end 7599.758833
    Metadata:
      title           : Chapter 47
    Chapter #0:47: start 7599.758833, end 7903.604042
    Metadata:
      title           : Chapter 48
    Chapter #0:48: start 7903.604042, end 8112.104000
    Metadata:
      title           : Chapter 49
    Chapter #0:49: start 8112.104000, end 8416.699958
    Metadata:
      title           : Chapter 50
    Chapter #0:50: start 8416.699958, end 8619.694417
    Metadata:
      title           : Chapter 51
    Chapter #0:51: start 8619.694417, end 9111.602500
    Metadata:
      title           : Chapter 52
    Stream #0:0(eng): Video: h264 (High), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], 23.98 fps, 23.98 tbr, 1k tbn, 47.95 tbc
    Metadata:
      BPS-eng         : 30705362
      DURATION-eng    : 02:31:51.602500000
      NUMBER_OF_FRAMES-eng: 218460
      NUMBER_OF_BYTES-eng: 34971880054
      SOURCE_ID-eng   : 001011
      _STATISTICS_WRITING_APP-eng: MakeMKV v1.12.0 linux(x64-release)
      _STATISTICS_WRITING_DATE_UTC-eng: 2018-04-19 12:36:00
      _STATISTICS_TAGS-eng: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES SOURCE_ID
    Stream #0:1(eng): Audio: dts (DTS), 48000 Hz, 5.1(side), fltp, 1536 kb/s (default)
    Metadata:
      title           : Surround 5.1
      BPS-eng         : 1509000
      DURATION-eng    : 02:31:51.616000000
      NUMBER_OF_FRAMES-eng: 854214
      NUMBER_OF_BYTES-eng: 1718678568
      SOURCE_ID-eng   : 001100
      _STATISTICS_WRITING_APP-eng: MakeMKV v1.12.0 linux(x64-release)
      _STATISTICS_WRITING_DATE_UTC-eng: 2018-04-19 12:36:00
      _STATISTICS_TAGS-eng: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES SOURCE_ID
    Stream #0:2(fra): Audio: ac3, 48000 Hz, 5.1(side), fltp, 640 kb/s
    Metadata:
      title           : Surround 5.1
      BPS-eng         : 640000
      DURATION-eng    : 02:31:51.616000000
      NUMBER_OF_FRAMES-eng: 284738
      NUMBER_OF_BYTES-eng: 728929280
      SOURCE_ID-eng   : 001102
      _STATISTICS_WRITING_APP-eng: MakeMKV v1.12.0 linux(x64-release)
      _STATISTICS_WRITING_DATE_UTC-eng: 2018-04-19 12:36:00
      _STATISTICS_TAGS-eng: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES SOURCE_ID
    Stream #0:3(spa): Audio: ac3, 48000 Hz, 5.1(side), fltp, 640 kb/s
    Metadata:
      title           : Surround 5.1
      BPS-eng         : 640000
      DURATION-eng    : 02:31:51.616000000
      NUMBER_OF_FRAMES-eng: 284738
      NUMBER_OF_BYTES-eng: 728929280
      SOURCE_ID-eng   : 001103
      _STATISTICS_WRITING_APP-eng: MakeMKV v1.12.0 linux(x64-release)
      _STATISTICS_WRITING_DATE_UTC-eng: 2018-04-19 12:36:00
      _STATISTICS_TAGS-eng: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES SOURCE_ID
    Stream #0:4(eng): Subtitle: hdmv_pgs_subtitle
    Metadata:
      BPS-eng         : 31027
      DURATION-eng    : 02:23:23.031937500
      NUMBER_OF_FRAMES-eng: 3796
      NUMBER_OF_BYTES-eng: 33366586
      SOURCE_ID-eng   : 001200
      _STATISTICS_WRITING_APP-eng: MakeMKV v1.12.0 linux(x64-release)
      _STATISTICS_WRITING_DATE_UTC-eng: 2018-04-19 12:36:00
      _STATISTICS_TAGS-eng: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES SOURCE_ID
    Stream #0:5(fra): Subtitle: hdmv_pgs_subtitle
    Metadata:
      BPS-eng         : 25846
      DURATION-eng    : 02:25:12.266062500
      NUMBER_OF_FRAMES-eng: 3416
      NUMBER_OF_BYTES-eng: 28148178
      SOURCE_ID-eng   : 001201
      _STATISTICS_WRITING_APP-eng: MakeMKV v1.12.0 linux(x64-release)
      _STATISTICS_WRITING_DATE_UTC-eng: 2018-04-19 12:36:00
      _STATISTICS_TAGS-eng: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES SOURCE_ID
    Stream #0:6(spa): Subtitle: hdmv_pgs_subtitle
    Metadata:
      BPS-eng         : 25710
      DURATION-eng    : 02:22:40.572854166
      NUMBER_OF_FRAMES-eng: 3182
      NUMBER_OF_BYTES-eng: 27511663
      SOURCE_ID-eng   : 001202
      _STATISTICS_WRITING_APP-eng: MakeMKV v1.12.0 linux(x64-release)
      _STATISTICS_WRITING_DATE_UTC-eng: 2018-04-19 12:36:00
      _STATISTICS_TAGS-eng: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES SOURCE_ID
    Stream #0:7(eng): Subtitle: hdmv_pgs_subtitle
    Metadata:
      BPS-eng         : 70591
      DURATION-eng    : 02:24:13.207062500
      NUMBER_OF_FRAMES-eng: 4798
      NUMBER_OF_BYTES-eng: 76355021
      SOURCE_ID-eng   : 001203
      _STATISTICS_WRITING_APP-eng: MakeMKV v1.12.0 linux(x64-release)
      _STATISTICS_WRITING_DATE_UTC-eng: 2018-04-19 12:36:00
      _STATISTICS_TAGS-eng: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES SOURCE_ID
    Stream #0:8(fra): Subtitle: hdmv_pgs_subtitle
    Metadata:
      BPS-eng         : 58091
      DURATION-eng    : 02:24:11.121645833
      NUMBER_OF_FRAMES-eng: 4758
      NUMBER_OF_BYTES-eng: 62819211
      SOURCE_ID-eng   : 001204
      _STATISTICS_WRITING_APP-eng: MakeMKV v1.12.0 linux(x64-release)
      _STATISTICS_WRITING_DATE_UTC-eng: 2018-04-19 12:36:00
      _STATISTICS_TAGS-eng: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES SOURCE_ID
    Stream #0:9(spa): Subtitle: hdmv_pgs_subtitle
    Metadata:
      BPS-eng         : 59683
      DURATION-eng    : 02:24:11.121645833
      NUMBER_OF_FRAMES-eng: 4758
      NUMBER_OF_BYTES-eng: 64541222
      SOURCE_ID-eng   : 001205
      _STATISTICS_WRITING_APP-eng: MakeMKV v1.12.0 linux(x64-release)
      _STATISTICS_WRITING_DATE_UTC-eng: 2018-04-19 12:36:00
      _STATISTICS_TAGS-eng: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES SOURCE_ID
    Stream #0:10(fra): Subtitle: hdmv_pgs_subtitle, 1920x1080
    Metadata:
      BPS-eng         : 252
      DURATION-eng    : 02:25:22.401187500
      NUMBER_OF_FRAMES-eng: 44
      NUMBER_OF_BYTES-eng: 275272
      SOURCE_ID-eng   : 001206
      _STATISTICS_WRITING_APP-eng: MakeMKV v1.12.0 linux(x64-release)
      _STATISTICS_WRITING_DATE_UTC-eng: 2018-04-19 12:36:00
      _STATISTICS_TAGS-eng: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES SOURCE_ID
Segmentation fault (core dumped)

It appear that the

pFormatCtx->streams[0]->codec->codec_id

equals to

0

and

pFormatCtx->streams[0]->codecpar

is

NULL

but according to the header file of libavformat
https://github.com/FFmpeg/FFmpeg/blob/master/libavformat/avformat.h#L1002

avformat_find_stream_info()

should fill

AVCodecParameters *codecpar;

during demuxing

Change History (0)

Note: See TracTickets for help on using tickets.