1 /***********************************************************************
2  *    asf.h:  for undersanding ASF(Advanced Systems Format) format
3  ***********************************************************************
4  * Copyright (C) 2007 metro <me_t_ro@yahoo.com>
5  *
6  * This file is part of msdl, media stream downloader
7  *
8  * This file is based on asf implementation of mplayer,
9  * and various information on the internet about ASF.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *
26  ***********************************************************************/
27 
28 /*
29  * Some code or data in this file is based on
30  * MPlayer project
31  * (MPlayer-1.0rc2/stream/libmpdemux/asfheader.c)
32  */
33 
34 
35 #ifndef __ASF_H__
36 #define __ASF_H__
37 
38 
39 enum {
40     ASF_GUID_LENGTH = 16,
41     ASF_HEADER_READ_AT_ONCE = 10000,
42 };
43 
44 
45 enum {
46     ASF_AUDIO_STREAM = 0xf8699e40,
47     ASF_VIDEO_STREAM = 0xbc19efc0,
48     ASF_COMMAND_MEDIA_STREAM = 0x59dacfc0,
49     ASF_FILE_TRANSFER_MEDIA_STREAM = 0x91bd222c,
50 };
51 
52 
53 
54 
55 /*
56  * ASF Object header
57  */
58 struct __attribute__((packed))  asf_obj_header_t {
59     uint8_t guid[ASF_GUID_LENGTH];
60     uint64_t size;
61 };
62 
63 
64 /*
65  * M$ ASF Header!!
66  */
67 struct __attribute__((packed)) asf_header_t {
68     struct asf_obj_header_t objh; /* object header       */
69     uint32_t cno;                 /* number of subchunks */
70     uint8_t v1;                   /* unknown (0x01)      */
71     uint8_t v2;                   /* unknown (0x02)      */
72 };
73 
74 
75 /*
76  * M$ ASF file header.
77  */
78 struct __attribute__((packed)) asf_file_header_t {
79     uint8_t  stream_id[ASF_GUID_LENGTH];  /* stream GUID */
80     uint64_t file_size;
81     uint64_t creation_time;  /* File creation time FILETIME 8                        */
82     uint64_t num_packets;    /* Number of packets UINT64 8                           */
83     uint64_t play_duration;  /* Timestamp of the end position UINT64 8               */
84     uint64_t send_duration;  /* Duration of the playback UINT64 8                    */
85     uint64_t preroll;        /* Time to bufferize before playing UINT32 4            */
86     uint32_t flags;          /* Unknown, maybe flags (usually contains 2) UINT32 4   */
87     uint32_t min_packet_size;/* Min size of the packet, in bytes UINT32 4            */
88     uint32_t max_packet_size;/* Max size of the packet  UINT32 4                     */
89     uint32_t max_bitrate;    /* Maximum bitrate of the media (sum of all the stream) */
90 };
91 
92 struct __attribute__((packed)) asf_stream_header_t {
93     uint8_t type[ASF_GUID_LENGTH];        /* Stream type (audio/video) GUID 16    */
94     uint8_t concealment[ASF_GUID_LENGTH]; /* Audio error concealment type GUID 16 */
95     uint64_t unk1;        /* Unknown, maybe reserved ( usually contains 0 ) UINT64 8 */
96     uint32_t type_size;   /* Total size of type-specific data UINT32 4               */
97     uint32_t stream_size; /* Size of stream-specific data UINT32 4                   */
98     uint16_t stream_no;   /* Stream number UINT16 2                                  */
99     uint32_t unk2;        /* Unknown UINT32 4                                        */
100 };
101 
102 
103 
104 struct __attribute__((packed)) asf_stream_chunk_t {
105     uint16_t type;
106     uint16_t size;
107     uint32_t seqnum;
108 };
109 
110 
111 
112 struct __attribute__((packed)) asf_stream_chunk_extra_t {
113     uint16_t unknown;
114     uint16_t size_confirm;
115 };
116 
117 
118 struct error_correction_data {
119     /* error correction flags */
120     int error_correction_data_length;
121     int opaque_data_present;
122     int error_correction_length_type;
123     int error_correction_present;
124 };
125 
126 
127 struct payload_parsing_information {
128     /*length type flags*/
129     int multiple_payloads_present;
130     int sequence_type;
131     int padding_length_type;
132     int packet_length_type;
133     int error_correction_present;
134     /* property flags */
135     int replicated_data_length_type;
136     int offset_into_media_object_length_type;
137     int media_object_number_length_type;
138     int stream_number_length_type;
139 
140     uint32_t packet_length;
141     uint32_t sequence;
142     uint32_t padding_length;
143     uint32_t send_time;
144     uint32_t duration;
145 };
146 
147 
148 struct asf_data_packet_header_t {
149     struct error_correction_data ecd;
150     struct payload_parsing_information ppi;
151 };
152 
153 
154 
155 
156 struct asf_streams_t {
157     int audio_id,video_id; /* best stuff. (ids)   */
158     int n_audio, n_video;  /* numbers of streams  */
159     int *audio_streams,*video_streams; /* streams */
160 };
161 
162 
163 
164 struct asf_headerinfo_t {
165     uint8_t *asf_header;              /* raw header         */
166     int asf_header_len;               /* its length         */
167     struct asf_file_header_t *fileh;  /* basic information  */
168     struct asf_streams_t *streams;    /* stream information */
169 };
170 
171 
172 
173 struct asf_headerinfo_t *new_asf_headerinfo_t(void);
174 void free_asf_headerinfo_t(struct asf_headerinfo_t *info);
175 
176 int asf_interpret_header(struct asf_headerinfo_t *hinfo,const int bw,
177 			 const uint8_t *buffer,const size_t asf_header_size);
178 
179 int find_asf_guid(const uint8_t *buffer,int cur_pos,const uint8_t *guid,int buffer_len);
180 
181 int asf_get_file_property(const uint8_t *header,int asf_header_size,
182 			  struct asf_file_header_t *fileh);
183 int asf_choose_best_streams(const uint8_t *header, int asf_header_size,
184 			    int bw, struct asf_streams_t *streams);
185 
186 int asf_get_npt_of_last_packet(char *local_filename,uint32_t *send_time,uint64_t *seek_offset);
187 
188 int asf_parse_data_packet_header(uint8_t *packet,struct asf_data_packet_header_t *adph);
189 
190 int asf_data_packet_get_send_time(uint8_t *packet,uint32_t *send_time);
191 
192 #endif /* __ASF_H__ */
193