[matroska] r1257 - trunk/foo_input_matroska

[email protected] Mon, 17 Jul 2006 10:48:08 +0400 (MSD)
Newsgroups gmane.comp.multimedia.matroska.cvs
Message-ID <[email protected]>
Author: ayana
Date: 2006-07-17 10:48:03 +0400 (Mon, 17 Jul 2006)
New Revision: 1257

Modified:
   trunk/foo_input_matroska/foo_input_matroska.cpp
   trunk/foo_input_matroska/matroska_parser.cpp
   trunk/foo_input_matroska/matroska_parser.h
Log:
change: simplify parsing except decoding

Modified: trunk/foo_input_matroska/foo_input_matroska.cpp
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- trunk/foo_input_matroska/foo_input_matroska.cpp	2006-07-17 06:46:55 U=
TC (rev 1256)
+++ trunk/foo_input_matroska/foo_input_matroska.cpp	2006-07-17 06:48:03 U=
TC (rev 1257)
@@ -143,8 +143,7 @@
=20
 		m_parser =3D new MatroskaAudioParser(m_file, p_abort);
=20
-		//if (m_parser->Parse(!(p_reason & input_open_decode))) {
-		if (m_parser->Parse()) {
+		if (m_parser->Parse(!(m_reason & input_open_decode))) {
 			console::error("Matroska: Invalid Matroska file.");
 			cleanup();
 			throw exception_io_unsupported_format();
@@ -208,8 +207,8 @@
=20
 		m_parser->SetFB2KInfo(p_info, m_subsong);
=20
-		//hprintf(L"Matroska: m_parser->GetDuration(): %f\n", (double)(int64)m=
_parser->GetDuration() / 1000000000.0);
-		p_info.set_length((double)(int64)m_parser->GetDuration() / 1000000000.=
0);
+		hprintf(L"Matroska: m_parser->GetDuration(): %f\n", m_parser->GetDurat=
ion());
+		p_info.set_length(m_parser->GetDuration());
=20
 		if (p_info.info_get_bitrate() =3D=3D 0) {
 			p_info.info_set_bitrate(m_parser->GetAvgBitrate());
@@ -247,9 +246,9 @@
 			return false;
 		}
=20
-		if (m_position>=3Dm_length)
+		if (m_position >=3D m_length)
 		{
-			hprintf(L"Matroska: decode_run() return false\n");
+            hprintf(L"Matroska: decode_run() return false: m_position=3D=
%d\n", m_position);
 			return false;
 		}
=20
@@ -265,18 +264,22 @@
 				int64 delta =3D duration_to_samples(m_frame.timecode) - m_position;
 				console::info(uStringPrintf("drift: %d",(int)delta));
 			}
-*/		=09
+*/
=20
 			if (m_frame_remaining=3D=3D0 || m_frame=3D=3D0)
 			{
 				if (m_frame!=3D0)
 					delete m_frame;
 				m_frame =3D m_parser->ReadSingleFrame();
-				if (m_frame=3D=3D0)
+                if (m_frame=3D=3D0) {
+                    hprintf(L"Matroska: decode_run() return false: m_fra=
me=3D0\n");
 					return false;
+                }
 				m_frame_remaining =3D m_frame->dataBuffer.size();
-				if (m_frame_remaining =3D=3D 0)
+                if (m_frame_remaining =3D=3D 0) {
+                    hprintf(L"Matroska: decode_run() return false: m_fra=
me_remaining=3D0\n");
 					return false;
+                }
 			}
=20
 			{
@@ -298,7 +301,8 @@
 				console::error(uStringPrintf("Matroska: '%s' decode error.", (const =
char*)currentTrack.codecID.c_str()));
 				//console::error(uStringPrintf("buffer_size =3D %d, m_frame_remainin=
g =3D %d", buffer_size, m_frame_remaining));
 				//console::error(uStringPrintf("buffer 0x%02X 0x%02X 0x%02X 0x%02X 0=
x%02X 0x%02X 0x%02X 0x%02X", m_buffer[0], m_buffer[1], m_buffer[2], m_buf=
fer[3], m_buffer[4], m_buffer[5], m_buffer[6], m_buffer[7]));			=09
-				cleanup();
+				hprintf(L"Matroska: decode_run() return false: decode error\n");
+                cleanup();
 				return false;
 			}
 			if (m_tempchunk.is_valid())
@@ -340,6 +344,7 @@
 				{
 					console::error("Matroska: decoder produced invalid chunk.");
 					cleanup();
+                    hprintf(L"Matroska: decode_run() return false: inval=
id chunk\n");
 					return false;
 				}
 				unsigned samplerate,channels,decoded_sample_count;
@@ -380,8 +385,10 @@
 					if (duration>max)
 						duration =3D max;
 					m_position +=3D duration;
-					if (m_position=3D=3Dm_length && duration=3D=3D0)
+                    if (m_position=3D=3Dm_length && duration=3D=3D0) {
+                        hprintf(L"Matroska: decode_run() return false: d=
uration=3D0\n");
 						return false;
+                    }
 				}
=20
=20
@@ -532,14 +539,14 @@
 			m_parser =3D NULL;
 		}
 	}
