Merge branch 'master' of git.sv.gnu.org:/srv/git/gnash
[gnash.git] / libcore / swf / VideoFrameTag.cpp
blob0f10556259398f6eb5909a217535864118c39958
1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
3 // Foundation, Inc
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #include "DefineVideoStreamTag.h"
21 #include <algorithm>
23 #include "RunResources.h"
24 #include "VideoFrameTag.h"
25 #include "VideoDecoder.h"
26 #include "SWFStream.h" // for read()
27 #include "movie_definition.h"
28 #include "utility.h"
29 #include "smart_ptr.h"
31 namespace gnash {
32 namespace SWF {
35 void
36 VideoFrameTag::loader(SWFStream& in, SWF::TagType tag, movie_definition& m,
37 const RunResources& /*r*/)
39 assert(tag == SWF::VIDEOFRAME);
41 in.ensureBytes(2);
42 boost::uint16_t id = in.read_u16();
43 DefinitionTag* chdef = m.getDefinitionTag(id);
45 if (!chdef)
47 IF_VERBOSE_MALFORMED_SWF(
48 log_swferror(_("VideoFrame tag refers to unknown video "
49 "stream id %d"), id);
51 return;
54 DefineVideoStreamTag* vs = dynamic_cast<DefineVideoStreamTag*> (chdef);
55 if (!vs)
57 IF_VERBOSE_MALFORMED_SWF(
58 log_swferror(_("VideoFrame tag refers to a non-video DisplayObject "
59 "%d (%s)"), id, typeName(*chdef));
61 return;
64 // TODO: skip if there's no MediaHandler registered ?
66 const unsigned short padding = 8;
68 in.ensureBytes(2);
69 unsigned int frameNum = in.read_u16();
71 const unsigned int dataLength = in.get_tag_end_position() - in.tell();
73 // FIXME: catch bad_alloc
74 boost::uint8_t* buffer = new boost::uint8_t[dataLength + padding];
76 const size_t bytesRead = in.read(reinterpret_cast<char*>(buffer),
77 dataLength);
79 if (bytesRead < dataLength)
81 throw ParserException(_("Could not read enough bytes when parsing "
82 "VideoFrame tag. Perhaps we reached the "
83 "end of the stream!"));
86 std::fill_n(buffer + bytesRead, padding, 0);
88 using namespace media;
90 std::auto_ptr<EncodedVideoFrame> frame(
91 new EncodedVideoFrame(buffer, dataLength, frameNum));
93 vs->addVideoFrameTag(frame);
96 } // namespace SWF
97 } // namespace gnash