- Main Page
- Modules
- Namespaces
- Classes
- Files

- Search for |

- File List
- File Members

# samples/common/classes/NvUtils.cpp

Go to the documentation of this file.

    00001 /*
    00002  * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
    00003  *
    00004  * Redistribution and use in source and binary forms, with or without
    00005  * modification, are permitted provided that the following conditions
    00006  * are met:
    00007  *  * Redistributions of source code must retain the above copyright
    00008  *    notice, this list of conditions and the following disclaimer.
    00009  *  * Redistributions in binary form must reproduce the above copyright
    00010  *    notice, this list of conditions and the following disclaimer in the
    00011  *    documentation and/or other materials provided with the distribution.
    00012  *  * Neither the name of NVIDIA CORPORATION nor the names of its
    00013  *    contributors may be used to endorse or promote products derived
    00014  *    from this software without specific prior written permission.
    00015  *
    00016  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
    00017  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    00018  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    00019  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
    00020  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    00021  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    00022  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    00023  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
    00024  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    00026  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    00027  */
    00028
    00029 #include "NvUtils.h"
    00030 #include "NvBuffer.h"
    00031 #include "NvLogging.h"
    00032 #include <fstream>
    00033
    00034 int
    00035 read_video_frame(std::ifstream * stream, NvBuffer & buffer)
    00036 {
    00037     uint32_t i, j;
    00038     char *data;
    00039
    00040     for (i = 0; i < buffer.n_planes; i++)
    00041     {
    00042         NvBuffer::NvBufferPlane &plane = buffer.planes[i];
    00043         std::streamsize bytes_to_read =
    00044             plane.fmt.bytesperpixel * plane.fmt.width;
    00045         data = (char *) plane.data;
    00046         plane.bytesused = 0;
    00047         for (j = 0; j < plane.fmt.height; j++)
    00048         {
    00049             stream->read(data, bytes_to_read);
    00050             if (stream->gcount() < bytes_to_read)
    00051                 return -1;
    00052             data += plane.fmt.stride;
    00053         }
    00054         plane.bytesused = plane.fmt.stride * plane.fmt.height;
    00055     }
    00056     return 0;
    00057 }
    00058
    00059 int
    00060 write_video_frame(std::ofstream * stream, NvBuffer &buffer)
    00061 {
    00062     uint32_t i, j;
    00063     char *data;
    00064
    00065     for (i = 0; i < buffer.n_planes; i++)
    00066     {
    00067         NvBuffer::NvBufferPlane &plane = buffer.planes[i];
    00068         size_t bytes_to_write =
    00069             plane.fmt.bytesperpixel * plane.fmt.width;
    00070
    00071         data = (char *) plane.data;
    00072         for (j = 0; j < plane.fmt.height; j++)
    00073         {
    00074             stream->write(data, bytes_to_write);
    00075             if (!stream->good())
    00076                 return -1;
    00077             data += plane.fmt.stride;
    00078         }
    00079     }
    00080     return 0;
    00081 }