-	int64 duration_to_samples(int64 val)
+	int64 duration_to_samples(double val)
 	{
-		return (int64) ( (double)val * (double)m_expected_sample_rate / 100000=
0000.0 + 0.5);
+		return static_cast<int64>(val * (double)m_expected_sample_rate + 0.5);
 	}
=20
-	int64 samples_to_duration(int64 val)
+	double samples_to_duration(int64 val)
 	{
-		return (int64) ( (double)val * 1000000000.0 / (double)m_expected_sampl=
e_rate + 0.5);
+		return static_cast<double>(val) / static_cast<double>(m_expected_sampl=
e_rate);
 	}
=20
 	void set_current_track(unsigned int p_index) {
@@ -605,10 +612,7 @@
 			m_skip_frames =3D 0;
 			m_frame_remaining =3D 0;
 			m_frame =3D 0;
-			if (decode_can_seek()) {
-				decode_seek(0, p_abort);
-			}
-			m_frame =3D m_parser->ReadSingleFrame();
+			m_frame =3D m_parser->ReadFirstFrame();
 			if (m_frame !=3D NULL) {
 				unsigned int buffer_size =3D m_frame->dataBuffer.at(0).size();
 				m_buffer.set_size(buffer_size);
@@ -633,7 +637,7 @@
 #endif
 DECLARE_COMPONENT_VERSION(
     INPUT_MATROSKA_NAME,
-    "0.9.1.0",
+    "0.9.1.1",
     "ported to 0.9 by Haru Ayana <[email protected]>\n"
     "Copyright (C) 2003-2004 Jory Stone ([email protected])\n"
     "Copyright (C) 2003-2004 Peter Pawlowski"

Modified: trunk/foo_input_matroska/matroska_parser.cpp
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- trunk/foo_input_matroska/matroska_parser.cpp	2006-07-17 06:46:55 UTC =
(rev 1256)
+++ trunk/foo_input_matroska/matroska_parser.cpp	2006-07-17 06:48:03 UTC =
(rev 1257)
@@ -381,8 +381,8 @@
 						KaxDuration &duration =3D *static_cast<KaxDuration *>(ElementLevel=
2.get());
 						duration.ReadData(m_InputStream.I_O());
=20
-						// it's in milliseconds?
-						m_Duration =3D double(duration) * (double)(int64)m_TimecodeScale;
+						// it's in milliseconds? -- in seconds.
+						m_Duration =3D float(duration) * m_TimecodeScale;
 						//matroskaGlobalTrack->SetDuration(float(duration) * int64(m_Timec=
odeScale) / 1000000000.0);
=20
 					} else if (EbmlId(*ElementLevel2) =3D=3D KaxDateUTC::ClassInfos.Glo=
balId) {
@@ -1074,11 +1074,11 @@
 	return foundTag;
 };
