/* ***** BEGIN LICENSE BLOCK ***** * * $Id: pic_io.cpp,v 1.23 2007/03/23 15:32:53 asuraparaju Exp $ $Name: Dirac_0_7_0 $ * * Version: MPL 1.1/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Mozilla Public License * Version 1.1 (the "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for * the specific language governing rights and limitations under the License. * * The Original Code is BBC Research and Development code. * * The Initial Developer of the Original Code is the British Broadcasting * Corporation. * Portions created by the Initial Developer are Copyright (C) 2004. * All Rights Reserved. * * Contributor(s): Thomas Davies (Original Author), * Scott Robert Ladd, * Stuart Cunningham, * Tim Borer * * Alternatively, the contents of this file may be used under the terms of * the GNU General Public License Version 2 (the "GPL"), or the GNU Lesser * Public License Version 2.1 (the "LGPL"), in which case the provisions of * the GPL or the LGPL are applicable instead of those above. If you wish to * allow use of your version of this file only under the terms of the either * the GPL or LGPL and not to allow others to use your version of this file * under the MPL, indicate your decision by deleting the provisions above * and replace them with the notice and other provisions required by the GPL * or LGPL. If you do not delete the provisions above, a recipient may use * your version of this file under the terms of any one of the MPL, the GPL * or the LGPL. * ***** END LICENSE BLOCK ***** */ #include #include using namespace dirac; /*************************************Output***********************************/ StreamPicOutput::StreamPicOutput() {} StreamPicOutput::~StreamPicOutput() {} StreamPicOutput::StreamPicOutput (const SeqParams& sp) : m_sparams(sp), m_op_pic_ptr(0) { } bool StreamPicOutput::WriteNextFrame( const Frame& myframe ) { bool ret_val; ret_val=WriteComponent(myframe.Ydata() , Y_COMP ); ret_val|=WriteComponent( myframe.Udata() , U_COMP ); ret_val|=WriteComponent( myframe.Vdata() , V_COMP ); return ret_val; } bool StreamPicOutput::WriteComponent( const PicArray& pic_data , const CompSort& cs) { //initially set up for 10-bit data input, rounded to 8 bits on file output //This will throw out any padding to the right and bottom of a frame int xl,yl; if (cs == Y_COMP) { xl = m_sparams.Xl(); yl = m_sparams.Yl(); } else { xl = m_sparams.ChromaWidth(); yl = m_sparams.ChromaHeight(); } unsigned char* tempc=new unsigned char[xl]; if (m_op_pic_ptr) { for (int j=0 ; jwrite((char*) tempc,xl); }//J } else { std::cerr<flush(); delete[] tempc; //exit success return true; } MemoryStreamOutput::MemoryStreamOutput() { //picture input m_op_pic_ptr = new std::ostream(&m_membuf); } MemoryStreamOutput::~MemoryStreamOutput() { delete m_op_pic_ptr; } void MemoryStreamOutput::SetMembufReference (unsigned char *buf, int buf_size) { m_membuf.SetMembufReference(buf, buf_size); } FileStreamOutput::FileStreamOutput(const char* output_name, const SeqParams& sp) : StreamPicOutput(sp) { OpenYUV(output_name); } bool FileStreamOutput::OpenYUV(const char* output_name) { char output_name_yuv[FILENAME_MAX]; strncpy(output_name_yuv,output_name, sizeof(output_name_yuv)); //strcat(output_name_yuv,".yuv"); //picture output m_op_pic_ptr = new std::ofstream(output_name_yuv,std::ios::out | std::ios::binary); if (!(*m_op_pic_ptr)) { std::cerr << std::endl << "Can't open output picture data file for output: " << output_name_yuv<(m_op_pic_ptr)->close(); delete m_op_pic_ptr; } } /**************************************Input***********************************/ StreamPicInput::StreamPicInput () : m_ip_pic_ptr(0), m_xpad(0), m_ypad(0) {} StreamPicInput::StreamPicInput (std::istream *ip_pic_ptr, const SeqParams &sparams) : m_sparams(sparams), m_ip_pic_ptr(ip_pic_ptr), m_xpad(0), m_ypad(0) {} StreamPicInput::~StreamPicInput () {} void StreamPicInput::SetPadding(const int xpd, const int ypd) { m_xpad=xpd; m_ypad=ypd; } bool StreamPicInput::ReadNextFrame(Frame& myframe) { //return value. Failure if one of the components can't be read, //success otherwise/. bool ret_val; ret_val=ReadComponent( myframe.Ydata() , Y_COMP); ret_val|=ReadComponent(myframe.Udata() , U_COMP); ret_val|=ReadComponent(myframe.Vdata() , V_COMP); return ret_val; } bool StreamPicInput::ReadComponent(PicArray& pic_data, const CompSort& cs) { if (! *m_ip_pic_ptr) return false; //initially set up for 8-bit file input expanded to 10 bits for array output int xl,yl; if (cs == Y_COMP){ xl = m_sparams.Xl(); yl = m_sparams.Yl(); } else{ if (m_sparams.CFormat()==format420) { xl = m_sparams.Xl()/2; yl = m_sparams.Yl()/2; } else if (m_sparams.CFormat() == format422) { xl = m_sparams.Xl()/2; yl = m_sparams.Yl(); } else{ xl = m_sparams.Xl(); yl = m_sparams.Yl(); } } unsigned char * temp = new unsigned char[xl];//array big enough for one line for (int j=0 ; jread((char*) temp, xl); for (int i=0 ; i(m_ip_pic_ptr)->close(); delete m_ip_pic_ptr; } void FileStreamInput::Skip(const int num) { const int num_pels = m_sparams.Xl()*m_sparams.Yl(); int num_bytes; const ChromaFormat cf = m_sparams.CFormat(); if ( cf == format420 ) num_bytes = (num_pels*3)/2; else if ( cf == format422 ) num_bytes = num_pels*2; else num_bytes = num_pels*3; m_ip_pic_ptr->seekg( num*num_bytes , std::ios::cur ); } bool StreamPicInput::End() const { return m_ip_pic_ptr->eof(); }