如何从单个文件描述符中分配多个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/File : Yuv420.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
最满意答案
所以你的问题是,如何正确调用你的
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 yourSaveImage
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);
相关问答
更多-
如何重命名现有文件?(How to rename an existing file?)[2023-02-21]
尝试做: 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 ... -
如何重命名现有文件(How to rename an existing file)[2022-06-05]
喜欢这个: new File(path).renameTo(new File(newPath)); Like this: new File(path).renameTo(new File(newPath)); -
如何重命名现有文件?(How do I rename the existing file?)[2023-08-13]
根据文件参考。 许多失败是可能的。 一些更可能的失败包括: 包含源路径和目标路径的目录都需要写入权限。 两条路径的所有父级都需要搜索权限。 两条路径都在同一个挂载点上。 在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 ...
-
iOS - 引用并以编程方式从XIB文件移动视图(iOS - referencing and programmatically moving a view from an XIB file)[2022-01-07]
假设您有这个故事板的视图控制器,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中重命名文件?(How to rename a file in php?)[2023-08-16]
你有没有尝试过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"); -
使用Qt在OSX上重命名.app(Rename .app on OSX using Qt)[2021-08-28]
你试过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 ...