=20
-uint64 MatroskaAudioParser::GetDuration() {=20
+double MatroskaAudioParser::GetDuration() {=20
 	if (m_CurrentChapter !=3D NULL) {=20
-		return m_CurrentChapter->timeEnd - m_CurrentChapter->timeStart;
+		return TimecodeToSeconds(m_CurrentChapter->timeEnd - m_CurrentChapter-=
>timeStart);
 	};
-	return m_Duration;
+	return m_Duration / 1000000000.0;
 	if (m_Tracks.size() !=3D 0) {
 		return m_Tracks.at(m_CurrentTrackNo).defaultDuration * (int64)m_Timeco=
deScale;
 	}
@@ -1520,7 +1520,7 @@
 	m_CurrentEdition =3D NULL;
 	m_CurrentChapter =3D NULL;
 	if(m_Editions.size() > 0)
-		m_CurrentEdition =3D &m_Editions.at(0);	=09
+		m_CurrentEdition =3D &m_Editions.at(0);
 	if (m_Chapters.size() > subsong)
 		m_CurrentChapter =3D &m_Chapters.at(subsong);
 =09
@@ -1529,10 +1529,10 @@
 int32 MatroskaAudioParser::GetAvgBitrate()=20
 {=20
 	double ret =3D 0;
-	ret =3D (double)(int64)m_FileSize / 1024;
-	ret =3D ret / ((double)(int64)m_Duration / (double)(int64)1000000000);=20
+	ret =3D static_cast<double>(int64(m_FileSize)) / 1024;
+	ret =3D ret / (m_Duration / 1000000000.0);
 	ret =3D ret * 8;
-	return (int32)ret;
+	return static_cast<int32>(ret);
 };
=20
 bool MatroskaAudioParser::skip_frames_until(double destination,unsigned =
& frames,double & last_timecode_delta,unsigned hint_samplerate)
@@ -1628,6 +1628,12 @@
 	}
 };
=20
+MatroskaAudioFrame * MatroskaAudioParser::ReadFirstFrame()
+{
+    m_CurrentTimecode =3D 0;
+    return ReadSingleFrame();
+};
+
 typedef boost::shared_ptr<EbmlId> EbmlIdPtr;
=20
 void MatroskaAudioParser::Parse_MetaSeek(ElementPtr metaSeekElement, boo=
l bInfoOnly)=20
@@ -1651,6 +1657,9 @@
 		if (UpperElementLevel < 0) {
 			UpperElementLevel =3D 0;
 		}
