1/*
2 * Copyright (c) 2005, David McPaul
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 *  * Redistributions of source code must retain the above copyright notice,
9 *    this list of conditions and the following disclaimer.
10 *  * Redistributions in binary form must reproduce the above copyright notice,
11 *    this list of conditions and the following disclaimer in the documentation
12 *    and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
18 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
22 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
23 * OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25#ifndef _ASF_FILE_READER_H
26#define _ASF_FILE_READER_H
27
28#include <File.h>
29#include <MediaDefs.h>
30#include <MediaFormats.h>
31#include <SupportDefs.h>
32
33#include <vector>
34
35extern "C" {
36	#include "libasf/asf.h"
37}
38#include "ASFIndex.h"
39
40struct ASFAudioFormat {
41	uint16 Compression;
42	uint16 NoChannels;
43	uint32 SamplesPerSec;
44	uint32 AvgBytesPerSec;
45	uint16 BlockAlign;
46	uint16 BitsPerSample;
47	uint32 extraDataSize;
48	uint8 *extraData;
49};
50
51struct ASFVideoFormat {
52	uint32 Compression;
53	uint32 VideoWidth;
54	uint32 VideoHeight;
55	uint16 Planes;
56	uint16 BitCount;
57	uint32 FrameRate;
58	uint32 FrameScale;
59	uint32 FrameCount;
60	uint32 extraDataSize;
61	uint8 *extraData;
62};
63
64// ASF file reader
65class ASFFileReader {
66private:
67	off_t	StreamSize;
68	BPositionIO	*theStream;
69	asf_file_t *asfFile;
70	asf_packet_t *packet;
71	std::vector<StreamEntry> streams;
72
73	void ParseIndex();
74
75public:
76			ASFFileReader(BPositionIO *pStream);
77	virtual	~ASFFileReader();
78
79	status_t	ParseFile();
80
81	bool	IsEndOfFile(off_t pPosition);
82	bool	IsEndOfFile();
83	bool	IsEndOfData(off_t pPosition);
84
85	// Is this a asf file
86	static bool	IsSupported(BPositionIO *source);
87
88	// How many tracks in file
89	uint32		getStreamCount();
90
91	// the Stream duration indexed by streamIndex
92	bigtime_t	getStreamDuration(uint32 streamIndex);
93
94	bool getAudioFormat(uint32 streamIndex, ASFAudioFormat *format);
95	bool getVideoFormat(uint32 streamIndex, ASFVideoFormat *format);
96
97	// The no of frames in the Stream indexed by streamIndex
98	uint32		getFrameCount(uint32 streamIndex);
99
100	// Is stream (track) a video track
101	bool		IsVideo(uint32 streamIndex);
102	// Is stream (track) a audio track
103	bool		IsAudio(uint32 streamIndex);
104
105	BPositionIO *Source() {return theStream;};
106
107	IndexEntry GetIndex(uint32 streamIndex, uint32 frameNo);
108	bool HasIndex(uint32 streamIndex, uint32 frameNo);
109
110	uint32 GetFrameForTime(uint32 streamIndex, bigtime_t time);
111
112	bool	GetNextChunkInfo(uint32 streamIndex, uint32 pFrameNo, char **buffer, uint32 *size, bool *keyframe, bigtime_t *pts);
113
114	static	int32_t read(void *opaque, void *buffer, int32_t size);
115	static	int32_t write(void *opaque, void *buffer, int32_t size);
116	static	int64_t seek(void *opaque, int64_t offset);
117};
118
119#endif
120