Mercurial > libavformat.hg
annotate asf.c @ 341:ad2a57c5467a libavformat
ASF extended header parsing patch by ("Kevin Kuphal" <kevin dot kuphal at sitecity dot net>)
author | michael |
---|---|
date | Tue, 06 Jan 2004 15:31:36 +0000 |
parents | 8a04d2e1be2f |
children | 2b485a3c5cfb |
rev | line source |
---|---|
0 | 1 /* |
2 * ASF compatible encoder and decoder. | |
3 * Copyright (c) 2000, 2001 Fabrice Bellard. | |
4 * | |
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. | |
9 * | |
10 * This library 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 GNU | |
13 * Lesser General Public License for more details. | |
14 * | |
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 | |
18 */ | |
19 #include "avformat.h" | |
20 #include "avi.h" | |
21 #include "mpegaudio.h" | |
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; | |
33 int timestamp; | |
65 | 34 int64_t duration; |
0 | 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 | |
42 } ASFStream; | |
43 | |
44 typedef struct { | |
65 | 45 uint32_t v1; |
46 uint16_t v2; | |
47 uint16_t v3; | |
48 uint8_t v4[8]; | |
0 | 49 } GUID; |
50 | |
188
6c9d6422a2f6
update duration and start_time - add av_new_stream() usage
bellard
parents:
136
diff
changeset
|
51 typedef struct { |
0 | 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 { | |
80 int seqno; | |
81 int packet_size; | |
82 int is_streamed; | |
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 */ | |
85 /* non streamed additonnal info */ | |
65 | 86 int64_t nb_packets; |
87 int64_t duration; /* in 100ns units */ | |
0 | 88 /* packet filling */ |
89 int packet_size_left; | |
90 int packet_timestamp_start; | |
91 int packet_timestamp_end; | |
92 int packet_nb_frames; | |
65 | 93 uint8_t packet_buf[PACKET_SIZE]; |
0 | 94 ByteIOContext pb; |
95 /* only for reading */ | |
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; | |
108 int packet_padsize; | |
109 int packet_frag_offset; | |
110 int packet_frag_size; | |
111 int packet_frag_timestamp; | |
112 int packet_multi_size; | |
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 */ | |
119 } ASFContext; | |
120 | |
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 = { | |
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 }, | |
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 }; | |
178 | |
341
ad2a57c5467a
ASF extended header parsing patch by ("Kevin Kuphal" <kevin dot kuphal at sitecity dot net>)
michael
parents:
312
diff
changeset
|
179 static const GUID extended_content_header = { |
ad2a57c5467a
ASF extended header parsing patch by ("Kevin Kuphal" <kevin dot kuphal at sitecity dot net>)
michael
parents:
312
diff
changeset
|
180 0xD2D0A440, 0xE307, 0x11D2, { 0x97, 0xF0, 0x00, 0xA0, 0xC9, 0x5E, 0xA8, 0x50 }, |
ad2a57c5467a
ASF extended header parsing patch by ("Kevin Kuphal" <kevin dot kuphal at sitecity dot net>)
michael
parents:
312
diff
changeset
|
181 }; |
ad2a57c5467a
ASF extended header parsing patch by ("Kevin Kuphal" <kevin dot kuphal at sitecity dot net>)
michael
parents:
312
diff
changeset
|
182 |
0 | 183 /* I am not a number !!! This GUID is the one found on the PC used to |
184 generate the stream */ | |
185 static const GUID my_guid = { | |
186 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0, 0 }, | |
187 }; | |
188 | |
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
241
diff
changeset
|
189 #ifdef CONFIG_ENCODERS |
0 | 190 static void put_guid(ByteIOContext *s, const GUID *g) |
191 { | |
192 int i; | |
193 | |
194 put_le32(s, g->v1); | |
195 put_le16(s, g->v2); | |
196 put_le16(s, g->v3); | |
197 for(i=0;i<8;i++) | |
198 put_byte(s, g->v4[i]); | |
199 } | |
200 | |
201 static void put_str16(ByteIOContext *s, const char *tag) | |
202 { | |
203 int c; | |
204 | |
205 put_le16(s,strlen(tag) + 1); | |
206 for(;;) { | |
65 | 207 c = (uint8_t)*tag++; |
0 | 208 put_le16(s, c); |
209 if (c == '\0') | |
210 break; | |
211 } | |
212 } | |
213 | |
214 static void put_str16_nolen(ByteIOContext *s, const char *tag) | |
215 { | |
216 int c; | |
217 | |
218 for(;;) { | |
65 | 219 c = (uint8_t)*tag++; |
0 | 220 put_le16(s, c); |
221 if (c == '\0') | |
222 break; | |
223 } | |
224 } | |
225 | |
65 | 226 static int64_t put_header(ByteIOContext *pb, const GUID *g) |
0 | 227 { |
65 | 228 int64_t pos; |
0 | 229 |
230 pos = url_ftell(pb); | |
231 put_guid(pb, g); | |
232 put_le64(pb, 24); | |
233 return pos; | |
234 } | |
235 | |
236 /* update header size */ | |
65 | 237 static void end_header(ByteIOContext *pb, int64_t pos) |
0 | 238 { |
65 | 239 int64_t pos1; |
0 | 240 |
241 pos1 = url_ftell(pb); | |
242 url_fseek(pb, pos + 16, SEEK_SET); | |
243 put_le64(pb, pos1 - pos); | |
244 url_fseek(pb, pos1, SEEK_SET); | |
245 } | |
246 | |
247 /* write an asf chunk (only used in streaming case) */ | |
248 static void put_chunk(AVFormatContext *s, int type, int payload_length, int flags) | |
249 { | |
250 ASFContext *asf = s->priv_data; | |
251 ByteIOContext *pb = &s->pb; | |
252 int length; | |
253 | |
254 length = payload_length + 8; | |
255 put_le16(pb, type); | |
256 put_le16(pb, length); | |
257 put_le32(pb, asf->seqno); | |
258 put_le16(pb, flags); /* unknown bytes */ | |
259 put_le16(pb, length); | |
260 asf->seqno++; | |
261 } | |
262 | |
263 /* convert from unix to windows time */ | |
65 | 264 static int64_t unix_to_file_time(int ti) |
0 | 265 { |
65 | 266 int64_t t; |
0 | 267 |
65 | 268 t = ti * int64_t_C(10000000); |
269 t += int64_t_C(116444736000000000); | |
0 | 270 return t; |
271 } | |
272 | |
273 /* write the header (used two times if non streamed) */ | |
65 | 274 static int asf_write_header1(AVFormatContext *s, int64_t file_size, int64_t data_chunk_size) |
0 | 275 { |
276 ASFContext *asf = s->priv_data; | |
277 ByteIOContext *pb = &s->pb; | |
278 int header_size, n, extra_size, extra_size2, wav_extra_size, file_time; | |
279 int has_title; | |
280 AVCodecContext *enc; | |
65 | 281 int64_t header_offset, cur_pos, hpos; |
0 | 282 int bit_rate; |
283 | |
284 has_title = (s->title[0] || s->author[0] || s->copyright[0] || s->comment[0]); | |
285 | |
286 bit_rate = 0; | |
287 for(n=0;n<s->nb_streams;n++) { | |
288 enc = &s->streams[n]->codec; | |
289 | |
290 bit_rate += enc->bit_rate; | |
291 } | |
292 | |
293 if (asf->is_streamed) { | |
294 put_chunk(s, 0x4824, 0, 0xc00); /* start of stream (length will be patched later) */ | |
295 } | |
296 | |
297 put_guid(pb, &asf_header); | |
298 put_le64(pb, -1); /* header length, will be patched after */ | |
299 put_le32(pb, 3 + has_title + s->nb_streams); /* number of chunks in header */ | |
300 put_byte(pb, 1); /* ??? */ | |
301 put_byte(pb, 2); /* ??? */ | |
302 | |
303 /* file header */ | |
304 header_offset = url_ftell(pb); | |
305 hpos = put_header(pb, &file_header); | |
306 put_guid(pb, &my_guid); | |
307 put_le64(pb, file_size); | |
308 file_time = 0; | |
309 put_le64(pb, unix_to_file_time(file_time)); | |
310 put_le64(pb, asf->nb_packets); /* number of packets */ | |
311 put_le64(pb, asf->duration); /* end time stamp (in 100ns units) */ | |
312 put_le64(pb, asf->duration); /* duration (in 100ns units) */ | |
313 put_le32(pb, 0); /* start time stamp */ | |
314 put_le32(pb, 0); /* ??? */ | |
315 put_le32(pb, asf->is_streamed ? 1 : 0); /* ??? */ | |
316 put_le32(pb, asf->packet_size); /* packet size */ | |
317 put_le32(pb, asf->packet_size); /* packet size */ | |
318 put_le32(pb, bit_rate); /* Nominal data rate in bps */ | |
319 end_header(pb, hpos); | |
320 | |
321 /* unknown headers */ | |
322 hpos = put_header(pb, &head1_guid); | |
323 put_guid(pb, &head2_guid); | |
324 put_le32(pb, 6); | |
325 put_le16(pb, 0); | |
326 end_header(pb, hpos); | |
327 | |
328 /* title and other infos */ | |
329 if (has_title) { | |
330 hpos = put_header(pb, &comment_header); | |
331 put_le16(pb, 2 * (strlen(s->title) + 1)); | |
332 put_le16(pb, 2 * (strlen(s->author) + 1)); | |
333 put_le16(pb, 2 * (strlen(s->copyright) + 1)); | |
334 put_le16(pb, 2 * (strlen(s->comment) + 1)); | |
335 put_le16(pb, 0); | |
336 put_str16_nolen(pb, s->title); | |
337 put_str16_nolen(pb, s->author); | |
338 put_str16_nolen(pb, s->copyright); | |
339 put_str16_nolen(pb, s->comment); | |
340 end_header(pb, hpos); | |
341 } | |
342 | |
343 /* stream headers */ | |
344 for(n=0;n<s->nb_streams;n++) { | |
65 | 345 int64_t es_pos; |
0 | 346 // ASFStream *stream = &asf->streams[n]; |
347 | |
348 enc = &s->streams[n]->codec; | |
349 asf->streams[n].num = n + 1; | |
350 asf->streams[n].seq = 0; | |
351 | |
352 switch(enc->codec_type) { | |
353 case CODEC_TYPE_AUDIO: | |
354 wav_extra_size = 0; | |
355 extra_size = 18 + wav_extra_size; | |
356 extra_size2 = 0; | |
357 break; | |
358 default: | |
359 case CODEC_TYPE_VIDEO: | |
360 wav_extra_size = 0; | |
361 extra_size = 0x33; | |
362 extra_size2 = 0; | |
363 break; | |
364 } | |
365 | |
366 hpos = put_header(pb, &stream_header); | |
367 if (enc->codec_type == CODEC_TYPE_AUDIO) { | |
368 put_guid(pb, &audio_stream); | |
369 put_guid(pb, &audio_conceal_none); | |
370 } else { | |
371 put_guid(pb, &video_stream); | |
372 put_guid(pb, &video_conceal_none); | |
373 } | |
374 put_le64(pb, 0); /* ??? */ | |
375 es_pos = url_ftell(pb); | |
376 put_le32(pb, extra_size); /* wav header len */ | |
377 put_le32(pb, extra_size2); /* additional data len */ | |
378 put_le16(pb, n + 1); /* stream number */ | |
379 put_le32(pb, 0); /* ??? */ | |
380 | |
381 if (enc->codec_type == CODEC_TYPE_AUDIO) { | |
382 /* WAVEFORMATEX header */ | |
383 int wavsize = put_wav_header(pb, enc); | |
384 | |
385 if (wavsize < 0) | |
386 return -1; | |
387 if (wavsize != extra_size) { | |
388 cur_pos = url_ftell(pb); | |
389 url_fseek(pb, es_pos, SEEK_SET); | |
390 put_le32(pb, wavsize); /* wav header len */ | |
391 url_fseek(pb, cur_pos, SEEK_SET); | |
392 } | |
393 } else { | |
394 put_le32(pb, enc->width); | |
395 put_le32(pb, enc->height); | |
396 put_byte(pb, 2); /* ??? */ | |
397 put_le16(pb, 40); /* size */ | |
398 | |
399 /* BITMAPINFOHEADER header */ | |
400 put_bmp_header(pb, enc, codec_bmp_tags, 1); | |
401 } | |
402 end_header(pb, hpos); | |
403 } | |
404 | |
405 /* media comments */ | |
406 | |
407 hpos = put_header(pb, &codec_comment_header); | |
408 put_guid(pb, &codec_comment1_header); | |
409 put_le32(pb, s->nb_streams); | |
410 for(n=0;n<s->nb_streams;n++) { | |
411 AVCodec *p; | |
412 | |
413 enc = &s->streams[n]->codec; | |
414 p = avcodec_find_encoder(enc->codec_id); | |
415 | |
416 put_le16(pb, asf->streams[n].num); | |
417 put_str16(pb, p ? p->name : enc->codec_name); | |
418 put_le16(pb, 0); /* no parameters */ | |
196 | 419 |
420 | |
0 | 421 /* id */ |
422 if (enc->codec_type == CODEC_TYPE_AUDIO) { | |
423 put_le16(pb, 2); | |
196 | 424 if(!enc->codec_tag) |
425 enc->codec_tag = codec_get_tag(codec_wav_tags, enc->codec_id); | |
426 if(!enc->codec_tag) | |
427 return -1; | |
428 put_le16(pb, enc->codec_tag); | |
0 | 429 } else { |
430 put_le16(pb, 4); | |
196 | 431 if(!enc->codec_tag) |
432 enc->codec_tag = codec_get_tag(codec_bmp_tags, enc->codec_id); | |
433 if(!enc->codec_tag) | |
434 return -1; | |
435 put_le32(pb, enc->codec_tag); | |
0 | 436 } |
437 } | |
438 end_header(pb, hpos); | |
439 | |
440 /* patch the header size fields */ | |
441 | |
442 cur_pos = url_ftell(pb); | |
443 header_size = cur_pos - header_offset; | |
444 if (asf->is_streamed) { | |
445 header_size += 8 + 30 + 50; | |
446 | |
447 url_fseek(pb, header_offset - 10 - 30, SEEK_SET); | |
448 put_le16(pb, header_size); | |
449 url_fseek(pb, header_offset - 2 - 30, SEEK_SET); | |
450 put_le16(pb, header_size); | |
451 | |
452 header_size -= 8 + 30 + 50; | |
453 } | |
454 header_size += 24 + 6; | |
455 url_fseek(pb, header_offset - 14, SEEK_SET); | |
456 put_le64(pb, header_size); | |
457 url_fseek(pb, cur_pos, SEEK_SET); | |
458 | |
459 /* movie chunk, followed by packets of packet_size */ | |
460 asf->data_offset = cur_pos; | |
461 put_guid(pb, &data_header); | |
462 put_le64(pb, data_chunk_size); | |
463 put_guid(pb, &my_guid); | |
464 put_le64(pb, asf->nb_packets); /* nb packets */ | |
465 put_byte(pb, 1); /* ??? */ | |
466 put_byte(pb, 1); /* ??? */ | |
467 return 0; | |
468 } | |
469 | |
470 static int asf_write_header(AVFormatContext *s) | |
471 { | |
472 ASFContext *asf = s->priv_data; | |
473 | |
474 av_set_pts_info(s, 32, 1, 1000); /* 32 bit pts in ms */ | |
475 | |
476 asf->packet_size = PACKET_SIZE; | |
477 asf->nb_packets = 0; | |
478 | |
479 if (asf_write_header1(s, 0, 50) < 0) { | |
480 //av_free(asf); | |
481 return -1; | |
482 } | |
483 | |
484 put_flush_packet(&s->pb); | |
485 | |
486 asf->packet_nb_frames = 0; | |
487 asf->packet_timestamp_start = -1; | |
488 asf->packet_timestamp_end = -1; | |
489 asf->packet_size_left = asf->packet_size - PACKET_HEADER_SIZE; | |
490 init_put_byte(&asf->pb, asf->packet_buf, asf->packet_size, 1, | |
491 NULL, NULL, NULL, NULL); | |
492 | |
493 return 0; | |
494 } | |
495 | |
496 static int asf_write_stream_header(AVFormatContext *s) | |
497 { | |
498 ASFContext *asf = s->priv_data; | |
499 | |
500 asf->is_streamed = 1; | |
501 | |
502 return asf_write_header(s); | |
503 } | |
504 | |
505 /* write a fixed size packet */ | |
506 static int put_packet(AVFormatContext *s, | |
507 unsigned int timestamp, unsigned int duration, | |
508 int nb_frames, int padsize) | |
509 { | |
510 ASFContext *asf = s->priv_data; | |
511 ByteIOContext *pb = &s->pb; | |
512 int flags; | |
513 | |
514 if (asf->is_streamed) { | |
515 put_chunk(s, 0x4424, asf->packet_size, 0); | |
516 } | |
517 | |
518 put_byte(pb, 0x82); | |
519 put_le16(pb, 0); | |
520 | |
521 flags = 0x01; /* nb segments present */ | |
522 if (padsize > 0) { | |
523 if (padsize < 256) | |
524 flags |= 0x08; | |
525 else | |
526 flags |= 0x10; | |
527 } | |
528 put_byte(pb, flags); /* flags */ | |
529 put_byte(pb, 0x5d); | |
530 if (flags & 0x10) | |
531 put_le16(pb, padsize - 2); | |
532 if (flags & 0x08) | |
533 put_byte(pb, padsize - 1); | |
534 put_le32(pb, timestamp); | |
535 put_le16(pb, duration); | |
536 put_byte(pb, nb_frames | 0x80); | |
537 | |
538 return PACKET_HEADER_SIZE + ((flags & 0x18) >> 3); | |
539 } | |
540 | |
541 static void flush_packet(AVFormatContext *s) | |
542 { | |
543 ASFContext *asf = s->priv_data; | |
544 int hdr_size, ptr; | |
545 | |
546 hdr_size = put_packet(s, asf->packet_timestamp_start, | |
547 asf->packet_timestamp_end - asf->packet_timestamp_start, | |
548 asf->packet_nb_frames, asf->packet_size_left); | |
549 | |
550 /* Clear out the padding bytes */ | |
551 ptr = asf->packet_size - hdr_size - asf->packet_size_left; | |
552 memset(asf->packet_buf + ptr, 0, asf->packet_size_left); | |
553 | |
554 put_buffer(&s->pb, asf->packet_buf, asf->packet_size - hdr_size); | |
555 | |
556 put_flush_packet(&s->pb); | |
557 asf->nb_packets++; | |
558 asf->packet_nb_frames = 0; | |
559 asf->packet_timestamp_start = -1; | |
560 asf->packet_timestamp_end = -1; | |
561 asf->packet_size_left = asf->packet_size - PACKET_HEADER_SIZE; | |
562 init_put_byte(&asf->pb, asf->packet_buf, asf->packet_size, 1, | |
563 NULL, NULL, NULL, NULL); | |
564 } | |
565 | |
566 static void put_frame_header(AVFormatContext *s, ASFStream *stream, int timestamp, | |
567 int payload_size, int frag_offset, int frag_len) | |
568 { | |
569 ASFContext *asf = s->priv_data; | |
570 ByteIOContext *pb = &asf->pb; | |
571 int val; | |
572 | |
573 val = stream->num; | |
7 | 574 if (s->streams[val - 1]->codec.coded_frame->key_frame /* && frag_offset == 0 */) |
0 | 575 val |= 0x80; |
576 put_byte(pb, val); | |
577 put_byte(pb, stream->seq); | |
578 put_le32(pb, frag_offset); /* fragment offset */ | |
579 put_byte(pb, 0x08); /* flags */ | |
580 put_le32(pb, payload_size); | |
581 put_le32(pb, timestamp); | |
582 put_le16(pb, frag_len); | |
583 } | |
584 | |
585 | |
586 /* Output a frame. We suppose that payload_size <= PACKET_SIZE. | |
587 | |
588 It is there that you understand that the ASF format is really | |
589 crap. They have misread the MPEG Systems spec ! | |
590 */ | |
591 static void put_frame(AVFormatContext *s, ASFStream *stream, int timestamp, | |
241 | 592 const uint8_t *buf, int payload_size) |
0 | 593 { |
594 ASFContext *asf = s->priv_data; | |
595 int frag_pos, frag_len, frag_len1; | |
596 | |
597 frag_pos = 0; | |
598 while (frag_pos < payload_size) { | |
599 frag_len = payload_size - frag_pos; | |
600 frag_len1 = asf->packet_size_left - FRAME_HEADER_SIZE; | |
601 if (frag_len1 > 0) { | |
602 if (frag_len > frag_len1) | |
603 frag_len = frag_len1; | |
604 put_frame_header(s, stream, timestamp+1, payload_size, frag_pos, frag_len); | |
605 put_buffer(&asf->pb, buf, frag_len); | |
606 asf->packet_size_left -= (frag_len + FRAME_HEADER_SIZE); | |
607 asf->packet_timestamp_end = timestamp; | |
608 if (asf->packet_timestamp_start == -1) | |
609 asf->packet_timestamp_start = timestamp; | |
610 asf->packet_nb_frames++; | |
611 } else { | |
612 frag_len = 0; | |
613 } | |
614 frag_pos += frag_len; | |
615 buf += frag_len; | |
616 /* output the frame if filled */ | |
617 if (asf->packet_size_left <= FRAME_HEADER_SIZE) | |
618 flush_packet(s); | |
619 } | |
620 stream->seq++; | |
621 } | |
622 | |
623 | |
624 static int asf_write_packet(AVFormatContext *s, int stream_index, | |
241 | 625 const uint8_t *buf, int size, int64_t timestamp) |
0 | 626 { |
627 ASFContext *asf = s->priv_data; | |
628 ASFStream *stream; | |
65 | 629 int64_t duration; |
0 | 630 AVCodecContext *codec; |
631 | |
632 codec = &s->streams[stream_index]->codec; | |
633 stream = &asf->streams[stream_index]; | |
634 | |
635 if (codec->codec_type == CODEC_TYPE_AUDIO) { | |
65 | 636 duration = (codec->frame_number * codec->frame_size * int64_t_C(10000000)) / |
0 | 637 codec->sample_rate; |
638 } else { | |
85
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
84
diff
changeset
|
639 duration = av_rescale(codec->frame_number * codec->frame_rate_base, 10000000, codec->frame_rate); |
0 | 640 } |
641 if (duration > asf->duration) | |
642 asf->duration = duration; | |
643 | |
644 put_frame(s, stream, timestamp, buf, size); | |
645 return 0; | |
646 } | |
647 | |
648 static int asf_write_trailer(AVFormatContext *s) | |
649 { | |
650 ASFContext *asf = s->priv_data; | |
65 | 651 int64_t file_size; |
0 | 652 |
653 /* flush the current packet */ | |
654 if (asf->pb.buf_ptr > asf->pb.buffer) | |
655 flush_packet(s); | |
656 | |
657 if (asf->is_streamed) { | |
658 put_chunk(s, 0x4524, 0, 0); /* end of stream */ | |
659 } else { | |
660 /* rewrite an updated header */ | |
661 file_size = url_ftell(&s->pb); | |
662 url_fseek(&s->pb, 0, SEEK_SET); | |
663 asf_write_header1(s, file_size, file_size - asf->data_offset); | |
664 } | |
665 | |
666 put_flush_packet(&s->pb); | |
667 return 0; | |
668 } | |
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
241
diff
changeset
|
669 #endif //CONFIG_ENCODERS |
0 | 670 |
671 /**********************************/ | |
672 /* decoding */ | |
673 | |
674 //#define DEBUG | |
675 | |
676 #ifdef DEBUG | |
69 | 677 #define PRINT_IF_GUID(g,cmp) \ |
678 if (!memcmp(g, &cmp, sizeof(GUID))) \ | |
679 printf("(GUID: %s) ", #cmp) | |
680 | |
0 | 681 static void print_guid(const GUID *g) |
682 { | |
683 int i; | |
69 | 684 PRINT_IF_GUID(g, asf_header); |
685 else PRINT_IF_GUID(g, file_header); | |
686 else PRINT_IF_GUID(g, stream_header); | |
687 else PRINT_IF_GUID(g, audio_stream); | |
688 else PRINT_IF_GUID(g, audio_conceal_none); | |
689 else PRINT_IF_GUID(g, video_stream); | |
690 else PRINT_IF_GUID(g, video_conceal_none); | |
691 else PRINT_IF_GUID(g, comment_header); | |
692 else PRINT_IF_GUID(g, codec_comment_header); | |
693 else PRINT_IF_GUID(g, codec_comment1_header); | |
694 else PRINT_IF_GUID(g, data_header); | |
695 else PRINT_IF_GUID(g, index_guid); | |
696 else PRINT_IF_GUID(g, head1_guid); | |
697 else PRINT_IF_GUID(g, head2_guid); | |
698 else PRINT_IF_GUID(g, my_guid); | |
699 else | |
700 printf("(GUID: unknown) "); | |
0 | 701 printf("0x%08x, 0x%04x, 0x%04x, {", g->v1, g->v2, g->v3); |
702 for(i=0;i<8;i++) | |
703 printf(" 0x%02x,", g->v4[i]); | |
704 printf("}\n"); | |
705 } | |
69 | 706 #undef PRINT_IF_GUID(g,cmp) |
0 | 707 #endif |
708 | |
709 static void get_guid(ByteIOContext *s, GUID *g) | |
710 { | |
711 int i; | |
712 | |
713 g->v1 = get_le32(s); | |
714 g->v2 = get_le16(s); | |
715 g->v3 = get_le16(s); | |
716 for(i=0;i<8;i++) | |
717 g->v4[i] = get_byte(s); | |
718 } | |
719 | |
720 #if 0 | |
721 static void get_str16(ByteIOContext *pb, char *buf, int buf_size) | |
722 { | |
723 int len, c; | |
724 char *q; | |
725 | |
726 len = get_le16(pb); | |
727 q = buf; | |
728 while (len > 0) { | |
729 c = get_le16(pb); | |
730 if ((q - buf) < buf_size - 1) | |
731 *q++ = c; | |
732 len--; | |
733 } | |
734 *q = '\0'; | |
735 } | |
736 #endif | |
737 | |
738 static void get_str16_nolen(ByteIOContext *pb, int len, char *buf, int buf_size) | |
739 { | |
740 int c; | |
741 char *q; | |
742 | |
743 q = buf; | |
744 while (len > 0) { | |
745 c = get_le16(pb); | |
746 if ((q - buf) < buf_size - 1) | |
747 *q++ = c; | |
748 len-=2; | |
749 } | |
750 *q = '\0'; | |
751 } | |
752 | |
753 static int asf_probe(AVProbeData *pd) | |
754 { | |
755 GUID g; | |
756 const unsigned char *p; | |
757 int i; | |
758 | |
759 /* check file header */ | |
760 if (pd->buf_size <= 32) | |
761 return 0; | |
762 p = pd->buf; | |
763 g.v1 = p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24); | |
764 p += 4; | |
765 g.v2 = p[0] | (p[1] << 8); | |
766 p += 2; | |
767 g.v3 = p[0] | (p[1] << 8); | |
768 p += 2; | |
769 for(i=0;i<8;i++) | |
770 g.v4[i] = *p++; | |
771 | |
772 if (!memcmp(&g, &asf_header, sizeof(GUID))) | |
773 return AVPROBE_SCORE_MAX; | |
774 else | |
775 return 0; | |
776 } | |
777 | |
778 static int asf_read_header(AVFormatContext *s, AVFormatParameters *ap) | |
779 { | |
780 ASFContext *asf = s->priv_data; | |
781 GUID g; | |
782 ByteIOContext *pb = &s->pb; | |
783 AVStream *st; | |
784 ASFStream *asf_st; | |
785 int size, i; | |
65 | 786 int64_t gsize; |
0 | 787 |
788 av_set_pts_info(s, 32, 1, 1000); /* 32 bit pts in ms */ | |
789 | |
790 get_guid(pb, &g); | |
791 if (memcmp(&g, &asf_header, sizeof(GUID))) | |
792 goto fail; | |
793 get_le64(pb); | |
794 get_le32(pb); | |
795 get_byte(pb); | |
796 get_byte(pb); | |
797 memset(&asf->asfid2avid, -1, sizeof(asf->asfid2avid)); | |
798 for(;;) { | |
799 get_guid(pb, &g); | |
800 gsize = get_le64(pb); | |
801 #ifdef DEBUG | |
802 printf("%08Lx: ", url_ftell(pb) - 24); | |
803 print_guid(&g); | |
804 printf(" size=0x%Lx\n", gsize); | |
805 #endif | |
806 if (gsize < 24) | |
807 goto fail; | |
808 if (!memcmp(&g, &file_header, sizeof(GUID))) { | |
809 get_guid(pb, &asf->hdr.guid); | |
810 asf->hdr.file_size = get_le64(pb); | |
811 asf->hdr.create_time = get_le64(pb); | |
812 asf->hdr.packets_count = get_le64(pb); | |
813 asf->hdr.play_time = get_le64(pb); | |
814 asf->hdr.send_time = get_le64(pb); | |
815 asf->hdr.preroll = get_le32(pb); | |
816 asf->hdr.ignore = get_le32(pb); | |
817 asf->hdr.flags = get_le32(pb); | |
818 asf->hdr.min_pktsize = get_le32(pb); | |
819 asf->hdr.max_pktsize = get_le32(pb); | |
820 asf->hdr.max_bitrate = get_le32(pb); | |
821 asf->packet_size = asf->hdr.max_pktsize; | |
822 asf->nb_packets = asf->hdr.packets_count; | |
823 } else if (!memcmp(&g, &stream_header, sizeof(GUID))) { | |
84
0068a6902911
correct AUDIO strf parsing patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
74
diff
changeset
|
824 int type, total_size, type_specific_size; |
0 | 825 unsigned int tag1; |
65 | 826 int64_t pos1, pos2; |
0 | 827 |
828 pos1 = url_ftell(pb); | |
829 | |
188
6c9d6422a2f6
update duration and start_time - add av_new_stream() usage
bellard
parents:
136
diff
changeset
|
830 st = av_new_stream(s, 0); |
0 | 831 if (!st) |
832 goto fail; | |
833 asf_st = av_mallocz(sizeof(ASFStream)); | |
834 if (!asf_st) | |
835 goto fail; | |
836 st->priv_data = asf_st; | |
188
6c9d6422a2f6
update duration and start_time - add av_new_stream() usage
bellard
parents:
136
diff
changeset
|
837 st->start_time = asf->hdr.preroll / (10000000 / AV_TIME_BASE); |
6c9d6422a2f6
update duration and start_time - add av_new_stream() usage
bellard
parents:
136
diff
changeset
|
838 st->duration = (asf->hdr.send_time - asf->hdr.preroll) / |
6c9d6422a2f6
update duration and start_time - add av_new_stream() usage
bellard
parents:
136
diff
changeset
|
839 (10000000 / AV_TIME_BASE); |
0 | 840 get_guid(pb, &g); |
841 if (!memcmp(&g, &audio_stream, sizeof(GUID))) { | |
842 type = CODEC_TYPE_AUDIO; | |
843 } else if (!memcmp(&g, &video_stream, sizeof(GUID))) { | |
844 type = CODEC_TYPE_VIDEO; | |
845 } else { | |
846 goto fail; | |
847 } | |
848 get_guid(pb, &g); | |
849 total_size = get_le64(pb); | |
84
0068a6902911
correct AUDIO strf parsing patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
74
diff
changeset
|
850 type_specific_size = get_le32(pb); |
0 | 851 get_le32(pb); |
852 st->id = get_le16(pb) & 0x7f; /* stream id */ | |
853 // mapping of asf ID to AV stream ID; | |
188
6c9d6422a2f6
update duration and start_time - add av_new_stream() usage
bellard
parents:
136
diff
changeset
|
854 asf->asfid2avid[st->id] = s->nb_streams - 1; |
0 | 855 |
856 get_le32(pb); | |
857 st->codec.codec_type = type; | |
312
8a04d2e1be2f
frame rate should be completely disabled in asf (closer now) - disabled seek
bellard
parents:
300
diff
changeset
|
858 /* 1 fps default (XXX: put 0 fps instead) */ |
8a04d2e1be2f
frame rate should be completely disabled in asf (closer now) - disabled seek
bellard
parents:
300
diff
changeset
|
859 st->codec.frame_rate = 1; |
8a04d2e1be2f
frame rate should be completely disabled in asf (closer now) - disabled seek
bellard
parents:
300
diff
changeset
|
860 st->codec.frame_rate_base = 1; |
0 | 861 if (type == CODEC_TYPE_AUDIO) { |
84
0068a6902911
correct AUDIO strf parsing patch by (Roman Shaposhnick <rvs at sun dot com>)
michaelni
parents:
74
diff
changeset
|
862 get_wav_header(pb, &st->codec, type_specific_size); |
312
8a04d2e1be2f
frame rate should be completely disabled in asf (closer now) - disabled seek
bellard
parents:
300
diff
changeset
|
863 st->need_parsing = 1; |
0 | 864 /* We have to init the frame size at some point .... */ |
865 pos2 = url_ftell(pb); | |
866 if (gsize > (pos2 + 8 - pos1 + 24)) { | |
867 asf_st->ds_span = get_byte(pb); | |
868 asf_st->ds_packet_size = get_le16(pb); | |
869 asf_st->ds_chunk_size = get_le16(pb); | |
870 asf_st->ds_data_size = get_le16(pb); | |
871 asf_st->ds_silence_data = get_byte(pb); | |
872 } | |
873 //printf("Descrambling: ps:%d cs:%d ds:%d s:%d sd:%d\n", | |
874 // asf_st->ds_packet_size, asf_st->ds_chunk_size, | |
875 // asf_st->ds_data_size, asf_st->ds_span, asf_st->ds_silence_data); | |
876 if (asf_st->ds_span > 1) { | |
877 if (!asf_st->ds_chunk_size | |
878 || (asf_st->ds_packet_size/asf_st->ds_chunk_size <= 1)) | |
879 asf_st->ds_span = 0; // disable descrambling | |
880 } | |
881 switch (st->codec.codec_id) { | |
232 | 882 case CODEC_ID_MP3: |
0 | 883 st->codec.frame_size = MPA_FRAME_SIZE; |
884 break; | |
885 case CODEC_ID_PCM_S16LE: | |
886 case CODEC_ID_PCM_S16BE: | |
887 case CODEC_ID_PCM_U16LE: | |
888 case CODEC_ID_PCM_U16BE: | |
889 case CODEC_ID_PCM_S8: | |
890 case CODEC_ID_PCM_U8: | |
891 case CODEC_ID_PCM_ALAW: | |
892 case CODEC_ID_PCM_MULAW: | |
893 st->codec.frame_size = 1; | |
894 break; | |
895 default: | |
896 /* This is probably wrong, but it prevents a crash later */ | |
897 st->codec.frame_size = 1; | |
898 break; | |
899 } | |
900 } else { | |
901 get_le32(pb); | |
902 get_le32(pb); | |
903 get_byte(pb); | |
904 size = get_le16(pb); /* size */ | |
905 get_le32(pb); /* size */ | |
906 st->codec.width = get_le32(pb); | |
907 st->codec.height = get_le32(pb); | |
908 /* not available for asf */ | |
909 get_le16(pb); /* panes */ | |
117 | 910 st->codec.bits_per_sample = get_le16(pb); /* depth */ |
0 | 911 tag1 = get_le32(pb); |
912 url_fskip(pb, 20); | |
913 if (size > 40) { | |
914 st->codec.extradata_size = size - 40; | |
915 st->codec.extradata = av_mallocz(st->codec.extradata_size); | |
916 get_buffer(pb, st->codec.extradata, st->codec.extradata_size); | |
917 } | |
297
85d558a18134
Make avi and asf demuxer export palette in palctrl
rtognimp
parents:
277
diff
changeset
|
918 |
85d558a18134
Make avi and asf demuxer export palette in palctrl
rtognimp
parents:
277
diff
changeset
|
919 /* Extract palette from extradata if bpp <= 8 */ |
85d558a18134
Make avi and asf demuxer export palette in palctrl
rtognimp
parents:
277
diff
changeset
|
920 /* This code assumes that extradata contains only palette */ |
85d558a18134
Make avi and asf demuxer export palette in palctrl
rtognimp
parents:
277
diff
changeset
|
921 /* This is true for all paletted codecs implemented in ffmpeg */ |
85d558a18134
Make avi and asf demuxer export palette in palctrl
rtognimp
parents:
277
diff
changeset
|
922 if (st->codec.extradata_size && (st->codec.bits_per_sample <= 8)) { |
85d558a18134
Make avi and asf demuxer export palette in palctrl
rtognimp
parents:
277
diff
changeset
|
923 st->codec.palctrl = av_mallocz(sizeof(AVPaletteControl)); |
85d558a18134
Make avi and asf demuxer export palette in palctrl
rtognimp
parents:
277
diff
changeset
|
924 #ifdef WORDS_BIGENDIAN |
300
6ee1b02f9b2a
* fixes for broken builds on Solaris, OS2 and all bingendian
romansh
parents:
297
diff
changeset
|
925 for (i = 0; i < FFMIN(st->codec.extradata_size, AVPALETTE_SIZE)/4; i++) |
6ee1b02f9b2a
* fixes for broken builds on Solaris, OS2 and all bingendian
romansh
parents:
297
diff
changeset
|
926 st->codec.palctrl->palette[i] = bswap_32(((uint32_t*)st->codec.extradata)[i]); |
297
85d558a18134
Make avi and asf demuxer export palette in palctrl
rtognimp
parents:
277
diff
changeset
|
927 #else |
85d558a18134
Make avi and asf demuxer export palette in palctrl
rtognimp
parents:
277
diff
changeset
|
928 memcpy(st->codec.palctrl->palette, st->codec.extradata, |
85d558a18134
Make avi and asf demuxer export palette in palctrl
rtognimp
parents:
277
diff
changeset
|
929 FFMIN(st->codec.extradata_size, AVPALETTE_SIZE)); |
85d558a18134
Make avi and asf demuxer export palette in palctrl
rtognimp
parents:
277
diff
changeset
|
930 #endif |
85d558a18134
Make avi and asf demuxer export palette in palctrl
rtognimp
parents:
277
diff
changeset
|
931 st->codec.palctrl->palette_changed = 1; |
85d558a18134
Make avi and asf demuxer export palette in palctrl
rtognimp
parents:
277
diff
changeset
|
932 } |
85d558a18134
Make avi and asf demuxer export palette in palctrl
rtognimp
parents:
277
diff
changeset
|
933 |
74 | 934 st->codec.codec_tag = tag1; |
0 | 935 st->codec.codec_id = codec_get_id(codec_bmp_tags, tag1); |
936 } | |
937 pos2 = url_ftell(pb); | |
938 url_fskip(pb, gsize - (pos2 - pos1 + 24)); | |
939 } else if (!memcmp(&g, &data_header, sizeof(GUID))) { | |
940 break; | |
941 } else if (!memcmp(&g, &comment_header, sizeof(GUID))) { | |
942 int len1, len2, len3, len4, len5; | |
943 | |
944 len1 = get_le16(pb); | |
945 len2 = get_le16(pb); | |
946 len3 = get_le16(pb); | |
947 len4 = get_le16(pb); | |
948 len5 = get_le16(pb); | |
949 get_str16_nolen(pb, len1, s->title, sizeof(s->title)); | |
950 get_str16_nolen(pb, len2, s->author, sizeof(s->author)); | |
951 get_str16_nolen(pb, len3, s->copyright, sizeof(s->copyright)); | |
952 get_str16_nolen(pb, len4, s->comment, sizeof(s->comment)); | |
953 url_fskip(pb, len5); | |
341
ad2a57c5467a
ASF extended header parsing patch by ("Kevin Kuphal" <kevin dot kuphal at sitecity dot net>)
michael
parents:
312
diff
changeset
|
954 } else if (!memcmp(&g, &extended_content_header, sizeof(GUID))) { |
ad2a57c5467a
ASF extended header parsing patch by ("Kevin Kuphal" <kevin dot kuphal at sitecity dot net>)
michael
parents:
312
diff
changeset
|
955 int desc_count, i; |
ad2a57c5467a
ASF extended header parsing patch by ("Kevin Kuphal" <kevin dot kuphal at sitecity dot net>)
michael
parents:
312
diff
changeset
|
956 |
ad2a57c5467a
ASF extended header parsing patch by ("Kevin Kuphal" <kevin dot kuphal at sitecity dot net>)
michael
parents:
312
diff
changeset
|
957 desc_count = get_le16(pb); |
ad2a57c5467a
ASF extended header parsing patch by ("Kevin Kuphal" <kevin dot kuphal at sitecity dot net>)
michael
parents:
312
diff
changeset
|
958 for(i=0;i<desc_count;i++) |
ad2a57c5467a
ASF extended header parsing patch by ("Kevin Kuphal" <kevin dot kuphal at sitecity dot net>)
michael
parents:
312
diff
changeset
|
959 { |
ad2a57c5467a
ASF extended header parsing patch by ("Kevin Kuphal" <kevin dot kuphal at sitecity dot net>)
michael
parents:
312
diff
changeset
|
960 int name_len,value_type,value_len,value_num = 0; |
ad2a57c5467a
ASF extended header parsing patch by ("Kevin Kuphal" <kevin dot kuphal at sitecity dot net>)
michael
parents:
312
diff
changeset
|
961 char *name, *value; |
ad2a57c5467a
ASF extended header parsing patch by ("Kevin Kuphal" <kevin dot kuphal at sitecity dot net>)
michael
parents:
312
diff
changeset
|
962 |
ad2a57c5467a
ASF extended header parsing patch by ("Kevin Kuphal" <kevin dot kuphal at sitecity dot net>)
michael
parents:
312
diff
changeset
|
963 name_len = get_le16(pb); |
ad2a57c5467a
ASF extended header parsing patch by ("Kevin Kuphal" <kevin dot kuphal at sitecity dot net>)
michael
parents:
312
diff
changeset
|
964 name = (char *)av_mallocz(name_len); |
ad2a57c5467a
ASF extended header parsing patch by ("Kevin Kuphal" <kevin dot kuphal at sitecity dot net>)
michael
parents:
312
diff
changeset
|
965 get_str16_nolen(pb, name_len, name, name_len); |
ad2a57c5467a
ASF extended header parsing patch by ("Kevin Kuphal" <kevin dot kuphal at sitecity dot net>)
michael
parents:
312
diff
changeset
|
966 value_type = get_le16(pb); |
ad2a57c5467a
ASF extended header parsing patch by ("Kevin Kuphal" <kevin dot kuphal at sitecity dot net>)
michael
parents:
312
diff
changeset
|
967 value_len = get_le16(pb); |
ad2a57c5467a
ASF extended header parsing patch by ("Kevin Kuphal" <kevin dot kuphal at sitecity dot net>)
michael
parents:
312
diff
changeset
|
968 if ((value_type == 0) || (value_type == 1)) // unicode or byte |
ad2a57c5467a
ASF extended header parsing patch by ("Kevin Kuphal" <kevin dot kuphal at sitecity dot net>)
michael
parents:
312
diff
changeset
|
969 { |
ad2a57c5467a
ASF extended header parsing patch by ("Kevin Kuphal" <kevin dot kuphal at sitecity dot net>)
michael
parents:
312
diff
changeset
|
970 value = (char *)av_mallocz(value_len); |
ad2a57c5467a
ASF extended header parsing patch by ("Kevin Kuphal" <kevin dot kuphal at sitecity dot net>)
michael
parents:
312
diff
changeset
|
971 get_str16_nolen(pb, value_len, value, value_len); |
ad2a57c5467a
ASF extended header parsing patch by ("Kevin Kuphal" <kevin dot kuphal at sitecity dot net>)
michael
parents:
312
diff
changeset
|
972 if (strcmp(name,"WM/AlbumTitle")==0) { strcpy(s->album, value); } |
ad2a57c5467a
ASF extended header parsing patch by ("Kevin Kuphal" <kevin dot kuphal at sitecity dot net>)
michael
parents:
312
diff
changeset
|
973 av_free(value); |
ad2a57c5467a
ASF extended header parsing patch by ("Kevin Kuphal" <kevin dot kuphal at sitecity dot net>)
michael
parents:
312
diff
changeset
|
974 } |
ad2a57c5467a
ASF extended header parsing patch by ("Kevin Kuphal" <kevin dot kuphal at sitecity dot net>)
michael
parents:
312
diff
changeset
|
975 if ((value_type >= 2) || (value_type <= 5)) // boolean or DWORD or QWORD or WORD |
ad2a57c5467a
ASF extended header parsing patch by ("Kevin Kuphal" <kevin dot kuphal at sitecity dot net>)
michael
parents:
312
diff
changeset
|
976 { |
ad2a57c5467a
ASF extended header parsing patch by ("Kevin Kuphal" <kevin dot kuphal at sitecity dot net>)
michael
parents:
312
diff
changeset
|
977 if (value_type==2) value_num = get_le32(pb); |
ad2a57c5467a
ASF extended header parsing patch by ("Kevin Kuphal" <kevin dot kuphal at sitecity dot net>)
michael
parents:
312
diff
changeset
|
978 if (value_type==3) value_num = get_le32(pb); |
ad2a57c5467a
ASF extended header parsing patch by ("Kevin Kuphal" <kevin dot kuphal at sitecity dot net>)
michael
parents:
312
diff
changeset
|
979 if (value_type==4) value_num = get_le64(pb); |
ad2a57c5467a
ASF extended header parsing patch by ("Kevin Kuphal" <kevin dot kuphal at sitecity dot net>)
michael
parents:
312
diff
changeset
|
980 if (value_type==5) value_num = get_le16(pb); |
ad2a57c5467a
ASF extended header parsing patch by ("Kevin Kuphal" <kevin dot kuphal at sitecity dot net>)
michael
parents:
312
diff
changeset
|
981 if (strcmp(name,"WM/Track")==0) s->track = value_num + 1; |
ad2a57c5467a
ASF extended header parsing patch by ("Kevin Kuphal" <kevin dot kuphal at sitecity dot net>)
michael
parents:
312
diff
changeset
|
982 if (strcmp(name,"WM/TrackNumber")==0) s->track = value_num; |
ad2a57c5467a
ASF extended header parsing patch by ("Kevin Kuphal" <kevin dot kuphal at sitecity dot net>)
michael
parents:
312
diff
changeset
|
983 } |
ad2a57c5467a
ASF extended header parsing patch by ("Kevin Kuphal" <kevin dot kuphal at sitecity dot net>)
michael
parents:
312
diff
changeset
|
984 av_free(name); |
ad2a57c5467a
ASF extended header parsing patch by ("Kevin Kuphal" <kevin dot kuphal at sitecity dot net>)
michael
parents:
312
diff
changeset
|
985 } |
0 | 986 #if 0 |
987 } else if (!memcmp(&g, &head1_guid, sizeof(GUID))) { | |
988 int v1, v2; | |
989 get_guid(pb, &g); | |
990 v1 = get_le32(pb); | |
991 v2 = get_le16(pb); | |
992 } else if (!memcmp(&g, &codec_comment_header, sizeof(GUID))) { | |
993 int len, v1, n, num; | |
994 char str[256], *q; | |
995 char tag[16]; | |
996 | |
997 get_guid(pb, &g); | |
998 print_guid(&g); | |
999 | |
1000 n = get_le32(pb); | |
1001 for(i=0;i<n;i++) { | |
1002 num = get_le16(pb); /* stream number */ | |
1003 get_str16(pb, str, sizeof(str)); | |
1004 get_str16(pb, str, sizeof(str)); | |
1005 len = get_le16(pb); | |
1006 q = tag; | |
1007 while (len > 0) { | |
1008 v1 = get_byte(pb); | |
1009 if ((q - tag) < sizeof(tag) - 1) | |
1010 *q++ = v1; | |
1011 len--; | |
1012 } | |
1013 *q = '\0'; | |
1014 } | |
1015 #endif | |
1016 } else if (url_feof(pb)) { | |
1017 goto fail; | |
1018 } else { | |
1019 url_fseek(pb, gsize - 24, SEEK_CUR); | |
1020 } | |
1021 } | |
1022 get_guid(pb, &g); | |
1023 get_le64(pb); | |
1024 get_byte(pb); | |
1025 get_byte(pb); | |
1026 if (url_feof(pb)) | |
1027 goto fail; | |
1028 asf->data_offset = url_ftell(pb); | |
1029 asf->packet_size_left = 0; | |
1030 | |
1031 return 0; | |
1032 | |
1033 fail: | |
1034 for(i=0;i<s->nb_streams;i++) { | |
1035 AVStream *st = s->streams[i]; | |
1036 if (st) { | |
1037 av_free(st->priv_data); | |
1038 av_free(st->codec.extradata); | |
1039 } | |
1040 av_free(st); | |
1041 } | |
1042 return -1; | |
1043 } | |
1044 | |
1045 #define DO_2BITS(bits, var, defval) \ | |
1046 switch (bits & 3) \ | |
1047 { \ | |
1048 case 3: var = get_le32(pb); rsize += 4; break; \ | |
1049 case 2: var = get_le16(pb); rsize += 2; break; \ | |
1050 case 1: var = get_byte(pb); rsize++; break; \ | |
1051 default: var = defval; break; \ | |
1052 } | |
1053 | |
1054 static int asf_get_packet(AVFormatContext *s) | |
1055 { | |
1056 ASFContext *asf = s->priv_data; | |
1057 ByteIOContext *pb = &s->pb; | |
1058 uint32_t packet_length, padsize; | |
1059 int rsize = 11; | |
1060 int c = get_byte(pb); | |
1061 if (c != 0x82) { | |
1062 if (!url_feof(pb)) | |
136 | 1063 printf("ff asf bad header %x at:%lld\n", c, url_ftell(pb)); |
0 | 1064 return -EIO; |
1065 } | |
1066 if ((c & 0x0f) == 2) { // always true for now | |
1067 if (get_le16(pb) != 0) { | |
1068 if (!url_feof(pb)) | |
1069 printf("ff asf bad non zero\n"); | |
1070 return -EIO; | |
1071 } | |
1072 } | |
1073 | |
1074 asf->packet_flags = get_byte(pb); | |
1075 asf->packet_property = get_byte(pb); | |
1076 | |
1077 DO_2BITS(asf->packet_flags >> 5, packet_length, asf->packet_size); | |
1078 DO_2BITS(asf->packet_flags >> 1, padsize, 0); // sequence ignored | |
1079 DO_2BITS(asf->packet_flags >> 3, padsize, 0); // padding length | |
1080 | |
1081 asf->packet_timestamp = get_le32(pb); | |
1082 get_le16(pb); /* duration */ | |
1083 // rsize has at least 11 bytes which have to be present | |
1084 | |
1085 if (asf->packet_flags & 0x01) { | |
1086 asf->packet_segsizetype = get_byte(pb); rsize++; | |
1087 asf->packet_segments = asf->packet_segsizetype & 0x3f; | |
1088 } else { | |
1089 asf->packet_segments = 1; | |
1090 asf->packet_segsizetype = 0x80; | |
1091 } | |
1092 asf->packet_size_left = packet_length - padsize - rsize; | |
1093 if (packet_length < asf->hdr.min_pktsize) | |
1094 padsize += asf->hdr.min_pktsize - packet_length; | |
1095 asf->packet_padsize = padsize; | |
1096 #ifdef DEBUG | |
1097 printf("packet: size=%d padsize=%d left=%d\n", asf->packet_size, asf->packet_padsize, asf->packet_size_left); | |
1098 #endif | |
1099 return 0; | |
1100 } | |
1101 | |
1102 static int asf_read_packet(AVFormatContext *s, AVPacket *pkt) | |
1103 { | |
1104 ASFContext *asf = s->priv_data; | |
1105 ASFStream *asf_st = 0; | |
1106 ByteIOContext *pb = &s->pb; | |
1107 //static int pc = 0; | |
1108 for (;;) { | |
1109 int rsize = 0; | |
1110 if (asf->packet_size_left < FRAME_HEADER_SIZE | |
1111 || asf->packet_segments < 1) { | |
1112 //asf->packet_size_left <= asf->packet_padsize) { | |
1113 int ret = asf->packet_size_left + asf->packet_padsize; | |
1114 //printf("PacketLeftSize:%d Pad:%d Pos:%Ld\n", asf->packet_size_left, asf->packet_padsize, url_ftell(pb)); | |
1115 /* fail safe */ | |
1116 url_fskip(pb, ret); | |
1117 ret = asf_get_packet(s); | |
1118 //printf("READ ASF PACKET %d r:%d c:%d\n", ret, asf->packet_size_left, pc++); | |
1119 if (ret < 0 || url_feof(pb)) | |
1120 return -EIO; | |
1121 asf->packet_time_start = 0; | |
1122 continue; | |
1123 } | |
1124 if (asf->packet_time_start == 0) { | |
1125 /* read frame header */ | |
1126 int num = get_byte(pb); | |
1127 asf->packet_segments--; | |
1128 rsize++; | |
1129 asf->packet_key_frame = (num & 0x80) >> 7; | |
1130 asf->stream_index = asf->asfid2avid[num & 0x7f]; | |
1131 // sequence should be ignored! | |
1132 DO_2BITS(asf->packet_property >> 4, asf->packet_seq, 0); | |
1133 DO_2BITS(asf->packet_property >> 2, asf->packet_frag_offset, 0); | |
1134 DO_2BITS(asf->packet_property, asf->packet_replic_size, 0); | |
1135 | |
1136 if (asf->packet_replic_size > 1) { | |
1137 // it should be always at least 8 bytes - FIXME validate | |
1138 asf->packet_obj_size = get_le32(pb); | |
1139 asf->packet_frag_timestamp = get_le32(pb); // timestamp | |
1140 if (asf->packet_replic_size > 8) | |
1141 url_fskip(pb, asf->packet_replic_size - 8); | |
1142 rsize += asf->packet_replic_size; // FIXME - check validity | |
1143 } else { | |
1144 // multipacket - frag_offset is begining timestamp | |
1145 asf->packet_time_start = asf->packet_frag_offset; | |
1146 asf->packet_frag_offset = 0; | |
1147 asf->packet_frag_timestamp = asf->packet_timestamp; | |
1148 | |
1149 if (asf->packet_replic_size == 1) { | |
1150 asf->packet_time_delta = get_byte(pb); | |
1151 rsize++; | |
1152 } | |
1153 } | |
1154 if (asf->packet_flags & 0x01) { | |
1155 DO_2BITS(asf->packet_segsizetype >> 6, asf->packet_frag_size, 0); // 0 is illegal | |
1156 #undef DO_2BITS | |
1157 //printf("Fragsize %d\n", asf->packet_frag_size); | |
1158 } else { | |
1159 asf->packet_frag_size = asf->packet_size_left - rsize; | |
1160 //printf("Using rest %d %d %d\n", asf->packet_frag_size, asf->packet_size_left, rsize); | |
1161 } | |
1162 if (asf->packet_replic_size == 1) { | |
1163 asf->packet_multi_size = asf->packet_frag_size; | |
1164 if (asf->packet_multi_size > asf->packet_size_left) { | |
1165 asf->packet_segments = 0; | |
1166 continue; | |
1167 } | |
1168 } | |
1169 asf->packet_size_left -= rsize; | |
1170 //printf("___objsize____ %d %d rs:%d\n", asf->packet_obj_size, asf->packet_frag_offset, rsize); | |
1171 | |
1172 if (asf->stream_index < 0) { | |
1173 asf->packet_time_start = 0; | |
1174 /* unhandled packet (should not happen) */ | |
1175 url_fskip(pb, asf->packet_frag_size); | |
1176 asf->packet_size_left -= asf->packet_frag_size; | |
1177 printf("ff asf skip %d %d\n", asf->packet_frag_size, num & 0x7f); | |
1178 continue; | |
1179 } | |
1180 asf->asf_st = s->streams[asf->stream_index]->priv_data; | |
1181 } | |
1182 asf_st = asf->asf_st; | |
1183 | |
1184 if ((asf->packet_frag_offset != asf_st->frag_offset | |
1185 || (asf->packet_frag_offset | |
1186 && asf->packet_seq != asf_st->seq)) // seq should be ignored | |
1187 ) { | |
1188 /* cannot continue current packet: free it */ | |
1189 // FIXME better check if packet was already allocated | |
1190 printf("ff asf parser skips: %d - %d o:%d - %d %d %d fl:%d\n", | |
1191 asf_st->pkt.size, | |
1192 asf->packet_obj_size, | |
1193 asf->packet_frag_offset, asf_st->frag_offset, | |
1194 asf->packet_seq, asf_st->seq, asf->packet_frag_size); | |
1195 if (asf_st->pkt.size) | |
1196 av_free_packet(&asf_st->pkt); | |
1197 asf_st->frag_offset = 0; | |
1198 if (asf->packet_frag_offset != 0) { | |
1199 url_fskip(pb, asf->packet_frag_size); | |
1200 printf("ff asf parser skiping %db\n", asf->packet_frag_size); | |
1201 asf->packet_size_left -= asf->packet_frag_size; | |
1202 continue; | |
1203 } | |
1204 } | |
1205 if (asf->packet_replic_size == 1) { | |
1206 // frag_offset is here used as the begining timestamp | |
1207 asf->packet_frag_timestamp = asf->packet_time_start; | |
1208 asf->packet_time_start += asf->packet_time_delta; | |
1209 asf->packet_obj_size = asf->packet_frag_size = get_byte(pb); | |
1210 asf->packet_size_left--; | |
1211 asf->packet_multi_size--; | |
1212 if (asf->packet_multi_size < asf->packet_obj_size) | |
1213 { | |
1214 asf->packet_time_start = 0; | |
1215 url_fskip(pb, asf->packet_multi_size); | |
1216 asf->packet_size_left -= asf->packet_multi_size; | |
1217 continue; | |
1218 } | |
1219 asf->packet_multi_size -= asf->packet_obj_size; | |
1220 //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); | |
1221 } | |
1222 if (asf_st->frag_offset == 0) { | |
1223 /* new packet */ | |
1224 av_new_packet(&asf_st->pkt, asf->packet_obj_size); | |
1225 asf_st->seq = asf->packet_seq; | |
1226 asf_st->pkt.pts = asf->packet_frag_timestamp - asf->hdr.preroll; | |
1227 asf_st->pkt.stream_index = asf->stream_index; | |
1228 if (asf->packet_key_frame) | |
1229 asf_st->pkt.flags |= PKT_FLAG_KEY; | |
1230 } | |
1231 | |
1232 /* read data */ | |
1233 //printf("READ PACKET s:%d os:%d o:%d,%d l:%d DATA:%p\n", | |
1234 // asf->packet_size, asf_st->pkt.size, asf->packet_frag_offset, | |
1235 // asf_st->frag_offset, asf->packet_frag_size, asf_st->pkt.data); | |
1236 asf->packet_size_left -= asf->packet_frag_size; | |
1237 if (asf->packet_size_left < 0) | |
1238 continue; | |
1239 get_buffer(pb, asf_st->pkt.data + asf->packet_frag_offset, | |
1240 asf->packet_frag_size); | |
1241 asf_st->frag_offset += asf->packet_frag_size; | |
1242 /* test if whole packet is read */ | |
1243 if (asf_st->frag_offset == asf_st->pkt.size) { | |
1244 /* return packet */ | |
1245 if (asf_st->ds_span > 1) { | |
1246 /* packet descrambling */ | |
1247 char* newdata = av_malloc(asf_st->pkt.size); | |
1248 if (newdata) { | |
1249 int offset = 0; | |
1250 while (offset < asf_st->pkt.size) { | |
1251 int off = offset / asf_st->ds_chunk_size; | |
1252 int row = off / asf_st->ds_span; | |
1253 int col = off % asf_st->ds_span; | |
1254 int idx = row + col * asf_st->ds_packet_size / asf_st->ds_chunk_size; | |
1255 //printf("off:%d row:%d col:%d idx:%d\n", off, row, col, idx); | |
1256 memcpy(newdata + offset, | |
1257 asf_st->pkt.data + idx * asf_st->ds_chunk_size, | |
1258 asf_st->ds_chunk_size); | |
1259 offset += asf_st->ds_chunk_size; | |
1260 } | |
1261 av_free(asf_st->pkt.data); | |
1262 asf_st->pkt.data = newdata; | |
1263 } | |
1264 } | |
1265 asf_st->frag_offset = 0; | |
1266 memcpy(pkt, &asf_st->pkt, sizeof(AVPacket)); | |
1267 //printf("packet %d %d\n", asf_st->pkt.size, asf->packet_frag_size); | |
1268 asf_st->pkt.size = 0; | |
1269 asf_st->pkt.data = 0; | |
1270 break; // packet completed | |
1271 } | |
1272 } | |
1273 return 0; | |
1274 } | |
1275 | |
1276 static int asf_read_close(AVFormatContext *s) | |
1277 { | |
1278 int i; | |
1279 | |
1280 for(i=0;i<s->nb_streams;i++) { | |
1281 AVStream *st = s->streams[i]; | |
1282 av_free(st->priv_data); | |
1283 av_free(st->codec.extradata); | |
297
85d558a18134
Make avi and asf demuxer export palette in palctrl
rtognimp
parents:
277
diff
changeset
|
1284 av_free(st->codec.palctrl); |
0 | 1285 } |
1286 return 0; | |
1287 } | |
1288 | |
312
8a04d2e1be2f
frame rate should be completely disabled in asf (closer now) - disabled seek
bellard
parents:
300
diff
changeset
|
1289 static int asf_read_seek(AVFormatContext *s, int stream_index, int64_t pts) |
0 | 1290 { |
312
8a04d2e1be2f
frame rate should be completely disabled in asf (closer now) - disabled seek
bellard
parents:
300
diff
changeset
|
1291 #if 0 |
8a04d2e1be2f
frame rate should be completely disabled in asf (closer now) - disabled seek
bellard
parents:
300
diff
changeset
|
1292 ASFContext *asf = s->priv_data; |
8a04d2e1be2f
frame rate should be completely disabled in asf (closer now) - disabled seek
bellard
parents:
300
diff
changeset
|
1293 int i; |
8a04d2e1be2f
frame rate should be completely disabled in asf (closer now) - disabled seek
bellard
parents:
300
diff
changeset
|
1294 |
8a04d2e1be2f
frame rate should be completely disabled in asf (closer now) - disabled seek
bellard
parents:
300
diff
changeset
|
1295 for(i = 0;; i++) { |
8a04d2e1be2f
frame rate should be completely disabled in asf (closer now) - disabled seek
bellard
parents:
300
diff
changeset
|
1296 url_fseek(&s->pb, asf->data_offset + i * asf->packet_size, SEEK_SET); |
8a04d2e1be2f
frame rate should be completely disabled in asf (closer now) - disabled seek
bellard
parents:
300
diff
changeset
|
1297 if (asf_get_packet(s) < 0) |
8a04d2e1be2f
frame rate should be completely disabled in asf (closer now) - disabled seek
bellard
parents:
300
diff
changeset
|
1298 break; |
8a04d2e1be2f
frame rate should be completely disabled in asf (closer now) - disabled seek
bellard
parents:
300
diff
changeset
|
1299 printf("timestamp=%0.3f\n", asf->packet_timestamp / 1000.0); |
8a04d2e1be2f
frame rate should be completely disabled in asf (closer now) - disabled seek
bellard
parents:
300
diff
changeset
|
1300 } |
8a04d2e1be2f
frame rate should be completely disabled in asf (closer now) - disabled seek
bellard
parents:
300
diff
changeset
|
1301 #endif |
0 | 1302 return -1; |
1303 } | |
1304 | |
1305 static AVInputFormat asf_iformat = { | |
1306 "asf", | |
1307 "asf format", | |
1308 sizeof(ASFContext), | |
1309 asf_probe, | |
1310 asf_read_header, | |
1311 asf_read_packet, | |
1312 asf_read_close, | |
1313 asf_read_seek, | |
1314 }; | |
1315 | |
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
241
diff
changeset
|
1316 #ifdef CONFIG_ENCODERS |
0 | 1317 static AVOutputFormat asf_oformat = { |
1318 "asf", | |
1319 "asf format", | |
14
b167760cd0aa
mimetype fixes patch by (Ryutaroh Matsumoto <ryutaroh at it dot ss dot titech dot ac dot jp>)
michaelni
parents:
7
diff
changeset
|
1320 "video/x-ms-asf", |
0 | 1321 "asf,wmv", |
1322 sizeof(ASFContext), | |
1323 #ifdef CONFIG_MP3LAME | |
232 | 1324 CODEC_ID_MP3, |
0 | 1325 #else |
1326 CODEC_ID_MP2, | |
1327 #endif | |
1328 CODEC_ID_MSMPEG4V3, | |
1329 asf_write_header, | |
1330 asf_write_packet, | |
1331 asf_write_trailer, | |
1332 }; | |
1333 | |
1334 static AVOutputFormat asf_stream_oformat = { | |
1335 "asf_stream", | |
1336 "asf format", | |
14
b167760cd0aa
mimetype fixes patch by (Ryutaroh Matsumoto <ryutaroh at it dot ss dot titech dot ac dot jp>)
michaelni
parents:
7
diff
changeset
|
1337 "video/x-ms-asf", |
0 | 1338 "asf,wmv", |
1339 sizeof(ASFContext), | |
1340 #ifdef CONFIG_MP3LAME | |
232 | 1341 CODEC_ID_MP3, |
0 | 1342 #else |
1343 CODEC_ID_MP2, | |
1344 #endif | |
1345 CODEC_ID_MSMPEG4V3, | |
1346 asf_write_stream_header, | |
1347 asf_write_packet, | |
1348 asf_write_trailer, | |
1349 }; | |
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
241
diff
changeset
|
1350 #endif //CONFIG_ENCODERS |
0 | 1351 |
1352 int asf_init(void) | |
1353 { | |
1354 av_register_input_format(&asf_iformat); | |
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
241
diff
changeset
|
1355 #ifdef CONFIG_ENCODERS |
0 | 1356 av_register_output_format(&asf_oformat); |
1357 av_register_output_format(&asf_stream_oformat); | |
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
241
diff
changeset
|
1358 #endif //CONFIG_ENCODERS |
0 | 1359 return 0; |
1360 } |