<p><br>
On May 11, 2012 12:12 PM, "Bob Self" <<a href="mailto:bobslf@gmail.com">bobslf@gmail.com</a>> wrote:<br>
><br>
> I finally found an incomplete example that I was able to get sort-of working<br>
> with non-deprecated functions. The only error (or warning?) that I get is<br>
> "[mpeg4 @ 0x1367c80] Invalid and inefficient vfw-avi packed B frames detected"<br>
> once. Other than that the program runs without errors, but there is still<br>
> something that I don't understand.<br>
><br>
> pFormatCtx->streams[videoStream]->nb_frames says that there are 154,959 frames<br>
> But if I count the decoded frames (frame++), I only get 36,395 frames<br>
> before the loop quits. Why does this happen?<br>
><br>
nb_frames may be not reliable. How many frames does ffmpeg command decide?</p>
<p>BR,<br>
Alex Cohn</p>
<p>> the program output:<br>
> -------------------------------<br>
><br>
> [avi @ 0x1361920] Format avi probed with size=2048 and score=100<br>
> [AVI demuxer @ 0x1361e60] use odml:1<br>
> st:1 removing common factor 128 from timebase<br>
> [mpeg4 @ 0x1367c80] Invalid and inefficient vfw-avi packed B frames detected<br>
> [avi @ 0x1361920] All info found<br>
> Input #0, avi, from 'test.avi':<br>
> Metadata:<br>
> encoder : FairUse Wizard - <a href="http://fairusewizard.com">http://fairusewizard.com</a><br>
> Duration: 01:47:43.08, start: 0.000000, bitrate: 1815 kb/s<br>
> Stream #0:0, 1, 1001/24000: Video: mpeg4 (Advanced Simple Profile)<br>
> (XVID / 0x44495658), yuv420p, 688x368 [SAR 1:1 DAR 43:23], 1001/24000,<br>
> 23.98 tbr, 23.98 tbn, 23.98 tbc<br>
> Stream #0:1, 21, 3/125: Audio: mp3 (U[0][0][0] / 0x0055), 48000<br>
> Hz, stereo, s16, 128 kb/s<br>
><br>
><br>
> the test function:<br>
> ----------------------------<br>
><br>
> int test(void) {<br>
> const char *inputfile = "test.avi";<br>
> int totalFrames = 0;<br>
> int frameLocation = 0;<br>
> AVFormatContext *pFormatCtx;<br>
> unsigned int i, videoStream;<br>
> AVCodecContext *pCodecCtx;<br>
> AVCodec *pCodec;<br>
> AVFrame *pFrame;<br>
> AVFrame *pFrameYUV;<br>
> int numBytes;<br>
> uint8_t *buffer;<br>
> int status;<br>
> unsigned int codec_version;<br>
><br>
> av_register_all();<br>
><br>
> codec_version = avcodec_version();<br>
> // avcodec version: 54.10.100<br>
> printf("avcodec version = %d.%d.%d\n",<br>
> (codec_version&0x00ff0000)>>16, (codec_version&0x0000ff00)>>8,<br>
> codec_version&0x000000ff); // version 52.72.2<br>
><br>
> av_log_set_level(AV_LOG_DEBUG);<br>
><br>
> pFormatCtx = avformat_alloc_context();<br>
><br>
> status = avformat_open_input(&pFormatCtx, inputfile, NULL, NULL);<br>
> if (status != 0) {<br>
> return -1;<br>
> }<br>
><br>
> status = avformat_find_stream_info(pFormatCtx, NULL);<br>
> if (status<0) {<br>
> return -2;<br>
> }<br>
><br>
> av_dump_format(pFormatCtx, 0, inputfile, false);<br>
><br>
> videoStream=-1;<br>
><br>
> for(i=0; i<pFormatCtx->nb_streams; i++) {<br>
> if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO) {<br>
> videoStream=i;<br>
> break;<br>
> }<br>
> }<br>
> if(videoStream==-1) {<br>
> return -3;<br>
> }<br>
><br>
> pCodecCtx = pFormatCtx->streams[videoStream]->codec;<br>
><br>
> double duration = double(pFormatCtx->duration) / AV_TIME_BASE;<br>
><br>
> pCodec=avcodec_find_decoder(pCodecCtx->codec_id);<br>
> if(pCodec==NULL) {<br>
> return -4;<br>
> }<br>
><br>
> if(pCodec->capabilities & CODEC_CAP_TRUNCATED) {<br>
> pCodecCtx->flags|=CODEC_FLAG_TRUNCATED;<br>
> }<br>
><br>
> if(avcodec_open2(pCodecCtx, pCodec, NULL)<0) {<br>
> return -5;<br>
> }<br>
><br>
> printf("Using codec: %s\n", pCodec->name);<br>
><br>
> pFrame = avcodec_alloc_frame();<br>
><br>
> pFrameYUV=avcodec_alloc_frame();<br>
> if(pFrameYUV==NULL) {<br>
> return -6;<br>
> }<br>
><br>
> numBytes = avpicture_get_size(PIX_FMT_YUV420P, pCodecCtx->width,<br>
> pCodecCtx->height); // 379776<br>
> buffer = (uint8_t *)av_malloc(numBytes*sizeof(uint8_t));<br>
><br>
> avpicture_fill((AVPicture *)pFrameYUV, buffer, PIX_FMT_YUV420P,<br>
> pCodecCtx->width, pCodecCtx->height);<br>
><br>
> totalFrames = pFormatCtx->streams[videoStream]->nb_frames;<br>
> // 154959<br>
> printf("Frames: %d\n", totalFrames);<br>
><br>
> AVPacket packet;<br>
> int isFrameFinished = 0;<br>
> int pixelScanCount = 0;<br>
><br>
> printf("video width = %d\n", pCodecCtx->width);<br>
> printf("video height = %d\n", pCodecCtx->height);<br>
><br>
> int frame = 0;<br>
> int loops = 0;<br>
><br>
> while (av_read_frame(pFormatCtx, &packet) >= 0) {<br>
> status = av_read_frame(pFormatCtx, &packet);<br>
> if (status<0) {<br>
> break;<br>
> }<br>
><br>
> loops++;<br>
><br>
> if (packet.stream_index == videoStream) {<br>
> status = avcodec_decode_video2(pCodecCtx, pFrame,<br>
> &isFrameFinished, &packet);<br>
> if (isFrameFinished) {<br>
> frame++;<br>
><br>
> if ( (frame%1000) == 0) {<br>
> printf("%d\n", frame);<br>
> }<br>
><br>
> pixelScanCount = 0;<br>
><br>
> for (int y = 0; y < pCodecCtx->height; y++) {<br>
> uint8_t *p = pFrame->data[0] + y * pFrame->linesize[0];<br>
><br>
> for (int x = 0; x < pCodecCtx->width; x++) {<br>
> pixelScanCount++;<br>
> }<br>
> }<br>
> av_free_packet(&packet);<br>
><br>
> }<br>
> else {<br>
> av_free_packet(&packet);<br>
> }<br>
> }<br>
> }<br>
><br>
><br>
> printf("frame = %d\n", frame); // frame = 36395<br>
> printf("loops = %d\n", loops); // loops = 212127<br>
> printf("totalFrames = %d\n", totalFrames); // totalFrames = 154959<br>
><br>
> delete [] buffer;<br>
> av_free(pFrameYUV);<br>
> av_free(pFrame);<br>
> avcodec_close(pCodecCtx);<br>
> avformat_close_input(&pFormatCtx);<br>
><br>
> return 0;<br>
> }<br>
> _______________________________________________<br>
> Libav-user mailing list<br>
> <a href="mailto:Libav-user@ffmpeg.org">Libav-user@ffmpeg.org</a><br>
> <a href="http://ffmpeg.org/mailman/listinfo/libav-user">http://ffmpeg.org/mailman/listinfo/libav-user</a><br>
</p>