亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 1039 | 回復: 0
打印 上一主題 下一主題

Using libavformat and libavcodec: An Update [復制鏈接]

論壇徽章:
0
跳轉到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2011-02-14 21:25 |只看該作者 |倒序瀏覽
Using libavformat and libavcodec: An Update
 
 
 Martin Böhme (boehme@inb.uni-luebeckREMOVETHIS.de) July 21, 2004

Update (January 23 2009): By now, these articles are quite out of date... unfortunately, I haven't found the time to update them, but thankfully, others have jumped in. Stephen Dranger has a more recent tutorial, ryanfb of cryptosystem.org has an updated version of the code, and David Hoerl has a more recent update.

Update (April 26, 2005): A reader informs me that to compile the example programs on Kanotix (a Debian derivative) and possibly Debian itself, the include directives for avcodec.h and avformat.h have to be prefixed with "ffmpeg", like this:

  1. #include <ffmpeg/avcodec.h>
  2. #include <ffmpeg/avformat.h>

Also, the library libdts has to be included when compiling the programs, like this:

  1. g++ -o avcodec_sample.0.4.9 avcodec_sample.0.4.9.cpp \ -lavformat -lavcodec -ldts -lz

A few months ago, I wrote an article on using the libavformat and libavcodec libraries that come with ffmpeg. Since then, I have received a number of comments, and a new prerelease version of ffmpeg (0.4.9-pre1) has recently become available, adding support for seeking in video files, new file formats, and a simplified interface for reading video frames. These changes have been in the CVS for a while, but now is the first time we get to see them in a release. (Thanks by the way to Silviu Minut for sharing the results of long hours of studying the CVS versions of ffmpeg - his page with ffmpeg information and a demo program is here.)

本文主要講:

----------------------------------------------------------------------------------

In this article, I'll describe only the differences between the previous release (0.4.8) and the new one, so if you're new to libavformat / libavcodec, I suggest you read the original article first.

----------------------------------------------------------------------------------

First, a word about compiling the new release. On my compiler (gcc 3.3.1 on SuSE), I get an internal compiler error while compiling the source file ffv1.c. I suspect this particular version of gcc is a little flaky - I've had the same thing happen to me when compiling OpenCV - but at any rate, a quick fix is to compile this one file without optimizations. The easiest way to do this is to do a make, then when the build hits the compiler error, change to the libavcodec subdirectory (since this is where ffv1.c lives), copy the gcc command to compile ffv1.c from your terminal window, paste it back in, edit out the "-O3" compiler switch and then run gcc using that command. After that, you can change back to the main ffmpeg directory and restart make, and it should complete the build.

What's New?
ffmpeg0.4.9和0.4.8/earlier版本中兩個函數(shù)的區(qū)別
new: av_read_frame()
old: av_read_packet()
---------------------------------------------------------------------------------

So what's new? From a programmer's point of view, the biggest change is probably the simplified interface for reading individual video frames from a video file. In ffmpeg 0.4.8 and earlier, data is read from the video file in packets using the routine av_read_packet(). Usually, the information for one video frame is spread out over several packets, and the situation is made even more complicated by the fact that the boundary between two video frames can come in the middle of two packets. Thankfully, ffmpeg 0.4.9 introduces a new routine called av_read_frame(), which returns all of the data for a video frame in a single packet. The old way of reading video data using av_read_packet() is still supported but deprecated - I say: good riddance.

---------------------------------------------------------------------------------------------

So let's take a look at how to access video data using the new API. In my original article (with the old 0.4.8 API), the main decode loop looked like this:

  1. while(GetNextFrame(pFormatCtx, pCodecCtx, videoStream, pFrame))
  2. {
  3.      img_convert((AVPicture *)pFrameRGB, PIX_FMT_RGB24, (AVPicture*)pFrame,
  4.          pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);
  5.      // Process the video frame (save to disk etc.)
  6.      DoSomethingWithTheImage(pFrameRGB);
  7. }

GetNextFrame() is a helper routine that handles the process of assembling all of the packets that make up one video frame. The new API simplifies things to the point that we can do the actual reading and decoding of data directly in our main loop:

  1. while(av_read_frame(pFormatCtx, &packet)>=0)
  2. {
  3.     // Is this a packet from the video stream?
  4.     if(packet.stream_index==videoStream)
  5.     {
  6.          // Decode video frame
  7.          avcodec_decode_video(pCodecCtx, pFrame, &frameFinished, packet.data, packet.size);
  8.         // Did we get a video frame?
  9.         if(frameFinished)
  10.         {
  11.               // Convert the image from its native format to RGB
  12.               img_convert((AVPicture *)pFrameRGB, PIX_FMT_RGB24,
  13.                 (AVPicture*)pFrame, pCodecCtx->pix_fmt, pCodecCtx->width,
  14.                    pCodecCtx->height);
  15.              // Process the video frame (save to disk etc.)
  16.              DoSomethingWithTheImage(pFrameRGB);
  17.          }
  18.      }
  19.      // Free the packet that was allocated by av_read_frame
  20.      av_free_packet(&packet);
  21.  }

At first sight, it looks as if things have actually gotten more complex - but that is just because this piece code does things that used to be hidden in the GetNextFrame() routine (checking if the packet belongs to the video stream, decoding the frame and freeing the packet). Overall, because we can eliminate GetNextFrame() completely, things have gotten a lot easier.

I've updated the demo program to use the new API. Simply comparing the number of lines (222 lines for the old version vs. 169 lines for the new one) shows that the new API has simplified things considerably.

--------------------------------------------------------------------------------------------

Another important addition in the 0.4.9 release is the ability to seek to a certain timestamp in a video file. This is accomplished using the av_seek_frame() function, which takes three parameters: A pointer to the AVFormatContext, a stream index and the timestamp to seek to. The function will then seek to the first key frame before the given timestamp. All of this is from the documentation - I haven't gotten round to actually testing av_seek_frame() yet, so I can't present any sample code either. If you've used av_seek_frame() successfully, I'd be glad to hear about it.

--------------------------------------------------------------------------------------------

Frame Grabbing (Video4Linux and IEEE1394)
Toru Tamaki sent me some sample code that demonstrates how to grab frames from a Video4Linux or IEEE1394 video source using libavformat / libavcodec. For Video4Linux, the call to av_open_input_file() should be modified as follows:

  1. AVFormatParameters formatParams;
  2. AVInputFormat *iformat;
  3. formatParams.device = "/dev/video0";
  4. formatParams.channel = 0;
  5. formatParams.standard = "ntsc";
  6. formatParams.width = 640;
  7. formatParams.height = 480;
  8. formatParams.frame_rate = 29;
  9. formatParams.frame_rate_base = 1;
  10. filename = "";
  11. iformat = av_find_input_format("video4linux");
  12. av_open_input_file(&ffmpegFormatContext, filename, iformat, 0, &formatParams);

For IEEE1394, call av_open_input_file() like this:

  1. AVFormatParameters formatParams;
  2. AVInputFormat *iformat;
  3. formatParams.device = "/dev/dv1394";
  4. filename = "";
  5. iformat = av_find_input_format("dv1394");
  6. av_open_input_file(&ffmpegFormatContext, filename, iformat, 0, &formatParams);

If I come across additional interesting information about libavformat / libavcodec, I plan to publish it here. So, if you have any comments, please contact me at the address given at the top of this article.

Standard disclaimer: I assume no liability for the correct functioning of the code and techniques presented in this article.

 

您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復

  

北京盛拓優(yōu)訊信息技術有限公司. 版權所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關心和支持過ChinaUnix的朋友們 轉載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP