Commit | Line | Data |
---|---|---|
de6d9b64 FB |
1 | /* |
2 | * ASF compatible encoder and decoder. | |
19720f15 | 3 | * Copyright (c) 2000, 2001 Fabrice Bellard. |
de6d9b64 | 4 | * |
19720f15 FB |
5 | * This library is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU Lesser General Public | |
7 | * License as published by the Free Software Foundation; either | |
8 | * version 2 of the License, or (at your option) any later version. | |
de6d9b64 | 9 | * |
19720f15 | 10 | * This library is distributed in the hope that it will be useful, |
de6d9b64 | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
19720f15 FB |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | * Lesser General Public License for more details. | |
de6d9b64 | 14 | * |
19720f15 FB |
15 | * You should have received a copy of the GNU Lesser General Public |
16 | * License along with this library; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
de6d9b64 | 18 | */ |
de6d9b64 FB |
19 | #include "avformat.h" |
20 | #include "avi.h" | |
f80c1ac0 | 21 | #include "mpegaudio.h" |
de6d9b64 FB |
22 | |
23 | #define PACKET_SIZE 3200 | |
24 | #define PACKET_HEADER_SIZE 12 | |
25 | #define FRAME_HEADER_SIZE 17 | |
26 | ||
27 | typedef struct { | |
28 | int num; | |
29 | int seq; | |
30 | /* use for reading */ | |
31 | AVPacket pkt; | |
32 | int frag_offset; | |
2a10020b | 33 | int timestamp; |
0c1a9eda | 34 | int64_t duration; |
2a10020b ZK |
35 | |
36 | int ds_span; /* descrambling */ | |
37 | int ds_packet_size; | |
38 | int ds_chunk_size; | |
39 | int ds_data_size; | |
40 | int ds_silence_data; | |
41 | ||
de6d9b64 FB |
42 | } ASFStream; |
43 | ||
44 | typedef struct { | |
0c1a9eda ZK |
45 | uint32_t v1; |
46 | uint16_t v2; | |
47 | uint16_t v3; | |
48 | uint8_t v4[8]; | |
2a10020b ZK |
49 | } GUID; |
50 | ||
247eadca | 51 | typedef struct { |
2a10020b ZK |
52 | GUID guid; // generated by client computer |
53 | uint64_t file_size; // in bytes | |
54 | // invalid if broadcasting | |
55 | uint64_t create_time; // time of creation, in 100-nanosecond units since 1.1.1601 | |
56 | // invalid if broadcasting | |
57 | uint64_t packets_count; // how many packets are there in the file | |
58 | // invalid if broadcasting | |
59 | uint64_t play_time; // play time, in 100-nanosecond units | |
60 | // invalid if broadcasting | |
61 | uint64_t send_time; // time to send file, in 100-nanosecond units | |
62 | // invalid if broadcasting (could be ignored) | |
63 | uint32_t preroll; // timestamp of the first packet, in milliseconds | |
64 | // if nonzero - substract from time | |
65 | uint32_t ignore; // preroll is 64bit - but let's just ignore it | |
66 | uint32_t flags; // 0x01 - broadcast | |
67 | // 0x02 - seekable | |
68 | // rest is reserved should be 0 | |
69 | uint32_t min_pktsize; // size of a data packet | |
70 | // invalid if broadcasting | |
71 | uint32_t max_pktsize; // shall be the same as for min_pktsize | |
72 | // invalid if broadcasting | |
73 | uint32_t max_bitrate; // bandwith of stream in bps | |
74 | // should be the sum of bitrates of the | |
75 | // individual media streams | |
76 | } ASFMainHeader; | |
77 | ||
78 | ||
79 | typedef struct { | |
de6d9b64 FB |
80 | int seqno; |
81 | int packet_size; | |
8b3c13f9 | 82 | int is_streamed; |
2141dc37 ZK |
83 | int asfid2avid[128]; /* conversion table from asf ID 2 AVStream ID */ |
84 | ASFStream streams[128]; /* it's max number and it's not that big */ | |
de6d9b64 | 85 | /* non streamed additonnal info */ |
0c1a9eda ZK |
86 | int64_t nb_packets; |
87 | int64_t duration; /* in 100ns units */ | |
de6d9b64 FB |
88 | /* packet filling */ |
89 | int packet_size_left; | |
90 | int packet_timestamp_start; | |
91 | int packet_timestamp_end; | |
92 | int packet_nb_frames; | |
0c1a9eda | 93 | uint8_t packet_buf[PACKET_SIZE]; |
de6d9b64 FB |
94 | ByteIOContext pb; |
95 | /* only for reading */ | |
2a10020b ZK |
96 | uint64_t data_offset; /* begining of the first data packet */ |
97 | ||
98 | ASFMainHeader hdr; | |
99 | ||
100 | int packet_flags; | |
101 | int packet_property; | |
102 | int packet_timestamp; | |
103 | int packet_segsizetype; | |
104 | int packet_segments; | |
105 | int packet_seq; | |
106 | int packet_replic_size; | |
107 | int packet_key_frame; | |
de6d9b64 | 108 | int packet_padsize; |
2a10020b ZK |
109 | int packet_frag_offset; |
110 | int packet_frag_size; | |
111 | int packet_frag_timestamp; | |
2141dc37 | 112 | int packet_multi_size; |
2a10020b ZK |
113 | int packet_obj_size; |
114 | int packet_time_delta; | |
115 | int packet_time_start; | |
116 | ||
117 | int stream_index; | |
118 | ASFStream* asf_st; /* currently decoded stream */ | |
de6d9b64 FB |
119 | } ASFContext; |
120 | ||
de6d9b64 FB |
121 | static const GUID asf_header = { |
122 | 0x75B22630, 0x668E, 0x11CF, { 0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C }, | |
123 | }; | |
124 | ||
125 | static const GUID file_header = { | |
126 | 0x8CABDCA1, 0xA947, 0x11CF, { 0x8E, 0xE4, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65 }, | |
127 | }; | |
128 | ||
129 | static const GUID stream_header = { | |
130 | 0xB7DC0791, 0xA9B7, 0x11CF, { 0x8E, 0xE6, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65 }, | |
131 | }; | |
132 | ||
133 | static const GUID audio_stream = { | |
134 | 0xF8699E40, 0x5B4D, 0x11CF, { 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B }, | |
135 | }; | |
136 | ||
137 | static const GUID audio_conceal_none = { | |
f80c1ac0 PG |
138 | // 0x49f1a440, 0x4ece, 0x11d0, { 0xa3, 0xac, 0x00, 0xa0, 0xc9, 0x03, 0x48, 0xf6 }, |
139 | // New value lifted from avifile | |
140 | 0x20fb5700, 0x5b55, 0x11cf, { 0xa8, 0xfd, 0x00, 0x80, 0x5f, 0x5c, 0x44, 0x2b }, | |
de6d9b64 FB |
141 | }; |
142 | ||
143 | static const GUID video_stream = { | |
144 | 0xBC19EFC0, 0x5B4D, 0x11CF, { 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B }, | |
145 | }; | |
146 | ||
147 | static const GUID video_conceal_none = { | |
148 | 0x20FB5700, 0x5B55, 0x11CF, { 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B }, | |
149 | }; | |
150 | ||
151 | ||
152 | static const GUID comment_header = { | |
153 | 0x75b22633, 0x668e, 0x11cf, { 0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c }, | |
154 | }; | |
155 | ||
156 | static const GUID codec_comment_header = { | |
157 | 0x86D15240, 0x311D, 0x11D0, { 0xA3, 0xA4, 0x00, 0xA0, 0xC9, 0x03, 0x48, 0xF6 }, | |
158 | }; | |
159 | static const GUID codec_comment1_header = { | |
160 | 0x86d15241, 0x311d, 0x11d0, { 0xa3, 0xa4, 0x00, 0xa0, 0xc9, 0x03, 0x48, 0xf6 }, | |
161 | }; | |
162 | ||
163 | static const GUID data_header = { | |
164 | 0x75b22636, 0x668e, 0x11cf, { 0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c }, | |
165 | }; | |
166 | ||
167 | static const GUID index_guid = { | |
168 | 0x33000890, 0xe5b1, 0x11cf, { 0x89, 0xf4, 0x00, 0xa0, 0xc9, 0x03, 0x49, 0xcb }, | |
169 | }; | |
170 | ||
171 | static const GUID head1_guid = { | |
172 | 0x5fbf03b5, 0xa92e, 0x11cf, { 0x8e, 0xe3, 0x00, 0xc0, 0x0c, 0x20, 0x53, 0x65 }, | |
173 | }; | |
174 | ||
175 | static const GUID head2_guid = { | |
176 | 0xabd3d211, 0xa9ba, 0x11cf, { 0x8e, 0xe6, 0x00, 0xc0, 0x0c, 0x20, 0x53, 0x65 }, | |
177 | }; | |
2a10020b | 178 | |
de6d9b64 FB |
179 | /* I am not a number !!! This GUID is the one found on the PC used to |
180 | generate the stream */ | |
181 | static const GUID my_guid = { | |
182 | 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0, 0 }, | |
183 | }; | |
184 | ||
764ef400 | 185 | #ifdef CONFIG_ENCODERS |
de6d9b64 FB |
186 | static void put_guid(ByteIOContext *s, const GUID *g) |
187 | { | |
188 | int i; | |
189 | ||
190 | put_le32(s, g->v1); | |
191 | put_le16(s, g->v2); | |
192 | put_le16(s, g->v3); | |
193 | for(i=0;i<8;i++) | |
194 | put_byte(s, g->v4[i]); | |
195 | } | |
196 | ||
197 | static void put_str16(ByteIOContext *s, const char *tag) | |
198 | { | |
199 | int c; | |
200 | ||
201 | put_le16(s,strlen(tag) + 1); | |
202 | for(;;) { | |
0c1a9eda | 203 | c = (uint8_t)*tag++; |
de6d9b64 | 204 | put_le16(s, c); |
2a10020b | 205 | if (c == '\0') |
de6d9b64 FB |
206 | break; |
207 | } | |
208 | } | |
209 | ||
210 | static void put_str16_nolen(ByteIOContext *s, const char *tag) | |
211 | { | |
212 | int c; | |
213 | ||
214 | for(;;) { | |
0c1a9eda | 215 | c = (uint8_t)*tag++; |
de6d9b64 | 216 | put_le16(s, c); |
2a10020b | 217 | if (c == '\0') |
de6d9b64 FB |
218 | break; |
219 | } | |
220 | } | |
221 | ||
0c1a9eda | 222 | static int64_t put_header(ByteIOContext *pb, const GUID *g) |
de6d9b64 | 223 | { |
0c1a9eda | 224 | int64_t pos; |
de6d9b64 FB |
225 | |
226 | pos = url_ftell(pb); | |
227 | put_guid(pb, g); | |
228 | put_le64(pb, 24); | |
229 | return pos; | |
230 | } | |
231 | ||
232 | /* update header size */ | |
0c1a9eda | 233 | static void end_header(ByteIOContext *pb, int64_t pos) |
de6d9b64 | 234 | { |
0c1a9eda | 235 | int64_t pos1; |
de6d9b64 FB |
236 | |
237 | pos1 = url_ftell(pb); | |
238 | url_fseek(pb, pos + 16, SEEK_SET); | |
239 | put_le64(pb, pos1 - pos); | |
240 | url_fseek(pb, pos1, SEEK_SET); | |
241 | } | |
242 | ||
243 | /* write an asf chunk (only used in streaming case) */ | |
f80c1ac0 | 244 | static void put_chunk(AVFormatContext *s, int type, int payload_length, int flags) |
de6d9b64 FB |
245 | { |
246 | ASFContext *asf = s->priv_data; | |
247 | ByteIOContext *pb = &s->pb; | |
248 | int length; | |
249 | ||
250 | length = payload_length + 8; | |
2a10020b ZK |
251 | put_le16(pb, type); |
252 | put_le16(pb, length); | |
de6d9b64 | 253 | put_le32(pb, asf->seqno); |
f80c1ac0 | 254 | put_le16(pb, flags); /* unknown bytes */ |
de6d9b64 FB |
255 | put_le16(pb, length); |
256 | asf->seqno++; | |
257 | } | |
258 | ||
259 | /* convert from unix to windows time */ | |
0c1a9eda | 260 | static int64_t unix_to_file_time(int ti) |
de6d9b64 | 261 | { |
0c1a9eda | 262 | int64_t t; |
2a10020b | 263 | |
0c1a9eda ZK |
264 | t = ti * int64_t_C(10000000); |
265 | t += int64_t_C(116444736000000000); | |
de6d9b64 FB |
266 | return t; |
267 | } | |
268 | ||
269 | /* write the header (used two times if non streamed) */ | |
0c1a9eda | 270 | static int asf_write_header1(AVFormatContext *s, int64_t file_size, int64_t data_chunk_size) |
de6d9b64 FB |
271 | { |
272 | ASFContext *asf = s->priv_data; | |
273 | ByteIOContext *pb = &s->pb; | |
274 | int header_size, n, extra_size, extra_size2, wav_extra_size, file_time; | |
275 | int has_title; | |
276 | AVCodecContext *enc; | |
0c1a9eda | 277 | int64_t header_offset, cur_pos, hpos; |
f80c1ac0 | 278 | int bit_rate; |
de6d9b64 | 279 | |
084fada8 | 280 | has_title = (s->title[0] || s->author[0] || s->copyright[0] || s->comment[0]); |
de6d9b64 | 281 | |
f80c1ac0 PG |
282 | bit_rate = 0; |
283 | for(n=0;n<s->nb_streams;n++) { | |
284 | enc = &s->streams[n]->codec; | |
285 | ||
286 | bit_rate += enc->bit_rate; | |
287 | } | |
288 | ||
8b3c13f9 | 289 | if (asf->is_streamed) { |
f80c1ac0 | 290 | put_chunk(s, 0x4824, 0, 0xc00); /* start of stream (length will be patched later) */ |
de6d9b64 | 291 | } |
f80c1ac0 PG |
292 | |
293 | put_guid(pb, &asf_header); | |
2a10020b | 294 | put_le64(pb, -1); /* header length, will be patched after */ |
f80c1ac0 PG |
295 | put_le32(pb, 3 + has_title + s->nb_streams); /* number of chunks in header */ |
296 | put_byte(pb, 1); /* ??? */ | |
297 | put_byte(pb, 2); /* ??? */ | |
2a10020b | 298 | |
de6d9b64 FB |
299 | /* file header */ |
300 | header_offset = url_ftell(pb); | |
301 | hpos = put_header(pb, &file_header); | |
302 | put_guid(pb, &my_guid); | |
303 | put_le64(pb, file_size); | |
304 | file_time = 0; | |
305 | put_le64(pb, unix_to_file_time(file_time)); | |
306 | put_le64(pb, asf->nb_packets); /* number of packets */ | |
307 | put_le64(pb, asf->duration); /* end time stamp (in 100ns units) */ | |
308 | put_le64(pb, asf->duration); /* duration (in 100ns units) */ | |
2a10020b ZK |
309 | put_le32(pb, 0); /* start time stamp */ |
310 | put_le32(pb, 0); /* ??? */ | |
311 | put_le32(pb, asf->is_streamed ? 1 : 0); /* ??? */ | |
de6d9b64 FB |
312 | put_le32(pb, asf->packet_size); /* packet size */ |
313 | put_le32(pb, asf->packet_size); /* packet size */ | |
f80c1ac0 | 314 | put_le32(pb, bit_rate); /* Nominal data rate in bps */ |
de6d9b64 FB |
315 | end_header(pb, hpos); |
316 | ||
317 | /* unknown headers */ | |
318 | hpos = put_header(pb, &head1_guid); | |
319 | put_guid(pb, &head2_guid); | |
320 | put_le32(pb, 6); | |
321 | put_le16(pb, 0); | |
322 | end_header(pb, hpos); | |
323 | ||
324 | /* title and other infos */ | |
325 | if (has_title) { | |
326 | hpos = put_header(pb, &comment_header); | |
327 | put_le16(pb, 2 * (strlen(s->title) + 1)); | |
328 | put_le16(pb, 2 * (strlen(s->author) + 1)); | |
329 | put_le16(pb, 2 * (strlen(s->copyright) + 1)); | |
330 | put_le16(pb, 2 * (strlen(s->comment) + 1)); | |
331 | put_le16(pb, 0); | |
332 | put_str16_nolen(pb, s->title); | |
333 | put_str16_nolen(pb, s->author); | |
334 | put_str16_nolen(pb, s->copyright); | |
335 | put_str16_nolen(pb, s->comment); | |
336 | end_header(pb, hpos); | |
337 | } | |
338 | ||
339 | /* stream headers */ | |
340 | for(n=0;n<s->nb_streams;n++) { | |
0c1a9eda | 341 | int64_t es_pos; |
17a241dd | 342 | // ASFStream *stream = &asf->streams[n]; |
f80c1ac0 | 343 | |
de6d9b64 FB |
344 | enc = &s->streams[n]->codec; |
345 | asf->streams[n].num = n + 1; | |
346 | asf->streams[n].seq = 0; | |
2a10020b | 347 | |
de6d9b64 FB |
348 | switch(enc->codec_type) { |
349 | case CODEC_TYPE_AUDIO: | |
350 | wav_extra_size = 0; | |
351 | extra_size = 18 + wav_extra_size; | |
352 | extra_size2 = 0; | |
353 | break; | |
354 | default: | |
355 | case CODEC_TYPE_VIDEO: | |
356 | wav_extra_size = 0; | |
357 | extra_size = 0x33; | |
358 | extra_size2 = 0; | |
359 | break; | |
360 | } | |
361 | ||
362 | hpos = put_header(pb, &stream_header); | |
363 | if (enc->codec_type == CODEC_TYPE_AUDIO) { | |
364 | put_guid(pb, &audio_stream); | |
365 | put_guid(pb, &audio_conceal_none); | |
366 | } else { | |
367 | put_guid(pb, &video_stream); | |
368 | put_guid(pb, &video_conceal_none); | |
369 | } | |
370 | put_le64(pb, 0); /* ??? */ | |
f80c1ac0 | 371 | es_pos = url_ftell(pb); |
de6d9b64 FB |
372 | put_le32(pb, extra_size); /* wav header len */ |
373 | put_le32(pb, extra_size2); /* additional data len */ | |
374 | put_le16(pb, n + 1); /* stream number */ | |
375 | put_le32(pb, 0); /* ??? */ | |
2a10020b | 376 | |
de6d9b64 FB |
377 | if (enc->codec_type == CODEC_TYPE_AUDIO) { |
378 | /* WAVEFORMATEX header */ | |
f80c1ac0 PG |
379 | int wavsize = put_wav_header(pb, enc); |
380 | ||
381 | if (wavsize < 0) | |
46a3d068 | 382 | return -1; |
f80c1ac0 PG |
383 | if (wavsize != extra_size) { |
384 | cur_pos = url_ftell(pb); | |
385 | url_fseek(pb, es_pos, SEEK_SET); | |
386 | put_le32(pb, wavsize); /* wav header len */ | |
387 | url_fseek(pb, cur_pos, SEEK_SET); | |
388 | } | |
de6d9b64 FB |
389 | } else { |
390 | put_le32(pb, enc->width); | |
391 | put_le32(pb, enc->height); | |
392 | put_byte(pb, 2); /* ??? */ | |
393 | put_le16(pb, 40); /* size */ | |
394 | ||
395 | /* BITMAPINFOHEADER header */ | |
2727c35e | 396 | put_bmp_header(pb, enc, codec_bmp_tags, 1); |
de6d9b64 FB |
397 | } |
398 | end_header(pb, hpos); | |
399 | } | |
400 | ||
401 | /* media comments */ | |
402 | ||
403 | hpos = put_header(pb, &codec_comment_header); | |
404 | put_guid(pb, &codec_comment1_header); | |
405 | put_le32(pb, s->nb_streams); | |
406 | for(n=0;n<s->nb_streams;n++) { | |
084fada8 PG |
407 | AVCodec *p; |
408 | ||
de6d9b64 | 409 | enc = &s->streams[n]->codec; |
084fada8 | 410 | p = avcodec_find_encoder(enc->codec_id); |
de6d9b64 FB |
411 | |
412 | put_le16(pb, asf->streams[n].num); | |
084fada8 | 413 | put_str16(pb, p ? p->name : enc->codec_name); |
de6d9b64 | 414 | put_le16(pb, 0); /* no parameters */ |
bd5a6020 MN |
415 | |
416 | ||
de6d9b64 FB |
417 | /* id */ |
418 | if (enc->codec_type == CODEC_TYPE_AUDIO) { | |
419 | put_le16(pb, 2); | |
bd5a6020 MN |
420 | if(!enc->codec_tag) |
421 | enc->codec_tag = codec_get_tag(codec_wav_tags, enc->codec_id); | |
422 | if(!enc->codec_tag) | |
423 | return -1; | |
424 | put_le16(pb, enc->codec_tag); | |
de6d9b64 FB |
425 | } else { |
426 | put_le16(pb, 4); | |
bd5a6020 MN |
427 | if(!enc->codec_tag) |
428 | enc->codec_tag = codec_get_tag(codec_bmp_tags, enc->codec_id); | |
429 | if(!enc->codec_tag) | |
430 | return -1; | |
431 | put_le32(pb, enc->codec_tag); | |
de6d9b64 FB |
432 | } |
433 | } | |
434 | end_header(pb, hpos); | |
435 | ||
436 | /* patch the header size fields */ | |
437 | ||
438 | cur_pos = url_ftell(pb); | |
439 | header_size = cur_pos - header_offset; | |
8b3c13f9 | 440 | if (asf->is_streamed) { |
f80c1ac0 PG |
441 | header_size += 8 + 30 + 50; |
442 | ||
443 | url_fseek(pb, header_offset - 10 - 30, SEEK_SET); | |
de6d9b64 | 444 | put_le16(pb, header_size); |
f80c1ac0 | 445 | url_fseek(pb, header_offset - 2 - 30, SEEK_SET); |
de6d9b64 | 446 | put_le16(pb, header_size); |
f80c1ac0 PG |
447 | |
448 | header_size -= 8 + 30 + 50; | |
de6d9b64 | 449 | } |
f80c1ac0 PG |
450 | header_size += 24 + 6; |
451 | url_fseek(pb, header_offset - 14, SEEK_SET); | |
452 | put_le64(pb, header_size); | |
de6d9b64 FB |
453 | url_fseek(pb, cur_pos, SEEK_SET); |
454 | ||
455 | /* movie chunk, followed by packets of packet_size */ | |
456 | asf->data_offset = cur_pos; | |
457 | put_guid(pb, &data_header); | |
458 | put_le64(pb, data_chunk_size); | |
459 | put_guid(pb, &my_guid); | |
460 | put_le64(pb, asf->nb_packets); /* nb packets */ | |
461 | put_byte(pb, 1); /* ??? */ | |
462 | put_byte(pb, 1); /* ??? */ | |
463 | return 0; | |
464 | } | |
465 | ||
466 | static int asf_write_header(AVFormatContext *s) | |
467 | { | |
c9a65ca8 | 468 | ASFContext *asf = s->priv_data; |
de6d9b64 | 469 | |
17a241dd FB |
470 | av_set_pts_info(s, 32, 1, 1000); /* 32 bit pts in ms */ |
471 | ||
de6d9b64 FB |
472 | asf->packet_size = PACKET_SIZE; |
473 | asf->nb_packets = 0; | |
474 | ||
f80c1ac0 | 475 | if (asf_write_header1(s, 0, 50) < 0) { |
2824c473 | 476 | //av_free(asf); |
46a3d068 FB |
477 | return -1; |
478 | } | |
de6d9b64 FB |
479 | |
480 | put_flush_packet(&s->pb); | |
481 | ||
482 | asf->packet_nb_frames = 0; | |
483 | asf->packet_timestamp_start = -1; | |
484 | asf->packet_timestamp_end = -1; | |
485 | asf->packet_size_left = asf->packet_size - PACKET_HEADER_SIZE; | |
486 | init_put_byte(&asf->pb, asf->packet_buf, asf->packet_size, 1, | |
487 | NULL, NULL, NULL, NULL); | |
488 | ||
489 | return 0; | |
490 | } | |
491 | ||
8b3c13f9 PG |
492 | static int asf_write_stream_header(AVFormatContext *s) |
493 | { | |
494 | ASFContext *asf = s->priv_data; | |
495 | ||
496 | asf->is_streamed = 1; | |
497 | ||
498 | return asf_write_header(s); | |
499 | } | |
500 | ||
de6d9b64 | 501 | /* write a fixed size packet */ |
2a10020b ZK |
502 | static int put_packet(AVFormatContext *s, |
503 | unsigned int timestamp, unsigned int duration, | |
de6d9b64 FB |
504 | int nb_frames, int padsize) |
505 | { | |
506 | ASFContext *asf = s->priv_data; | |
507 | ByteIOContext *pb = &s->pb; | |
508 | int flags; | |
509 | ||
8b3c13f9 | 510 | if (asf->is_streamed) { |
f80c1ac0 | 511 | put_chunk(s, 0x4424, asf->packet_size, 0); |
de6d9b64 FB |
512 | } |
513 | ||
514 | put_byte(pb, 0x82); | |
515 | put_le16(pb, 0); | |
2a10020b | 516 | |
de6d9b64 FB |
517 | flags = 0x01; /* nb segments present */ |
518 | if (padsize > 0) { | |
519 | if (padsize < 256) | |
520 | flags |= 0x08; | |
521 | else | |
522 | flags |= 0x10; | |
523 | } | |
524 | put_byte(pb, flags); /* flags */ | |
525 | put_byte(pb, 0x5d); | |
526 | if (flags & 0x10) | |
f80c1ac0 | 527 | put_le16(pb, padsize - 2); |
de6d9b64 | 528 | if (flags & 0x08) |
f80c1ac0 | 529 | put_byte(pb, padsize - 1); |
de6d9b64 FB |
530 | put_le32(pb, timestamp); |
531 | put_le16(pb, duration); | |
532 | put_byte(pb, nb_frames | 0x80); | |
f80c1ac0 PG |
533 | |
534 | return PACKET_HEADER_SIZE + ((flags & 0x18) >> 3); | |
de6d9b64 FB |
535 | } |
536 | ||
537 | static void flush_packet(AVFormatContext *s) | |
538 | { | |
539 | ASFContext *asf = s->priv_data; | |
540 | int hdr_size, ptr; | |
2a10020b ZK |
541 | |
542 | hdr_size = put_packet(s, asf->packet_timestamp_start, | |
543 | asf->packet_timestamp_end - asf->packet_timestamp_start, | |
de6d9b64 | 544 | asf->packet_nb_frames, asf->packet_size_left); |
2a10020b | 545 | |
f80c1ac0 PG |
546 | /* Clear out the padding bytes */ |
547 | ptr = asf->packet_size - hdr_size - asf->packet_size_left; | |
de6d9b64 | 548 | memset(asf->packet_buf + ptr, 0, asf->packet_size_left); |
2a10020b | 549 | |
de6d9b64 | 550 | put_buffer(&s->pb, asf->packet_buf, asf->packet_size - hdr_size); |
2a10020b | 551 | |
de6d9b64 FB |
552 | put_flush_packet(&s->pb); |
553 | asf->nb_packets++; | |
554 | asf->packet_nb_frames = 0; | |
555 | asf->packet_timestamp_start = -1; | |
556 | asf->packet_timestamp_end = -1; | |
557 | asf->packet_size_left = asf->packet_size - PACKET_HEADER_SIZE; | |
558 | init_put_byte(&asf->pb, asf->packet_buf, asf->packet_size, 1, | |
559 | NULL, NULL, NULL, NULL); | |
560 | } | |
561 | ||
562 | static void put_frame_header(AVFormatContext *s, ASFStream *stream, int timestamp, | |
563 | int payload_size, int frag_offset, int frag_len) | |
564 | { | |
565 | ASFContext *asf = s->priv_data; | |
566 | ByteIOContext *pb = &asf->pb; | |
567 | int val; | |
568 | ||
569 | val = stream->num; | |
492cd3a9 | 570 | if (s->streams[val - 1]->codec.coded_frame->key_frame /* && frag_offset == 0 */) |
de6d9b64 FB |
571 | val |= 0x80; |
572 | put_byte(pb, val); | |
573 | put_byte(pb, stream->seq); | |
574 | put_le32(pb, frag_offset); /* fragment offset */ | |
575 | put_byte(pb, 0x08); /* flags */ | |
576 | put_le32(pb, payload_size); | |
577 | put_le32(pb, timestamp); | |
578 | put_le16(pb, frag_len); | |
579 | } | |
580 | ||
581 | ||
582 | /* Output a frame. We suppose that payload_size <= PACKET_SIZE. | |
583 | ||
584 | It is there that you understand that the ASF format is really | |
2a10020b | 585 | crap. They have misread the MPEG Systems spec ! |
de6d9b64 FB |
586 | */ |
587 | static void put_frame(AVFormatContext *s, ASFStream *stream, int timestamp, | |
49057904 | 588 | const uint8_t *buf, int payload_size) |
de6d9b64 FB |
589 | { |
590 | ASFContext *asf = s->priv_data; | |
591 | int frag_pos, frag_len, frag_len1; | |
2a10020b | 592 | |
de6d9b64 FB |
593 | frag_pos = 0; |
594 | while (frag_pos < payload_size) { | |
595 | frag_len = payload_size - frag_pos; | |
596 | frag_len1 = asf->packet_size_left - FRAME_HEADER_SIZE; | |
597 | if (frag_len1 > 0) { | |
598 | if (frag_len > frag_len1) | |
599 | frag_len = frag_len1; | |
b13a517f | 600 | put_frame_header(s, stream, timestamp+1, payload_size, frag_pos, frag_len); |
de6d9b64 FB |
601 | put_buffer(&asf->pb, buf, frag_len); |
602 | asf->packet_size_left -= (frag_len + FRAME_HEADER_SIZE); | |
603 | asf->packet_timestamp_end = timestamp; | |
604 | if (asf->packet_timestamp_start == -1) | |
605 | asf->packet_timestamp_start = timestamp; | |
606 | asf->packet_nb_frames++; | |
607 | } else { | |
608 | frag_len = 0; | |
609 | } | |
610 | frag_pos += frag_len; | |
611 | buf += frag_len; | |
612 | /* output the frame if filled */ | |
613 | if (asf->packet_size_left <= FRAME_HEADER_SIZE) | |
614 | flush_packet(s); | |
615 | } | |
616 | stream->seq++; | |
617 | } | |
618 | ||
619 | ||
620 | static int asf_write_packet(AVFormatContext *s, int stream_index, | |
49057904 | 621 | const uint8_t *buf, int size, int64_t timestamp) |
de6d9b64 FB |
622 | { |
623 | ASFContext *asf = s->priv_data; | |
e0d2714a | 624 | ASFStream *stream; |
0c1a9eda | 625 | int64_t duration; |
de6d9b64 FB |
626 | AVCodecContext *codec; |
627 | ||
628 | codec = &s->streams[stream_index]->codec; | |
e0d2714a | 629 | stream = &asf->streams[stream_index]; |
2a10020b | 630 | |
de6d9b64 | 631 | if (codec->codec_type == CODEC_TYPE_AUDIO) { |
0c1a9eda | 632 | duration = (codec->frame_number * codec->frame_size * int64_t_C(10000000)) / |
de6d9b64 FB |
633 | codec->sample_rate; |
634 | } else { | |
14bea432 | 635 | duration = av_rescale(codec->frame_number * codec->frame_rate_base, 10000000, codec->frame_rate); |
de6d9b64 FB |
636 | } |
637 | if (duration > asf->duration) | |
638 | asf->duration = duration; | |
2a10020b | 639 | |
e0d2714a | 640 | put_frame(s, stream, timestamp, buf, size); |
de6d9b64 FB |
641 | return 0; |
642 | } | |
2a10020b | 643 | |
de6d9b64 FB |
644 | static int asf_write_trailer(AVFormatContext *s) |
645 | { | |
646 | ASFContext *asf = s->priv_data; | |
0c1a9eda | 647 | int64_t file_size; |
de6d9b64 FB |
648 | |
649 | /* flush the current packet */ | |
650 | if (asf->pb.buf_ptr > asf->pb.buffer) | |
651 | flush_packet(s); | |
652 | ||
8b3c13f9 | 653 | if (asf->is_streamed) { |
f80c1ac0 | 654 | put_chunk(s, 0x4524, 0, 0); /* end of stream */ |
de6d9b64 FB |
655 | } else { |
656 | /* rewrite an updated header */ | |
657 | file_size = url_ftell(&s->pb); | |
658 | url_fseek(&s->pb, 0, SEEK_SET); | |
659 | asf_write_header1(s, file_size, file_size - asf->data_offset); | |
660 | } | |
661 | ||
662 | put_flush_packet(&s->pb); | |
de6d9b64 FB |
663 | return 0; |
664 | } | |
764ef400 | 665 | #endif //CONFIG_ENCODERS |
de6d9b64 FB |
666 | |
667 | /**********************************/ | |
668 | /* decoding */ | |
669 | ||
670 | //#define DEBUG | |
671 | ||
672 | #ifdef DEBUG | |
5082f759 FR |
673 | #define PRINT_IF_GUID(g,cmp) \ |
674 | if (!memcmp(g, &cmp, sizeof(GUID))) \ | |
675 | printf("(GUID: %s) ", #cmp) | |
676 | ||
de6d9b64 FB |
677 | static void print_guid(const GUID *g) |
678 | { | |
679 | int i; | |
5082f759 FR |
680 | PRINT_IF_GUID(g, asf_header); |
681 | else PRINT_IF_GUID(g, file_header); | |
682 | else PRINT_IF_GUID(g, stream_header); | |
683 | else PRINT_IF_GUID(g, audio_stream); | |
684 | else PRINT_IF_GUID(g, audio_conceal_none); | |
685 | else PRINT_IF_GUID(g, video_stream); | |
686 | else PRINT_IF_GUID(g, video_conceal_none); | |
687 | else PRINT_IF_GUID(g, comment_header); | |
688 | else PRINT_IF_GUID(g, codec_comment_header); | |
689 | else PRINT_IF_GUID(g, codec_comment1_header); | |
690 | else PRINT_IF_GUID(g, data_header); | |
691 | else PRINT_IF_GUID(g, index_guid); | |
692 | else PRINT_IF_GUID(g, head1_guid); | |
693 | else PRINT_IF_GUID(g, head2_guid); | |
694 | else PRINT_IF_GUID(g, my_guid); | |
695 | else | |
696 | printf("(GUID: unknown) "); | |
de6d9b64 | 697 | printf("0x%08x, 0x%04x, 0x%04x, {", g->v1, g->v2, g->v3); |
2a10020b | 698 | for(i=0;i<8;i++) |
de6d9b64 FB |
699 | printf(" 0x%02x,", g->v4[i]); |
700 | printf("}\n"); | |
701 | } | |
5082f759 | 702 | #undef PRINT_IF_GUID(g,cmp) |
de6d9b64 FB |
703 | #endif |
704 | ||
705 | static void get_guid(ByteIOContext *s, GUID *g) | |
706 | { | |
707 | int i; | |
708 | ||
709 | g->v1 = get_le32(s); | |
710 | g->v2 = get_le16(s); | |
711 | g->v3 = get_le16(s); | |
712 | for(i=0;i<8;i++) | |
713 | g->v4[i] = get_byte(s); | |
714 | } | |
715 | ||
716 | #if 0 | |
717 | static void get_str16(ByteIOContext *pb, char *buf, int buf_size) | |
718 | { | |
719 | int len, c; | |
720 | char *q; | |
721 | ||
722 | len = get_le16(pb); | |
723 | q = buf; | |
724 | while (len > 0) { | |
725 | c = get_le16(pb); | |
726 | if ((q - buf) < buf_size - 1) | |
727 | *q++ = c; | |
728 | len--; | |
729 | } | |
730 | *q = '\0'; | |
731 | } | |
732 | #endif | |
733 | ||
734 | static void get_str16_nolen(ByteIOContext *pb, int len, char *buf, int buf_size) | |
735 | { | |
736 | int c; | |
737 | char *q; | |
738 | ||
739 | q = buf; | |
740 | while (len > 0) { | |
741 | c = get_le16(pb); | |
742 | if ((q - buf) < buf_size - 1) | |
743 | *q++ = c; | |
744 | len-=2; | |
745 | } | |
746 | *q = '\0'; | |
747 | } | |
748 | ||
c9a65ca8 FB |
749 | static int asf_probe(AVProbeData *pd) |
750 | { | |
751 | GUID g; | |
752 | const unsigned char *p; | |
753 | int i; | |
754 | ||
755 | /* check file header */ | |
756 | if (pd->buf_size <= 32) | |
757 | return 0; | |
758 | p = pd->buf; | |
759 | g.v1 = p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24); | |
760 | p += 4; | |
761 | g.v2 = p[0] | (p[1] << 8); | |
762 | p += 2; | |
763 | g.v3 = p[0] | (p[1] << 8); | |
764 | p += 2; | |
765 | for(i=0;i<8;i++) | |
766 | g.v4[i] = *p++; | |
767 | ||
768 | if (!memcmp(&g, &asf_header, sizeof(GUID))) | |
769 | return AVPROBE_SCORE_MAX; | |
770 | else | |
771 | return 0; | |
772 | } | |
773 | ||
de6d9b64 FB |
774 | static int asf_read_header(AVFormatContext *s, AVFormatParameters *ap) |
775 | { | |
c9a65ca8 | 776 | ASFContext *asf = s->priv_data; |
de6d9b64 FB |
777 | GUID g; |
778 | ByteIOContext *pb = &s->pb; | |
779 | AVStream *st; | |
780 | ASFStream *asf_st; | |
e095026a | 781 | int size, i; |
0c1a9eda | 782 | int64_t gsize; |
de6d9b64 | 783 | |
17a241dd FB |
784 | av_set_pts_info(s, 32, 1, 1000); /* 32 bit pts in ms */ |
785 | ||
de6d9b64 FB |
786 | get_guid(pb, &g); |
787 | if (memcmp(&g, &asf_header, sizeof(GUID))) | |
788 | goto fail; | |
789 | get_le64(pb); | |
790 | get_le32(pb); | |
791 | get_byte(pb); | |
792 | get_byte(pb); | |
2141dc37 | 793 | memset(&asf->asfid2avid, -1, sizeof(asf->asfid2avid)); |
de6d9b64 FB |
794 | for(;;) { |
795 | get_guid(pb, &g); | |
796 | gsize = get_le64(pb); | |
797 | #ifdef DEBUG | |
798 | printf("%08Lx: ", url_ftell(pb) - 24); | |
799 | print_guid(&g); | |
800 | printf(" size=0x%Lx\n", gsize); | |
801 | #endif | |
802 | if (gsize < 24) | |
803 | goto fail; | |
804 | if (!memcmp(&g, &file_header, sizeof(GUID))) { | |
2a10020b ZK |
805 | get_guid(pb, &asf->hdr.guid); |
806 | asf->hdr.file_size = get_le64(pb); | |
807 | asf->hdr.create_time = get_le64(pb); | |
808 | asf->hdr.packets_count = get_le64(pb); | |
809 | asf->hdr.play_time = get_le64(pb); | |
810 | asf->hdr.send_time = get_le64(pb); | |
811 | asf->hdr.preroll = get_le32(pb); | |
812 | asf->hdr.ignore = get_le32(pb); | |
813 | asf->hdr.flags = get_le32(pb); | |
814 | asf->hdr.min_pktsize = get_le32(pb); | |
815 | asf->hdr.max_pktsize = get_le32(pb); | |
816 | asf->hdr.max_bitrate = get_le32(pb); | |
2141dc37 ZK |
817 | asf->packet_size = asf->hdr.max_pktsize; |
818 | asf->nb_packets = asf->hdr.packets_count; | |
de6d9b64 | 819 | } else if (!memcmp(&g, &stream_header, sizeof(GUID))) { |
2e7973bb | 820 | int type, total_size, type_specific_size; |
de6d9b64 | 821 | unsigned int tag1; |
0c1a9eda | 822 | int64_t pos1, pos2; |
2a10020b | 823 | |
de6d9b64 FB |
824 | pos1 = url_ftell(pb); |
825 | ||
247eadca | 826 | st = av_new_stream(s, 0); |
de6d9b64 FB |
827 | if (!st) |
828 | goto fail; | |
de6d9b64 FB |
829 | asf_st = av_mallocz(sizeof(ASFStream)); |
830 | if (!asf_st) | |
831 | goto fail; | |
832 | st->priv_data = asf_st; | |
247eadca FB |
833 | st->start_time = asf->hdr.preroll / (10000000 / AV_TIME_BASE); |
834 | st->duration = (asf->hdr.send_time - asf->hdr.preroll) / | |
835 | (10000000 / AV_TIME_BASE); | |
de6d9b64 FB |
836 | get_guid(pb, &g); |
837 | if (!memcmp(&g, &audio_stream, sizeof(GUID))) { | |
838 | type = CODEC_TYPE_AUDIO; | |
839 | } else if (!memcmp(&g, &video_stream, sizeof(GUID))) { | |
840 | type = CODEC_TYPE_VIDEO; | |
841 | } else { | |
842 | goto fail; | |
843 | } | |
844 | get_guid(pb, &g); | |
845 | total_size = get_le64(pb); | |
2e7973bb | 846 | type_specific_size = get_le32(pb); |
de6d9b64 | 847 | get_le32(pb); |
2a10020b ZK |
848 | st->id = get_le16(pb) & 0x7f; /* stream id */ |
849 | // mapping of asf ID to AV stream ID; | |
247eadca | 850 | asf->asfid2avid[st->id] = s->nb_streams - 1; |
2a10020b | 851 | |
de6d9b64 | 852 | get_le32(pb); |
2141dc37 | 853 | st->codec.codec_type = type; |
41568e4a | 854 | st->codec.frame_rate = 15 * s->pts_den / s->pts_num; // 15 fps default |
de6d9b64 | 855 | if (type == CODEC_TYPE_AUDIO) { |
2e7973bb | 856 | get_wav_header(pb, &st->codec, type_specific_size); |
2a10020b ZK |
857 | /* We have to init the frame size at some point .... */ |
858 | pos2 = url_ftell(pb); | |
2141dc37 | 859 | if (gsize > (pos2 + 8 - pos1 + 24)) { |
2a10020b ZK |
860 | asf_st->ds_span = get_byte(pb); |
861 | asf_st->ds_packet_size = get_le16(pb); | |
862 | asf_st->ds_chunk_size = get_le16(pb); | |
863 | asf_st->ds_data_size = get_le16(pb); | |
864 | asf_st->ds_silence_data = get_byte(pb); | |
865 | } | |
866 | //printf("Descrambling: ps:%d cs:%d ds:%d s:%d sd:%d\n", | |
867 | // asf_st->ds_packet_size, asf_st->ds_chunk_size, | |
868 | // asf_st->ds_data_size, asf_st->ds_span, asf_st->ds_silence_data); | |
2141dc37 | 869 | if (asf_st->ds_span > 1) { |
2a10020b ZK |
870 | if (!asf_st->ds_chunk_size |
871 | || (asf_st->ds_packet_size/asf_st->ds_chunk_size <= 1)) | |
872 | asf_st->ds_span = 0; // disable descrambling | |
873 | } | |
f80c1ac0 | 874 | switch (st->codec.codec_id) { |
80783dc2 | 875 | case CODEC_ID_MP3: |
f80c1ac0 PG |
876 | st->codec.frame_size = MPA_FRAME_SIZE; |
877 | break; | |
878 | case CODEC_ID_PCM_S16LE: | |
879 | case CODEC_ID_PCM_S16BE: | |
880 | case CODEC_ID_PCM_U16LE: | |
881 | case CODEC_ID_PCM_U16BE: | |
882 | case CODEC_ID_PCM_S8: | |
883 | case CODEC_ID_PCM_U8: | |
884 | case CODEC_ID_PCM_ALAW: | |
885 | case CODEC_ID_PCM_MULAW: | |
886 | st->codec.frame_size = 1; | |
887 | break; | |
888 | default: | |
889 | /* This is probably wrong, but it prevents a crash later */ | |
890 | st->codec.frame_size = 1; | |
891 | break; | |
892 | } | |
de6d9b64 | 893 | } else { |
2141dc37 | 894 | get_le32(pb); |
de6d9b64 FB |
895 | get_le32(pb); |
896 | get_byte(pb); | |
897 | size = get_le16(pb); /* size */ | |
898 | get_le32(pb); /* size */ | |
899 | st->codec.width = get_le32(pb); | |
2a10020b ZK |
900 | st->codec.height = get_le32(pb); |
901 | /* not available for asf */ | |
de6d9b64 | 902 | get_le16(pb); /* panes */ |
693feac5 | 903 | st->codec.bits_per_sample = get_le16(pb); /* depth */ |
de6d9b64 | 904 | tag1 = get_le32(pb); |
2141dc37 ZK |
905 | url_fskip(pb, 20); |
906 | if (size > 40) { | |
e40fed0b ZK |
907 | st->codec.extradata_size = size - 40; |
908 | st->codec.extradata = av_mallocz(st->codec.extradata_size); | |
909 | get_buffer(pb, st->codec.extradata, st->codec.extradata_size); | |
2141dc37 | 910 | } |
5e29abf8 RT |
911 | |
912 | /* Extract palette from extradata if bpp <= 8 */ | |
913 | /* This code assumes that extradata contains only palette */ | |
914 | /* This is true for all paletted codecs implemented in ffmpeg */ | |
915 | if (st->codec.extradata_size && (st->codec.bits_per_sample <= 8)) { | |
916 | st->codec.palctrl = av_mallocz(sizeof(AVPaletteControl)); | |
917 | #ifdef WORDS_BIGENDIAN | |
19d053c5 RS |
918 | for (i = 0; i < FFMIN(st->codec.extradata_size, AVPALETTE_SIZE)/4; i++) |
919 | st->codec.palctrl->palette[i] = bswap_32(((uint32_t*)st->codec.extradata)[i]); | |
5e29abf8 RT |
920 | #else |
921 | memcpy(st->codec.palctrl->palette, st->codec.extradata, | |
922 | FFMIN(st->codec.extradata_size, AVPALETTE_SIZE)); | |
923 | #endif | |
924 | st->codec.palctrl->palette_changed = 1; | |
925 | } | |
926 | ||
7004ffb3 | 927 | st->codec.codec_tag = tag1; |
17a241dd | 928 | st->codec.codec_id = codec_get_id(codec_bmp_tags, tag1); |
de6d9b64 FB |
929 | } |
930 | pos2 = url_ftell(pb); | |
931 | url_fskip(pb, gsize - (pos2 - pos1 + 24)); | |
932 | } else if (!memcmp(&g, &data_header, sizeof(GUID))) { | |
933 | break; | |
934 | } else if (!memcmp(&g, &comment_header, sizeof(GUID))) { | |
935 | int len1, len2, len3, len4, len5; | |
936 | ||
937 | len1 = get_le16(pb); | |
938 | len2 = get_le16(pb); | |
939 | len3 = get_le16(pb); | |
940 | len4 = get_le16(pb); | |
941 | len5 = get_le16(pb); | |
942 | get_str16_nolen(pb, len1, s->title, sizeof(s->title)); | |
943 | get_str16_nolen(pb, len2, s->author, sizeof(s->author)); | |
944 | get_str16_nolen(pb, len3, s->copyright, sizeof(s->copyright)); | |
945 | get_str16_nolen(pb, len4, s->comment, sizeof(s->comment)); | |
2141dc37 | 946 | url_fskip(pb, len5); |
de6d9b64 FB |
947 | #if 0 |
948 | } else if (!memcmp(&g, &head1_guid, sizeof(GUID))) { | |
949 | int v1, v2; | |
950 | get_guid(pb, &g); | |
951 | v1 = get_le32(pb); | |
952 | v2 = get_le16(pb); | |
953 | } else if (!memcmp(&g, &codec_comment_header, sizeof(GUID))) { | |
954 | int len, v1, n, num; | |
955 | char str[256], *q; | |
956 | char tag[16]; | |
957 | ||
958 | get_guid(pb, &g); | |
959 | print_guid(&g); | |
2a10020b | 960 | |
de6d9b64 FB |
961 | n = get_le32(pb); |
962 | for(i=0;i<n;i++) { | |
963 | num = get_le16(pb); /* stream number */ | |
964 | get_str16(pb, str, sizeof(str)); | |
965 | get_str16(pb, str, sizeof(str)); | |
966 | len = get_le16(pb); | |
967 | q = tag; | |
968 | while (len > 0) { | |
969 | v1 = get_byte(pb); | |
970 | if ((q - tag) < sizeof(tag) - 1) | |
971 | *q++ = v1; | |
972 | len--; | |
973 | } | |
974 | *q = '\0'; | |
975 | } | |
976 | #endif | |
977 | } else if (url_feof(pb)) { | |
978 | goto fail; | |
979 | } else { | |
980 | url_fseek(pb, gsize - 24, SEEK_CUR); | |
981 | } | |
982 | } | |
983 | get_guid(pb, &g); | |
984 | get_le64(pb); | |
985 | get_byte(pb); | |
986 | get_byte(pb); | |
2141dc37 ZK |
987 | if (url_feof(pb)) |
988 | goto fail; | |
989 | asf->data_offset = url_ftell(pb); | |
de6d9b64 FB |
990 | asf->packet_size_left = 0; |
991 | ||
992 | return 0; | |
993 | ||
994 | fail: | |
4c3dff6d | 995 | for(i=0;i<s->nb_streams;i++) { |
de6d9b64 | 996 | AVStream *st = s->streams[i]; |
4c3dff6d | 997 | if (st) { |
2141dc37 | 998 | av_free(st->priv_data); |
4c3dff6d ZK |
999 | av_free(st->codec.extradata); |
1000 | } | |
1ea4f593 | 1001 | av_free(st); |
de6d9b64 | 1002 | } |
de6d9b64 FB |
1003 | return -1; |
1004 | } | |
1005 | ||
2a10020b ZK |
1006 | #define DO_2BITS(bits, var, defval) \ |
1007 | switch (bits & 3) \ | |
1008 | { \ | |
1009 | case 3: var = get_le32(pb); rsize += 4; break; \ | |
1010 | case 2: var = get_le16(pb); rsize += 2; break; \ | |
1011 | case 1: var = get_byte(pb); rsize++; break; \ | |
1012 | default: var = defval; break; \ | |
1013 | } | |
1014 | ||
de6d9b64 FB |
1015 | static int asf_get_packet(AVFormatContext *s) |
1016 | { | |
1017 | ASFContext *asf = s->priv_data; | |
1018 | ByteIOContext *pb = &s->pb; | |
2a10020b ZK |
1019 | uint32_t packet_length, padsize; |
1020 | int rsize = 11; | |
1021 | int c = get_byte(pb); | |
1359c2d4 | 1022 | if (c != 0x82) { |
5acdd6e6 | 1023 | if (!url_feof(pb)) |
4cfbf61b | 1024 | printf("ff asf bad header %x at:%lld\n", c, url_ftell(pb)); |
2a10020b ZK |
1025 | return -EIO; |
1026 | } | |
1027 | if ((c & 0x0f) == 2) { // always true for now | |
1359c2d4 | 1028 | if (get_le16(pb) != 0) { |
5acdd6e6 ZK |
1029 | if (!url_feof(pb)) |
1030 | printf("ff asf bad non zero\n"); | |
2a10020b ZK |
1031 | return -EIO; |
1032 | } | |
de6d9b64 | 1033 | } |
2a10020b ZK |
1034 | |
1035 | asf->packet_flags = get_byte(pb); | |
1036 | asf->packet_property = get_byte(pb); | |
1037 | ||
1038 | DO_2BITS(asf->packet_flags >> 5, packet_length, asf->packet_size); | |
1039 | DO_2BITS(asf->packet_flags >> 1, padsize, 0); // sequence ignored | |
1040 | DO_2BITS(asf->packet_flags >> 3, padsize, 0); // padding length | |
1041 | ||
1042 | asf->packet_timestamp = get_le32(pb); | |
de6d9b64 | 1043 | get_le16(pb); /* duration */ |
2141dc37 | 1044 | // rsize has at least 11 bytes which have to be present |
2a10020b ZK |
1045 | |
1046 | if (asf->packet_flags & 0x01) { | |
1047 | asf->packet_segsizetype = get_byte(pb); rsize++; | |
1048 | asf->packet_segments = asf->packet_segsizetype & 0x3f; | |
1049 | } else { | |
1050 | asf->packet_segments = 1; | |
1051 | asf->packet_segsizetype = 0x80; | |
1052 | } | |
1053 | asf->packet_size_left = packet_length - padsize - rsize; | |
acbe6cfa ZK |
1054 | if (packet_length < asf->hdr.min_pktsize) |
1055 | padsize += asf->hdr.min_pktsize - packet_length; | |
2a10020b | 1056 | asf->packet_padsize = padsize; |
de6d9b64 | 1057 | #ifdef DEBUG |
2a10020b | 1058 | printf("packet: size=%d padsize=%d left=%d\n", asf->packet_size, asf->packet_padsize, asf->packet_size_left); |
de6d9b64 | 1059 | #endif |
de6d9b64 FB |
1060 | return 0; |
1061 | } | |
1062 | ||
1063 | static int asf_read_packet(AVFormatContext *s, AVPacket *pkt) | |
1064 | { | |
1065 | ASFContext *asf = s->priv_data; | |
2a10020b | 1066 | ASFStream *asf_st = 0; |
de6d9b64 | 1067 | ByteIOContext *pb = &s->pb; |
1359c2d4 | 1068 | //static int pc = 0; |
2a10020b ZK |
1069 | for (;;) { |
1070 | int rsize = 0; | |
2141dc37 | 1071 | if (asf->packet_size_left < FRAME_HEADER_SIZE |
1359c2d4 | 1072 | || asf->packet_segments < 1) { |
2a10020b | 1073 | //asf->packet_size_left <= asf->packet_padsize) { |
1359c2d4 ZK |
1074 | int ret = asf->packet_size_left + asf->packet_padsize; |
1075 | //printf("PacketLeftSize:%d Pad:%d Pos:%Ld\n", asf->packet_size_left, asf->packet_padsize, url_ftell(pb)); | |
1076 | /* fail safe */ | |
1077 | url_fskip(pb, ret); | |
2a10020b ZK |
1078 | ret = asf_get_packet(s); |
1079 | //printf("READ ASF PACKET %d r:%d c:%d\n", ret, asf->packet_size_left, pc++); | |
5acdd6e6 | 1080 | if (ret < 0 || url_feof(pb)) |
2a10020b | 1081 | return -EIO; |
2141dc37 | 1082 | asf->packet_time_start = 0; |
2a10020b ZK |
1083 | continue; |
1084 | } | |
2141dc37 | 1085 | if (asf->packet_time_start == 0) { |
2a10020b | 1086 | /* read frame header */ |
1359c2d4 ZK |
1087 | int num = get_byte(pb); |
1088 | asf->packet_segments--; | |
2a10020b ZK |
1089 | rsize++; |
1090 | asf->packet_key_frame = (num & 0x80) >> 7; | |
1091 | asf->stream_index = asf->asfid2avid[num & 0x7f]; | |
2a10020b ZK |
1092 | // sequence should be ignored! |
1093 | DO_2BITS(asf->packet_property >> 4, asf->packet_seq, 0); | |
1094 | DO_2BITS(asf->packet_property >> 2, asf->packet_frag_offset, 0); | |
1095 | DO_2BITS(asf->packet_property, asf->packet_replic_size, 0); | |
1096 | ||
1097 | if (asf->packet_replic_size > 1) { | |
1098 | // it should be always at least 8 bytes - FIXME validate | |
1099 | asf->packet_obj_size = get_le32(pb); | |
1100 | asf->packet_frag_timestamp = get_le32(pb); // timestamp | |
acbe6cfa ZK |
1101 | if (asf->packet_replic_size > 8) |
1102 | url_fskip(pb, asf->packet_replic_size - 8); | |
2a10020b ZK |
1103 | rsize += asf->packet_replic_size; // FIXME - check validity |
1104 | } else { | |
acbe6cfa | 1105 | // multipacket - frag_offset is begining timestamp |
2a10020b ZK |
1106 | asf->packet_time_start = asf->packet_frag_offset; |
1107 | asf->packet_frag_offset = 0; | |
1108 | asf->packet_frag_timestamp = asf->packet_timestamp; | |
2141dc37 | 1109 | |
2a10020b ZK |
1110 | if (asf->packet_replic_size == 1) { |
1111 | asf->packet_time_delta = get_byte(pb); | |
1112 | rsize++; | |
1113 | } | |
1114 | } | |
2a10020b ZK |
1115 | if (asf->packet_flags & 0x01) { |
1116 | DO_2BITS(asf->packet_segsizetype >> 6, asf->packet_frag_size, 0); // 0 is illegal | |
acbe6cfa | 1117 | #undef DO_2BITS |
2a10020b ZK |
1118 | //printf("Fragsize %d\n", asf->packet_frag_size); |
1119 | } else { | |
1120 | asf->packet_frag_size = asf->packet_size_left - rsize; | |
1121 | //printf("Using rest %d %d %d\n", asf->packet_frag_size, asf->packet_size_left, rsize); | |
1122 | } | |
1359c2d4 | 1123 | if (asf->packet_replic_size == 1) { |
2141dc37 | 1124 | asf->packet_multi_size = asf->packet_frag_size; |
1359c2d4 | 1125 | if (asf->packet_multi_size > asf->packet_size_left) { |
2141dc37 ZK |
1126 | asf->packet_segments = 0; |
1127 | continue; | |
1128 | } | |
1129 | } | |
2a10020b ZK |
1130 | asf->packet_size_left -= rsize; |
1131 | //printf("___objsize____ %d %d rs:%d\n", asf->packet_obj_size, asf->packet_frag_offset, rsize); | |
1359c2d4 ZK |
1132 | |
1133 | if (asf->stream_index < 0) { | |
1134 | asf->packet_time_start = 0; | |
1135 | /* unhandled packet (should not happen) */ | |
1136 | url_fskip(pb, asf->packet_frag_size); | |
1137 | asf->packet_size_left -= asf->packet_frag_size; | |
1138 | printf("ff asf skip %d %d\n", asf->packet_frag_size, num & 0x7f); | |
1139 | continue; | |
1140 | } | |
1141 | asf->asf_st = s->streams[asf->stream_index]->priv_data; | |
2a10020b ZK |
1142 | } |
1143 | asf_st = asf->asf_st; | |
1144 | ||
1145 | if ((asf->packet_frag_offset != asf_st->frag_offset | |
1146 | || (asf->packet_frag_offset | |
1147 | && asf->packet_seq != asf_st->seq)) // seq should be ignored | |
1148 | ) { | |
1149 | /* cannot continue current packet: free it */ | |
1150 | // FIXME better check if packet was already allocated | |
2141dc37 | 1151 | printf("ff asf parser skips: %d - %d o:%d - %d %d %d fl:%d\n", |
2a10020b | 1152 | asf_st->pkt.size, |
2141dc37 | 1153 | asf->packet_obj_size, |
2a10020b ZK |
1154 | asf->packet_frag_offset, asf_st->frag_offset, |
1155 | asf->packet_seq, asf_st->seq, asf->packet_frag_size); | |
2141dc37 | 1156 | if (asf_st->pkt.size) |
2a10020b ZK |
1157 | av_free_packet(&asf_st->pkt); |
1158 | asf_st->frag_offset = 0; | |
1159 | if (asf->packet_frag_offset != 0) { | |
2a10020b | 1160 | url_fskip(pb, asf->packet_frag_size); |
2141dc37 ZK |
1161 | printf("ff asf parser skiping %db\n", asf->packet_frag_size); |
1162 | asf->packet_size_left -= asf->packet_frag_size; | |
2a10020b ZK |
1163 | continue; |
1164 | } | |
1165 | } | |
2141dc37 ZK |
1166 | if (asf->packet_replic_size == 1) { |
1167 | // frag_offset is here used as the begining timestamp | |
2a10020b | 1168 | asf->packet_frag_timestamp = asf->packet_time_start; |
2141dc37 | 1169 | asf->packet_time_start += asf->packet_time_delta; |
2a10020b | 1170 | asf->packet_obj_size = asf->packet_frag_size = get_byte(pb); |
2141dc37 ZK |
1171 | asf->packet_size_left--; |
1172 | asf->packet_multi_size--; | |
1173 | if (asf->packet_multi_size < asf->packet_obj_size) | |
1174 | { | |
1175 | asf->packet_time_start = 0; | |
1176 | url_fskip(pb, asf->packet_multi_size); | |
1177 | asf->packet_size_left -= asf->packet_multi_size; | |
1178 | continue; | |
1179 | } | |
1180 | asf->packet_multi_size -= asf->packet_obj_size; | |
1181 | //printf("COMPRESS size %d %d %d ms:%d\n", asf->packet_obj_size, asf->packet_frag_timestamp, asf->packet_size_left, asf->packet_multi_size); | |
2a10020b | 1182 | } |
2a10020b ZK |
1183 | if (asf_st->frag_offset == 0) { |
1184 | /* new packet */ | |
1185 | av_new_packet(&asf_st->pkt, asf->packet_obj_size); | |
1186 | asf_st->seq = asf->packet_seq; | |
1187 | asf_st->pkt.pts = asf->packet_frag_timestamp - asf->hdr.preroll; | |
1188 | asf_st->pkt.stream_index = asf->stream_index; | |
1189 | if (asf->packet_key_frame) | |
1190 | asf_st->pkt.flags |= PKT_FLAG_KEY; | |
1191 | } | |
1192 | ||
1193 | /* read data */ | |
1194 | //printf("READ PACKET s:%d os:%d o:%d,%d l:%d DATA:%p\n", | |
1195 | // asf->packet_size, asf_st->pkt.size, asf->packet_frag_offset, | |
1196 | // asf_st->frag_offset, asf->packet_frag_size, asf_st->pkt.data); | |
2141dc37 ZK |
1197 | asf->packet_size_left -= asf->packet_frag_size; |
1198 | if (asf->packet_size_left < 0) | |
1199 | continue; | |
2a10020b ZK |
1200 | get_buffer(pb, asf_st->pkt.data + asf->packet_frag_offset, |
1201 | asf->packet_frag_size); | |
1202 | asf_st->frag_offset += asf->packet_frag_size; | |
2a10020b ZK |
1203 | /* test if whole packet is read */ |
1204 | if (asf_st->frag_offset == asf_st->pkt.size) { | |
1205 | /* return packet */ | |
1206 | if (asf_st->ds_span > 1) { | |
2141dc37 | 1207 | /* packet descrambling */ |
2a10020b ZK |
1208 | char* newdata = av_malloc(asf_st->pkt.size); |
1209 | if (newdata) { | |
1210 | int offset = 0; | |
1211 | while (offset < asf_st->pkt.size) { | |
1212 | int off = offset / asf_st->ds_chunk_size; | |
1213 | int row = off / asf_st->ds_span; | |
1214 | int col = off % asf_st->ds_span; | |
1215 | int idx = row + col * asf_st->ds_packet_size / asf_st->ds_chunk_size; | |
1216 | //printf("off:%d row:%d col:%d idx:%d\n", off, row, col, idx); | |
1217 | memcpy(newdata + offset, | |
1218 | asf_st->pkt.data + idx * asf_st->ds_chunk_size, | |
1219 | asf_st->ds_chunk_size); | |
1220 | offset += asf_st->ds_chunk_size; | |
1221 | } | |
1222 | av_free(asf_st->pkt.data); | |
1223 | asf_st->pkt.data = newdata; | |
1224 | } | |
1225 | } | |
2a10020b ZK |
1226 | asf_st->frag_offset = 0; |
1227 | memcpy(pkt, &asf_st->pkt, sizeof(AVPacket)); | |
1228 | //printf("packet %d %d\n", asf_st->pkt.size, asf->packet_frag_size); | |
1229 | asf_st->pkt.size = 0; | |
1230 | asf_st->pkt.data = 0; | |
2a10020b ZK |
1231 | break; // packet completed |
1232 | } | |
de6d9b64 | 1233 | } |
de6d9b64 FB |
1234 | return 0; |
1235 | } | |
1236 | ||
1237 | static int asf_read_close(AVFormatContext *s) | |
1238 | { | |
de6d9b64 FB |
1239 | int i; |
1240 | ||
1241 | for(i=0;i<s->nb_streams;i++) { | |
4c3dff6d ZK |
1242 | AVStream *st = s->streams[i]; |
1243 | av_free(st->priv_data); | |
1244 | av_free(st->codec.extradata); | |
5e29abf8 | 1245 | av_free(st->codec.palctrl); |
de6d9b64 | 1246 | } |
de6d9b64 FB |
1247 | return 0; |
1248 | } | |
1249 | ||
2a10020b ZK |
1250 | static int asf_read_seek(AVFormatContext *s, int64_t pts) |
1251 | { | |
4cfbf61b | 1252 | printf("SEEK TO %lld", pts); |
2a10020b ZK |
1253 | return -1; |
1254 | } | |
1255 | ||
0570bf06 | 1256 | static AVInputFormat asf_iformat = { |
c9a65ca8 FB |
1257 | "asf", |
1258 | "asf format", | |
1259 | sizeof(ASFContext), | |
1260 | asf_probe, | |
1261 | asf_read_header, | |
1262 | asf_read_packet, | |
1263 | asf_read_close, | |
2a10020b | 1264 | asf_read_seek, |
c9a65ca8 FB |
1265 | }; |
1266 | ||
764ef400 | 1267 | #ifdef CONFIG_ENCODERS |
0570bf06 | 1268 | static AVOutputFormat asf_oformat = { |
de6d9b64 FB |
1269 | "asf", |
1270 | "asf format", | |
c6c11cb6 | 1271 | "video/x-ms-asf", |
a56c66a7 | 1272 | "asf,wmv", |
c9a65ca8 | 1273 | sizeof(ASFContext), |
4606ac8d | 1274 | #ifdef CONFIG_MP3LAME |
80783dc2 | 1275 | CODEC_ID_MP3, |
4606ac8d | 1276 | #else |
de6d9b64 | 1277 | CODEC_ID_MP2, |
4606ac8d | 1278 | #endif |
d7425f59 | 1279 | CODEC_ID_MSMPEG4V3, |
de6d9b64 FB |
1280 | asf_write_header, |
1281 | asf_write_packet, | |
1282 | asf_write_trailer, | |
de6d9b64 | 1283 | }; |
c9a65ca8 | 1284 | |
0570bf06 | 1285 | static AVOutputFormat asf_stream_oformat = { |
8b3c13f9 PG |
1286 | "asf_stream", |
1287 | "asf format", | |
c6c11cb6 | 1288 | "video/x-ms-asf", |
8b3c13f9 PG |
1289 | "asf,wmv", |
1290 | sizeof(ASFContext), | |
1291 | #ifdef CONFIG_MP3LAME | |
80783dc2 | 1292 | CODEC_ID_MP3, |
8b3c13f9 PG |
1293 | #else |
1294 | CODEC_ID_MP2, | |
1295 | #endif | |
d7425f59 | 1296 | CODEC_ID_MSMPEG4V3, |
8b3c13f9 PG |
1297 | asf_write_stream_header, |
1298 | asf_write_packet, | |
1299 | asf_write_trailer, | |
1300 | }; | |
764ef400 | 1301 | #endif //CONFIG_ENCODERS |
8b3c13f9 | 1302 | |
c9a65ca8 FB |
1303 | int asf_init(void) |
1304 | { | |
1305 | av_register_input_format(&asf_iformat); | |
764ef400 | 1306 | #ifdef CONFIG_ENCODERS |
c9a65ca8 | 1307 | av_register_output_format(&asf_oformat); |
8b3c13f9 | 1308 | av_register_output_format(&asf_stream_oformat); |
764ef400 | 1309 | #endif //CONFIG_ENCODERS |
c9a65ca8 FB |
1310 | return 0; |
1311 | } |