Hi Andrey,<br><br>Thanks for the help!<br><br>1. Lib versions: Using the shared library and my code below, I tried 0.8.3 (PC) and 08d2cee4 (Mac) and could not scan the file. However, I tried the static executable of 0.8 and 0.10 on Mac and I *could* transcode it to MP4.<br>
<br>2. Link to video (it's almost 2gb): <a href="http://dl.dropbox.com/u/28441949/00793.MTS" target="_blank">http://dl.dropbox.com/u/28441949/00793.MTS</a><br>
<br>3. Simplified, but compilable source code:<br><br><span style="font-family:courier new,monospace">int Scan(const char *inputfile)</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">{</span><br style="font-family:courier new,monospace">
<br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> totalFrames = 0;</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> frameLocation = 0;</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> </span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> AVFormatContext *pFormatCtx;</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> unsigned int i, videoStream;</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> AVCodecContext *pCodecCtx;</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> AVCodec *pCodec;</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> AVFrame *pFrame; </span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> AVFrame *pFrameYUV;</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> int numBytes;</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> uint8_t *buffer;</span><br style="font-family:courier new,monospace"><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> clock_t start = clock();</span><br style="font-family:courier new,monospace">
<br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> // Register all formats and codecs</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> av_register_all();</span><br style="font-family:courier new,monospace">
<br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> // Open video file</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> if(av_open_input_file(&pFormatCtx, inputfile, NULL, 0, NULL)!=0)</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> {</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> return -1; // Couldn't open file</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> }</span><br style="font-family:courier new,monospace"><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> // Retrieve stream information</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> if(av_find_stream_info(pFormatCtx)<0)</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> {</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> return -2; // Couldn't find stream information</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> }</span><br style="font-family:courier new,monospace">
<br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> // Dump information about file onto standard error</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> dump_format(pFormatCtx, 0, inputfile, false);</span><br style="font-family:courier new,monospace">
<br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> // Find the first video stream</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> videoStream=-1;</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> for(i=0; i<pFormatCtx->nb_streams; i++)</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> {</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO)</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> {</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> videoStream=i;</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> break;</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> }</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> }</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> if(videoStream==-1)</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> {</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> return -3; // Didn't find a video stream</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> }</span><br style="font-family:courier new,monospace"><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> // Get a pointer to the codec context for the video stream</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> pCodecCtx = pFormatCtx->streams[videoStream]->codec;</span><br style="font-family:courier new,monospace"><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> // get duration</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> double duration = double(pFormatCtx->duration) / AV_TIME_BASE;</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> cout << "Video duration: " << duration << std::endl;</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> </span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> // Find the decoder for the video stream</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> pCodec=avcodec_find_decoder(pCodecCtx->codec_id);</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> if(pCodec==NULL)</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> {</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> return -4; // Codec not found</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> }</span><br style="font-family:courier new,monospace"><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> // Inform the codec that we can handle truncated bitstreams -- i.e.,</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> // bitstreams where frame boundaries can fall in the middle of packets</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> if(pCodec->capabilities & CODEC_CAP_TRUNCATED)</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> pCodecCtx->flags|=CODEC_FLAG_TRUNCATED;</span><br style="font-family:courier new,monospace"><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> // Open codec</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> if(avcodec_open(pCodecCtx, pCodec)<0)</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> {</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> return -5; // Could not open codec</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> }</span><br style="font-family:courier new,monospace">
<br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> std::cout << "Using codec: " << pCodec->name << std::endl;</span><br style="font-family:courier new,monospace">
<br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> // Allocate video frame</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> pFrame=avcodec_alloc_frame();</span><br style="font-family:courier new,monospace">
<br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> // Allocate an AVFrame structure</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> pFrameYUV=avcodec_alloc_frame();</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> if(pFrameYUV==NULL)</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> {</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> return -6;</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> }</span><br style="font-family:courier new,monospace">
<br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> // Determine required buffer size and allocate buffer</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> // originally: PIX_FMT_RGB24</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> numBytes=avpicture_get_size(PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t));</span><br style="font-family:courier new,monospace">
<br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> // Assign appropriate parts of buffer to image planes in pFrameRGB</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> // originally: PIX_FMT_RGB24</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> avpicture_fill((AVPicture *)pFrameYUV, buffer, PIX_FMT_YUV420P,</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> pCodecCtx->width, pCodecCtx->height);</span><br style="font-family:courier new,monospace">
<br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> totalFrames = pFormatCtx->streams[videoStream]->nb_frames;</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> printf("Frames: %d\n", totalFrames);</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> </span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> AVPacket packet;</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> int isFrameFinished = 0;</span><br style="font-family:courier new,monospace"><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> int pixelScanCount = 0;</span><br style="font-family:courier new,monospace">
<br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> std::cout << "videoWidth = " << pCodecCtx->width << std::endl;</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> std::cout << "videoHeight = " << pCodecCtx->height << std::endl;</span><br style="font-family:courier new,monospace"><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> while (av_read_frame(pFormatCtx, &packet) >= 0) </span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> {</span><br style="font-family:courier new,monospace">
<br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> // Is this a packet from the video stream?</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> if (packet.stream_index == videoStream && </span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> frameLocation++ % frameSkip == 0)</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> {</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> // Decode video frame</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> </span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> avcodec_decode_video2(pCodecCtx, pFrame, &isFrameFinished, &packet);</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> </span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> // Did we get a video frame?</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> if (isFrameFinished) {</span><br style="font-family:courier new,monospace">
<br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> // SCANNING GOES HERE</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> pixelScanCount = 0;</span><br style="font-family:courier new,monospace">
<br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> for (int y = 0; y < pCodecCtx->height; y++)</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> {</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> uint8_t *p = pFrame->data[0] + y * pFrame->linesize[0]; // this gets Y' in YUV</span><br style="font-family:courier new,monospace"><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> for (int x = 0; x < pCodecCtx->width; x++)</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> {</span><br style="font-family:courier new,monospace">
<br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> pixelScanCount++;</span><br style="font-family:courier new,monospace"><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> //printf("Frame %d at %d, %d has Y'=%d\n", frameLocation, x, y, p[x]);</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> }</span><br style="font-family:courier new,monospace">
<br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> }</span><br style="font-family:courier new,monospace"><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> av_free_packet(&packet);</span><br style="font-family:courier new,monospace">
<br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> } </span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> else // frame isn't finished. not sure why we'd hit this</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> {</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> av_free_packet(&packet);</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> }</span><br style="font-family:courier new,monospace"><br style="font-family:courier new,monospace"><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> }</span><br style="font-family:courier new,monospace">
<br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> }</span><br style="font-family:courier new,monospace"><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> delete [] buffer;</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> av_free(pFrameYUV);</span><br style="font-family:courier new,monospace"><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> // Free the YUV frame</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> av_free(pFrame);</span><br style="font-family:courier new,monospace"><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> // Close the codec</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> avcodec_close(pCodecCtx);</span><br style="font-family:courier new,monospace"><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> // Close the video file</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> av_close_input_file(pFormatCtx);</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">}</span><br><br><br>4. When I enabled av_log_set_level(AV_LOG_DEBUG) in my app, I saw several of these:<br>
<span style="font-family:courier new,monospace">[h264 @ 09FC0940] Frame num gap 10 8</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">[h264 @ 09FC0940] Frame num gap 15 12</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace">[h264 @ 09FC0940] Frame num gap 15 13</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">[h264 @ 09FC0940] Frame num gap 20 17</span><br>
...<br><br><br>So considering the fact that the command-line utility was able to transcode, I'm assuming there's something wrong in my code. I've seen some warnings for deprecated functions so maybe that's my problem?<br>
<br>Thanks!<br><br>Simon<br><br><br><br><br><br><div class="gmail_quote">On Mon, Feb 6, 2012 at 2:56 AM, Andrey Utkin <span dir="ltr"><<a href="mailto:andrey.krieger.utkin@gmail.com" target="_blank">andrey.krieger.utkin@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Please help us help you - provide<br>
- version of libs you use<br>
- compilable minimized source code of your app<br>
- this sample file<br>
<br>
BTW, did you try to transcode it with ffmpeg command-line utility? If<br>
it also fails, please provide its full output with -loglevel debug<br>
BTW2 try to av_log_set_level(AV_LOG_DEBUG); in your app - maybe you'll<br>
see useful messages on stderr.<br>
<span><font color="#888888"><br>
<br>
--<br>
Andrey Utkin<br>
_______________________________________________<br>
Libav-user mailing list<br>
<a href="mailto:Libav-user@ffmpeg.org" target="_blank">Libav-user@ffmpeg.org</a><br>
<a href="http://ffmpeg.org/mailman/listinfo/libav-user" target="_blank">http://ffmpeg.org/mailman/listinfo/libav-user</a><br>
</font></span></blockquote></div><br>