首页 \ 问答 \ 如何从单个文件描述符中分配多个MMAP?(How would I assign multiple MMAP's from single file descriptor?)

如何从单个文件描述符中分配多个MMAP?(How would I assign multiple MMAP's from single file descriptor?)

因此,对于我的最后一年项目,我使用Video4Linux2从相机中提取YUV420图像,将它们解析为x264(本机使用这些图像),然后通过Live555将编码流发送到符合RTP / RTCP的视频播放器通过无线网络在客户端上。 所有这些我都试图实时进行,所以会有一个控制算法,但这不是这个问题的范围。 所有这些 - 除了Live555 - 都是用C语言编写的。目前,我已接近编码视频的结尾,但希望提高性能。

至少可以说,我遇到了麻烦......我正在尝试避免使用V4L2的用户空间指针并使用mmap()。 我正在对视频进行编码,但由于它是YUV420,我一直在使用新的存储器将Y',U和V平面保存在三个不同的变量中,以供x264读取。 我想将这些变量作为指向mmap内存的指针。

但是,V4L2设备有一个用于缓冲流的单个文件描述符,我需要将流分成三个符合YUV420标准的mmap变量,就像这样......

buffers[n_buffers].y_plane = mmap(NULL, (2 * width * height) / 3,
                                    PROT_READ | PROT_WRITE, MAP_SHARED,
                                    fd, buf.m.offset);
buffers[n_buffers].u_plane = mmap(NULL, width * height / 6,
                                    PROT_READ | PROT_WRITE, MAP_SHARED,
                                    fd, buf.m.offset +
                                    ((2 * width * height) / 3 + 1) /
                                    sysconf(_SC_PAGE_SIZE));
buffers[n_buffers].v_plane = mmap(NULL, width * height / 6,
                                    PROT_READ | PROT_WRITE, MAP_SHARED,
                                    fd, buf.m.offset +
                                    ((2 * width * height) / 3 + 
                                    width * height / 6 + 1) / 
                                    sysconf(_SC_PAGE_SIZE));

其中“宽度”和“高度”是视频的分辨率(例如640x480)。

根据我的理解...... MMAP寻找一个文件,有点像这样(伪代码):

fd = v4l2_open(...);
lseek(fd, buf.m.offset + (2 * width * height) / 3);
read(fd, buffers[n_buffers].u_plane, width * height / 6);

我的代码位于Launchpad Repo(更多背景信息): http//bazaar.launchpad.net/~alex-stevens/+junk/spyPanda/files (Revision 11)

从这个Wiki插图中可以清楚地看到YUV420格式: http//en.wikipedia.org/wiki/FileYuv420.svg (我基本上想要将Y,U和V字节分成每个mmap'ed记忆)

有人想解释一种方法,从一个文件描述符中将三个变量映射到内存,或者为什么我出错? 或者甚至暗示将YUV420缓冲区解析为x264更好的主意? :P

干杯! ^^


So, for my final year project, I'm using Video4Linux2 to pull YUV420 images from a camera, parse them through to x264 (which uses these images natively), and then send the encoded stream via Live555 to an RTP/RTCP compliant video player on a client over a wireless network. All of this I'm trying to do in real-time, so there'll be a control algorithm, but that's not the scope of this question. All of this - except Live555 - is being written in C. Currently, I'm near the end of encoding the video, but want to improve performance.

To say the least, I've hit a snag... I'm trying to avoid User Space Pointers for V4L2 and use mmap(). I'm encoding video, but since it's YUV420, I've been malloc'ing new memory to hold the Y', U and V planes in three different variables for x264 to read upon. I would like to keep these variables as pointers to an mmap'ed piece of memory.

However, the V4L2 device has one single file descriptor for the buffered stream, and I need to split the stream into three mmap'ed variables adhering to the YUV420 standard, like so...

buffers[n_buffers].y_plane = mmap(NULL, (2 * width * height) / 3,
                                    PROT_READ | PROT_WRITE, MAP_SHARED,
                                    fd, buf.m.offset);
buffers[n_buffers].u_plane = mmap(NULL, width * height / 6,
                                    PROT_READ | PROT_WRITE, MAP_SHARED,
                                    fd, buf.m.offset +
                                    ((2 * width * height) / 3 + 1) /
                                    sysconf(_SC_PAGE_SIZE));
buffers[n_buffers].v_plane = mmap(NULL, width * height / 6,
                                    PROT_READ | PROT_WRITE, MAP_SHARED,
                                    fd, buf.m.offset +
                                    ((2 * width * height) / 3 + 
                                    width * height / 6 + 1) / 
                                    sysconf(_SC_PAGE_SIZE));

Where "width" and "height" is the resolution of the video (eg. 640x480).

From what I understand... MMAP seeks through a file, kind of like this (pseudoish-code):

fd = v4l2_open(...);
lseek(fd, buf.m.offset + (2 * width * height) / 3);
read(fd, buffers[n_buffers].u_plane, width * height / 6);

My code is located in a Launchpad Repo here (for more background): http://bazaar.launchpad.net/~alex-stevens/+junk/spyPanda/files (Revision 11)

And the YUV420 format can be seen clearly from this Wiki illustration: http://en.wikipedia.org/wiki/File:Yuv420.svg (I essentially want to split up the Y, U, and V bytes into each mmap'ed memory)

Anyone care to explain a way to mmap three variables to memory from the one file descriptor, or why I went wrong? Or even hint at a better idea to parse the YUV420 buffer to x264? :P

Cheers! ^^


原文:https://stackoverflow.com/questions/6290147
更新时间:2023-04-26 21:04

最满意答案

所以你的问题是,如何正确调用你的SaveImage(Bitmap finalBitmap)方法,对吧? 因为你的SaveImage方法获得一个Bitmap作为参数,你需要发送一个Bitmap作为参数。

您可以使用BitmapFactory从您的文件创建一个Bitmap对象,并将此Bitmap对象发送到您的SaveImage方法:

String root = Environment.getExternalStorageDirectory().toString();
Bitmap bMap = BitmapFactory.decodeFile(root + "/Movies/myimage.png");
SaveImage(bMap);

So your question is, how to properly call your SaveImage(Bitmap finalBitmap) method, right ? as your SaveImage method get a Bitmap as parameter you need to send it a Bitmap as parameter.

You can use BitmapFactory to create a Bitmap object from your file and send this Bitmap object to your SaveImage method :

String root = Environment.getExternalStorageDirectory().toString();
Bitmap bMap = BitmapFactory.decodeFile(root + "/Movies/myimage.png");
SaveImage(bMap);

相关问答

更多
  • 尝试做: new File("loc/xyz1.mp3").renameTo(new File("loc/xyz.mp3")); 这应该会自动覆盖原始文件。 这个答案取自于: 如何重命名现有文件 Try doing: new File("loc/xyz1.mp3").renameTo(new File("loc/xyz.mp3")); This should automatically overwrite the original file. This answer was taken from her ...
  • 喜欢这个: new File(path).renameTo(new File(newPath)); Like this: new File(path).renameTo(new File(newPath));
  • 根据文件参考。 许多失败是可能的。 一些更可能的失败包括: 包含源路径和目标路径的目录都需要写入权限。 两条路径的所有父级都需要搜索权限。 两条路径都在同一个挂载点上。 在Android上,当尝试在内部存储和SD卡之间进行复制时,应用程序最有可能达到此限制。 请注意,此方法在失败时不会抛出IOException。 呼叫者必须检查返回值。 你可以一个一个仔细检查吗? 我想你可以知道发生了什么。 I figured it out! The problem was in this line: File direc ...
  • 看起来InkFilePicker会在上传期间为文件名添加唯一键。 当myfile.pdf保存在Amazon S3上时,它变成类似于DNjimbeSQWVrcd0Uv8lJ_myfile.pdf的内容,因此它本身可以防止覆盖。 Looks like InkFilePicker prepends a unique key to file name during upload. myfile.pdf becomes something like DNjimbeSQWVrcd0Uv8lJ_myfile.pdf wh ...
  • 假设您有这个故事板的视图控制器,Option +单击您的源文件以在助理编辑器中打开它。 您可以从文档大纲(箭头指向的位置)CTRL +拖动到源文件中要显示插座的位置,Xcode将为视图创建IBOutlet。 如果您的问题是,您可以在没有视图控制器的情况下从故事板加载视图。 我想你可以实例化视图控制器,对视图进行强引用,从超级视图中删除视图然后删除视图控制器,但这看起来有点浪费; 在这种情况下,我会将视图复制并粘贴到自己的笔尖中。 请注意,就移动视图而言,是的,它将移动其所有子视图。 如果这是一个临时动画类型 ...
  • 您可以使用find ~/Projects -type f -name '*.png' -execdir mv {} Info.png \; You can go with find ~/Projects -type f -name '*.png' -execdir mv {} Info.png \;
  • 你有没有尝试过php的重命名功能? rename("The existing file name", "the new name"); Have u tried the rename function of the php? rename("The existing file name", "the new name");
  • 你试过QDir::rename()吗? http://qt-project.org/doc/qt-4.8/qdir.html#rename 我主要是Windows / Linux Qt用户,因此我无法帮助您了解OS X的最佳实践。 Have you tried QDir::rename()? http://qt-project.org/doc/qt-4.8/qdir.html#rename I'm primarily a Windows/Linux Qt user so I can't help you o ...
  • 所以你的问题是,如何正确调用你的SaveImage(Bitmap finalBitmap)方法,对吧? 因为你的SaveImage方法获得一个Bitmap作为参数,你需要发送一个Bitmap作为参数。 您可以使用BitmapFactory从您的文件创建一个Bitmap对象,并将此Bitmap对象发送到您的SaveImage方法: String root = Environment.getExternalStorageDirectory().toString(); Bitmap bMap = BitmapFa ...
  • 问题是“名称冲突”,所以这可能是一个解决方案 DropboxAPI.UploadRequest putFileRequest(String path, InputStream is, long length, String parentRev, boolean autoRename, ProgressListener listener) force boolean autoRename为true 但是,我仍然希望在以编程方式上载到Android上的Dropbox之前重命名 The problem is ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)