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