+        if (bInfoOnly) {
+            if (m_ClusterIndex.size() >=3D 1) break;
+        }
=20
 		if (EbmlId(*l2) =3D=3D KaxSeek::ClassInfos.GlobalId) {
 			//Wow we found the SeekEntries, time to speed up reading ;)
@@ -1664,6 +1673,9 @@
 				if (UpperElementLevel < 0) {
 					UpperElementLevel =3D 0;
 				}
+                if (bInfoOnly) {
+                    if (m_ClusterIndex.size() >=3D 1) break;
+                }
=20
 				if (EbmlId(*l3) =3D=3D KaxSeekID::ClassInfos.GlobalId) {
 					binary *b =3D NULL;
@@ -1682,7 +1694,7 @@
 					if (endSeekPos < lastSeekPos)
 						endSeekPos =3D uint64(seek_pos);
=20
-					if ((*id =3D=3D KaxCluster::ClassInfos.GlobalId) && !bInfoOnly) {
+					if (*id =3D=3D KaxCluster::ClassInfos.GlobalId) {
 						//NOTE1("Found Cluster Seek Entry Postion: %u", (unsigned long)las=
tSeekPos);
 						//uint64 orig_pos =3D inputFile.getFilePointer();
 						//MatroskaMetaSeekClusterEntry newCluster;
@@ -1691,13 +1703,13 @@
 						newCluster->filePos =3D static_cast<KaxSegment *>(m_ElementLevel0.=
get())->GetGlobalPosition(lastSeekPos);
 						m_ClusterIndex.push_back(newCluster);
=20
-					} else if ((*id =3D=3D KaxSeekHead::ClassInfos.GlobalId) && !bInfoO=
nly) {
+					} else if (*id =3D=3D KaxSeekHead::ClassInfos.GlobalId) {
 						NOTE1("Found MetaSeek Seek Entry Postion: %u", (unsigned long)last=
SeekPos);
 						uint64 orig_pos =3D m_IOCallback.getFilePointer();
 						m_IOCallback.setFilePointer(static_cast<KaxSegment *>(m_ElementLev=
el0.get())->GetGlobalPosition(lastSeekPos));
 					=09
 						ElementPtr levelUnknown =3D ElementPtr(m_InputStream.FindNextID(Ka=
xSeekHead::ClassInfos, 0xFFFFFFFFFFFFFFFFL));									=09
-						Parse_MetaSeek(levelUnknown, false);
+						Parse_MetaSeek(levelUnknown, bInfoOnly);
=20
 						m_IOCallback.setFilePointer(orig_pos);
 					}
@@ -2402,7 +2414,7 @@
 			return m_ClusterIndex.at(0);
 	=09
 		cluster_entry_ptr_t correctEntry;
-		double clusterDuration =3D (double)(int64)m_ClusterIndex.size() / (dou=
ble)(int64)m_Duration;
+		double clusterDuration =3D (double)(int64)m_ClusterIndex.size() / m_Du=
ration;
 		size_t clusterIndex =3D clusterDuration * (double)(int64)timecode;
 		//int lookCount =3D 0;
 		if (clusterIndex > m_ClusterIndex.size())
@@ -2505,7 +2517,7 @@
 	if (m_Chapters.size() > 0) {
 		MatroskaChapterInfo *nextChapter =3D &m_Chapters.at(m_Chapters.size()-=
1);
 		if (nextChapter->timeEnd =3D=3D 0) {
-			nextChapter->timeEnd =3D m_Duration;
+			nextChapter->timeEnd =3D static_cast<uint64>(m_Duration);
 		}
 		for (uint32 c =3D 0; c < m_Chapters.size()-1; c++) {
 			MatroskaChapterInfo &currentChapter =3D m_Chapters.at(c);=09
@@ -2516,7 +2528,7 @@
 		}
 		nextChapter =3D &m_Chapters.at(m_Chapters.size()-1);
 		if ((nextChapter->timeEnd =3D=3D 0) || (nextChapter->timeEnd =3D=3D ne=
xtChapter->timeStart)) {
-			nextChapter->timeEnd =3D m_Duration;
+			nextChapter->timeEnd =3D static_cast<uint64>(m_Duration);
 		}
 	}=09
 }

Modified: trunk/foo_input_matroska/matroska_parser.h
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- trunk/foo_input_matroska/matroska_parser.h	2006-07-17 06:46:55 UTC (r=
ev 1256)
+++ trunk/foo_input_matroska/matroska_parser.h	2006-07-17 06:48:03 UTC (r=
ev 1257)
@@ -251,7 +251,7 @@
 	MatroskaTrackInfo &GetTrack(uint16 trackNo) { return m_Tracks.at(trackN=
o); };
 	uint64 GetTimecodeScale() { return m_TimecodeScale; };
 	/// Returns an adjusted duration of the file
-	uint64 GetDuration();
+	double GetDuration();
 	/// Returns the track index of the first decodable track
 	int32 GetFirstAudioTrack();
=20
@@ -296,6 +296,7 @@
 	/// \return 1 If file could not be read or it not open=09
 	/// \return 2 End of track (EOT)
 	MatroskaAudioFrame * ReadSingleFrame();
+    MatroskaAudioFrame * ReadFirstFrame();
=20
 	UTFstring GetSegmentFileName() { return m_SegmentFilename; }
     typedef pfc::list_t<MatroskaAttachment> attachment_list_t;
@@ -358,7 +359,7 @@
     attachment_list_t m_AttachmentList;
=20
 	uint64 m_CurrentTimecode;
-	uint64 m_Duration;
+	double m_Duration;
 	uint64 m_TimecodeScale;
 	UTFstring m_WritingApp;
 	UTFstring m_MuxingApp;