ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

image/codecs/mpeg.cpp

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "common/debug.h"
00024 #include "common/stream.h"
00025 #include "common/system.h"
00026 #include "common/textconsole.h"
00027 #include "graphics/surface.h"
00028 #include "graphics/yuv_to_rgb.h"
00029 
00030 #include "image/codecs/mpeg.h"
00031 
00032 namespace Image {
00033 
00034 MPEGDecoder::MPEGDecoder() : Codec() {
00035     _pixelFormat = g_system->getScreenFormat();
00036     _surface = 0;
00037 
00038     _mpegDecoder = mpeg2_init();
00039 
00040     if (!_mpegDecoder)
00041         error("Could not initialize libmpeg2");
00042 
00043     _mpegInfo = mpeg2_info(_mpegDecoder);
00044 }
00045 
00046 MPEGDecoder::~MPEGDecoder() {
00047     mpeg2_close(_mpegDecoder);
00048 
00049     if (_surface) {
00050         _surface->free();
00051         delete _surface;
00052     }
00053 }
00054 
00055 const Graphics::Surface *MPEGDecoder::decodeFrame(Common::SeekableReadStream &stream) {
00056     uint32 framePeriod;
00057     decodePacket(stream, framePeriod);
00058     return _surface;
00059 }
00060 
00061 bool MPEGDecoder::decodePacket(Common::SeekableReadStream &packet, uint32 &framePeriod, Graphics::Surface *dst) {
00062     // Decode as much as we can out of this packet
00063     uint32 size = 0xFFFFFFFF;
00064     mpeg2_state_t state;
00065     bool foundFrame = false;
00066     framePeriod = 0;
00067 
00068     do {
00069         state = mpeg2_parse(_mpegDecoder);
00070 
00071         switch (state) {
00072         case STATE_BUFFER:
00073             size = packet.read(_buffer, BUFFER_SIZE);
00074             mpeg2_buffer(_mpegDecoder, _buffer, _buffer + size);
00075             break;
00076         case STATE_SLICE:
00077         case STATE_END:
00078             if (_mpegInfo->display_fbuf) {
00079                 foundFrame = true;
00080                 const mpeg2_sequence_t *sequence = _mpegInfo->sequence;
00081                 const mpeg2_picture_t *picture = _mpegInfo->display_picture;
00082 
00083                 framePeriod += sequence->frame_period;
00084                 if (picture->nb_fields > 2) {
00085                     framePeriod += (sequence->frame_period / 2);
00086 
00087                 }
00088 
00089                 if (!dst) {
00090                     // If no destination is specified, use our internal storage
00091                     if (!_surface) {
00092                         _surface = new Graphics::Surface();
00093                         _surface->create(sequence->picture_width, sequence->picture_height, _pixelFormat);
00094                     }
00095 
00096                     dst = _surface;
00097                 }
00098 
00099                 YUVToRGBMan.convert420(dst, Graphics::YUVToRGBManager::kScaleITU, _mpegInfo->display_fbuf->buf[0],
00100                         _mpegInfo->display_fbuf->buf[1], _mpegInfo->display_fbuf->buf[2], sequence->picture_width,
00101                         sequence->picture_height, sequence->width, sequence->chroma_width);
00102             }
00103             break;
00104         default:
00105             break;
00106         }
00107     } while (size != 0);
00108 
00109     return foundFrame;
00110 }
00111 
00112 } // End of namespace Image


Generated on Sat Feb 9 2019 05:00:37 for ResidualVM by doxygen 1.7.1
curved edge   curved edge