1 // ASF file parser for DEMUXER v0.3 by A'rpi/ESP-team
12 #include "stream/stream.h"
16 #include "libvo/fastmemcpy.h"
17 #include "libavutil/intreadwrite.h"
19 // defined at asfheader.c:
21 int asf_check_header(demuxer_t
*demuxer
);
22 int read_asf_header(demuxer_t
*demuxer
,struct asf_priv
* asf
);
24 // based on asf file-format doc by Eugene [http://divx.euro.ru]
27 * \brief reads int stored in number of bytes given by len
28 * \param ptr pointer to read from, is incremented appropriately
29 * \param len lowest 2 bits indicate number of bytes to read
30 * \param def default value to return if len is invalid
32 static inline unsigned read_varlen(uint8_t **ptr
, int len
, int def
) {
33 const uint8_t *p
= *ptr
;
36 case 1: *ptr
+= 1; return *p
;
37 case 2: *ptr
+= 2; return AV_RL16(p
);
38 case 3: *ptr
+= 4; return AV_RL32(p
);
44 * \brief checks if there is enough data to read the bytes given by len
45 * \param ptr pointer to read from
46 * \param endptr pointer to the end of the buffer
47 * \param len lowest 2 bits indicate number of bytes to read
49 static inline int check_varlen(uint8_t *ptr
, uint8_t *endptr
, int len
) {
50 return len
&3 ? ptr
+ (1<<((len
&3) - 1)) <= endptr
: 1;
53 static void asf_descrambling(unsigned char **src
,unsigned len
, struct asf_priv
* asf
){
55 unsigned char *s2
=*src
;
57 if (len
> UINT_MAX
- MP_INPUT_BUFFER_PADDING_SIZE
)
59 dst
= malloc(len
+ MP_INPUT_BUFFER_PADDING_SIZE
);
60 while(len
>=asf
->scrambling_h
*asf
->scrambling_w
*asf
->scrambling_b
+i
){
61 // mp_msg(MSGT_DEMUX,MSGL_DBG4,"descrambling! (w=%d b=%d)\n",w,asf_scrambling_b);
62 //i+=asf_scrambling_h*asf_scrambling_w;
63 for(x
=0;x
<asf
->scrambling_w
;x
++)
64 for(y
=0;y
<asf
->scrambling_h
;y
++){
65 fast_memcpy(dst
+i
,s2
+(y
*asf
->scrambling_w
+x
)*asf
->scrambling_b
,asf
->scrambling_b
);
68 s2
+=asf
->scrambling_h
*asf
->scrambling_w
*asf
->scrambling_b
;
70 //if(i<len) fast_memcpy(dst+i,src+i,len-i);
75 /*****************************************************************
76 * \brief initializes asf private data
79 static void init_priv (struct asf_priv
* asf
){
81 asf
->vid_ext_timing_index
=-1;
82 asf
->aud_ext_timing_index
=-1;
83 asf
->vid_ext_frame_index
=-1;
86 static void demux_asf_append_to_packet(demux_packet_t
* dp
,unsigned char *data
,int len
,int offs
)
88 if(dp
->len
!=offs
&& offs
!=-1) mp_msg(MSGT_DEMUX
,MSGL_V
,"warning! fragment.len=%d BUT next fragment offset=%d \n",dp
->len
,offs
);
89 dp
->buffer
=realloc(dp
->buffer
,dp
->len
+len
+MP_INPUT_BUFFER_PADDING_SIZE
);
90 fast_memcpy(dp
->buffer
+dp
->len
,data
,len
);
91 memset(dp
->buffer
+dp
->len
+len
, 0, MP_INPUT_BUFFER_PADDING_SIZE
);
92 mp_dbg(MSGT_DEMUX
,MSGL_DBG4
,"data appended! %d+%d\n",dp
->len
,len
);
96 static int demux_asf_read_packet(demuxer_t
*demux
,unsigned char *data
,int len
,int id
,int seq
,uint64_t time
,unsigned short dur
,int offs
,int keyframe
){
97 struct asf_priv
* asf
= demux
->priv
;
98 demux_stream_t
*ds
=NULL
;
101 mp_dbg(MSGT_DEMUX
,MSGL_DBG4
,"demux_asf.read_packet: id=%d seq=%d len=%d\n",id
,seq
,len
);
103 if(demux
->video
->id
==-1)
104 if(demux
->v_streams
[id
])
107 if(demux
->audio
->id
==-1)
108 if(demux
->a_streams
[id
])
111 if(id
==demux
->audio
->id
){
115 ds
->sh
=demux
->a_streams
[id
];
116 mp_msg(MSGT_DEMUX
,MSGL_V
,"Auto-selected ASF audio ID = %d\n",ds
->id
);
119 if(id
==demux
->video
->id
){
123 ds
->sh
=demux
->v_streams
[id
];
124 mp_msg(MSGT_DEMUX
,MSGL_V
,"Auto-selected ASF video ID = %d\n",ds
->id
);
130 demux_packet_t
* dp
=ds
->asf_packet
;
132 if (ds
==demux
->video
&& asf
->asf_is_dvr_ms
) {
133 if (asf
->new_vid_frame_seg
) {
134 dp
->pos
=demux
->filepos
;
136 } else seq
= ds
->asf_seq
;
137 } else close_seg
= ds
->asf_seq
!=seq
;
140 // closed segment, finalize packet:
142 if(asf
->scrambling_h
>1 && asf
->scrambling_w
>1 && asf
->scrambling_b
>0)
143 asf_descrambling(&ds
->asf_packet
->buffer
,ds
->asf_packet
->len
,asf
);
144 ds_add_packet(ds
,ds
->asf_packet
);
147 // append data to it!
148 demux_asf_append_to_packet(dp
,data
,len
,offs
);
153 // create new packet:
154 { demux_packet_t
* dp
;
156 mp_msg(MSGT_DEMUX
,MSGL_V
,"warning! broken fragment, %d bytes missing \n",offs
);
159 dp
=new_demux_packet(len
);
160 fast_memcpy(dp
->buffer
,data
,len
);
161 if (asf
->asf_is_dvr_ms
)
162 dp
->pts
=time
*0.0000001;
166 // if(ds==demux->video) printf("ASF time: %8d dur: %5d \n",time,dur);
167 dp
->pos
=demux
->filepos
;
178 /*****************************************************************
179 * \brief read the replicated data associated with each segment
180 * \parameter pp reference to replicated data
181 * \parameter id stream number
182 * \parameter seq media object number
183 * \parameter keyframe key frame indicator - set to zero if keyframe, non-zero otherwise
184 * \parameter seg_time set to payload time when valid, if audio or new video frame payload, zero otherwise
187 static void get_payload_extension_data(demuxer_t
*demux
, unsigned char** pp
, unsigned char id
, unsigned int seq
, int *keyframe
, uint64_t *seg_time
){
188 struct asf_priv
* asf
= demux
->priv
;
189 uint64_t payload_time
; //100ns units
190 int i
, ext_max
, ext_timing_index
;
193 if(demux
->video
->id
==-1)
194 if(demux
->v_streams
[id
])
197 if(demux
->audio
->id
==-1)
198 if(demux
->a_streams
[id
])
201 if (id
!=demux
->video
->id
&& id
!=demux
->audio
->id
) return;
203 if (id
==demux
->video
->id
) {
204 ext_max
= asf
->vid_repdata_count
;
205 ext_timing_index
= asf
->vid_ext_timing_index
;
207 ext_max
= asf
->aud_repdata_count
;
208 ext_timing_index
= asf
->aud_ext_timing_index
;
212 asf
->new_vid_frame_seg
= 0;
214 for (i
=0; i
<ext_max
; i
++) {
216 uint8_t segment_marker
;
218 if (id
==demux
->video
->id
)
219 payextsize
= asf
->vid_repdata_sizes
[i
];
221 payextsize
= asf
->aud_repdata_sizes
[i
];
223 if (payextsize
== 65535) {
224 payextsize
= AV_RL16(pi
);
228 // if this is the timing info extension then read the payload time
229 if (i
== ext_timing_index
)
230 payload_time
= AV_RL64(pi
+8);
232 // if this is the video frame info extension then
233 // set the keyframe indicator, the 'new frame segment' indicator
234 // and (initially) the 'frame time'
235 if (i
== asf
->vid_ext_frame_index
&& id
==demux
->video
->id
) {
236 segment_marker
= pi
[0];
237 // Known video stream segment_marker values that
238 // contain useful information:
240 // NTSC/ATSC (29.97fps): 0X4A 01001010
244 // PAL/ATSC (25fps): 0X3A 00111010
248 // ATSC progressive (29.97fps): 0X7A 01111010
252 // ^ this is new video frame marker
254 // ^^^^ these bits indicate the framerate
255 // 0X4 is 29.97i, 0X3 is 25i, 0X7 is 29.97p, ???=25p
257 // ^^^ these bits indicate the frame type:
259 // 010 and 011 probably mean P and B
261 asf
->new_vid_frame_seg
= (0X08 & segment_marker
) && seq
!= asf
->last_vid_seq
;
263 if (asf
->new_vid_frame_seg
) asf
->last_vid_seq
= seq
;
265 if (asf
->avg_vid_frame_time
== 0) {
266 // set the average frame time initially (in 100ns units).
267 // This is based on what works for known samples.
268 // It can be extended if more samples of different types can be obtained.
269 if (((segment_marker
& 0XF0) >> 4) == 4) {
270 asf
->avg_vid_frame_time
= (uint64_t)((1.001 / 30.0) * 10000000.0);
271 asf
->know_frame_time
=1;
272 } else if (((segment_marker
& 0XF0) >> 4) == 3) {
273 asf
->avg_vid_frame_time
= (uint64_t)(0.04 * 10000000.0);
274 asf
->know_frame_time
=1;
275 } else if (((segment_marker
& 0XF0) >> 4) == 6) {
276 asf
->avg_vid_frame_time
= (uint64_t)(0.02 * 10000000.0);
277 asf
->know_frame_time
=1;
278 } else if (((segment_marker
& 0XF0) >> 4) == 7) {
279 asf
->avg_vid_frame_time
= (uint64_t)((1.001 / 60.0) * 10000000.0);
280 asf
->know_frame_time
=1;
282 // we dont know the frame time initially so
283 // make a guess and then recalculate as we go.
284 asf
->avg_vid_frame_time
= (uint64_t)((1.001 / 60.0) * 10000000.0);
285 asf
->know_frame_time
=0;
288 *keyframe
= (asf
->new_vid_frame_seg
&& (segment_marker
& 0X07) == 1);
293 if (id
==demux
->video
->id
&& asf
->new_vid_frame_seg
) {
295 // Some samples only have timings on key frames and
296 // the rest contain non-cronological timestamps. Interpolating
297 // the values between key frames works for all samples.
299 asf
->found_first_key_frame
=1;
300 if (!asf
->know_frame_time
&& asf
->last_key_payload_time
> 0) {
301 // We dont know average frametime so recalculate.
302 // Giving precedence to the 'weight' of the existing
303 // average limits damage done to new value when there is
304 // a sudden time jump which happens occasionally.
305 asf
->avg_vid_frame_time
=
306 (0.9 * asf
->avg_vid_frame_time
) +
307 (0.1 * ((payload_time
- asf
->last_key_payload_time
) / asf
->vid_frame_ct
));
309 asf
->last_key_payload_time
= payload_time
;
310 asf
->vid_frame_ct
= 1;
311 *seg_time
= payload_time
;
313 *seg_time
= (asf
->last_key_payload_time
+ (asf
->avg_vid_frame_time
* (asf
->vid_frame_ct
-1)));
316 if (id
==demux
->audio
->id
) {
317 if (payload_time
!= -1)
318 asf
->last_aud_diff
= payload_time
- asf
->last_aud_pts
;
319 asf
->last_aud_pts
+= asf
->last_aud_diff
;
320 *seg_time
= asf
->last_aud_pts
;
323 //static int num_elementary_packets100=0;
324 //static int num_elementary_packets101=0;
327 // 0 = EOF or no stream found
328 // 1 = successfully read a packet
329 static int demux_asf_fill_buffer(demuxer_t
*demux
, demux_stream_t
*ds
){
330 struct asf_priv
* asf
= demux
->priv
;
332 demux
->filepos
=stream_tell(demux
->stream
);
333 // Brodcast stream have movi_start==movi_end
335 if((demux
->movi_start
< demux
->movi_end
) && (demux
->filepos
>=demux
->movi_end
)){
336 demux
->stream
->eof
=1;
340 stream_read(demux
->stream
,asf
->packet
,asf
->packetsize
);
341 if(demux
->stream
->eof
) return 0; // EOF
342 if(asf
->packetsize
< 2) return 0; // Packet too short
345 unsigned char* p
=asf
->packet
;
346 unsigned char* p_end
=asf
->packet
+asf
->packetsize
;
347 unsigned char flags
=p
[0];
348 unsigned char segtype
=p
[1];
352 unsigned long time
=0;
353 unsigned short duration
=0;
356 unsigned char segsizetype
=0x80;
359 if( mp_msg_test(MSGT_DEMUX
,MSGL_DBG2
) ){
361 for(i
=0;i
<FFMIN(16, asf
->packetsize
);i
++) printf(" %02X",asf
->packet
[i
]);
365 // skip ECC data if present by testing bit 7 of flags
366 // 1xxxbbbb -> ecc data present, skip bbbb byte(s)
367 // 0xxxxxxx -> payload parsing info starts
370 p
+= (flags
& 0x0f)+1;
371 if (p
+1 >= p_end
) return 0; // Packet too short
376 //if(segtype!=0x5d) printf("Warning! packet[4] != 0x5d \n");
378 p
+=2; // skip flags & segtype
380 // Read packet size (plen):
381 if(!check_varlen(p
, p_end
, flags
>> 5)) return 0; // Not enough data
382 plen
= read_varlen(&p
, flags
>> 5, 0);
385 if(!check_varlen(p
, p_end
, flags
>> 1)) return 0; // Not enough data
386 sequence
= read_varlen(&p
, flags
>> 1, 0);
388 // Read padding size (padding):
389 if(!check_varlen(p
, p_end
, flags
>> 3)) return 0; // Not enough data
390 padding
= read_varlen(&p
, flags
>> 3, 0);
392 if(((flags
>>5)&3)!=0){
393 // Explicit (absoulte) packet size
394 mp_dbg(MSGT_DEMUX
,MSGL_DBG2
,"Explicit packet size specified: %d \n",plen
);
395 if(plen
>asf
->packetsize
) mp_msg(MSGT_DEMUX
,MSGL_V
,"Warning! plen>packetsize! (%d>%d) \n",plen
,asf
->packetsize
);
397 // Padding (relative) size
398 plen
=asf
->packetsize
-padding
;
401 // Read time & duration:
402 if (p
+5 >= p_end
) return 0; // Packet too short
403 time
= AV_RL32(p
); p
+=4;
404 duration
= AV_RL16(p
); p
+=2;
406 // Read payload flags:
408 // multiple sub-packets
409 if (p
>= p_end
) return 0; // Packet too short
414 mp_dbg(MSGT_DEMUX
,MSGL_DBG4
,"%08"PRIu64
": flag=%02X segs=%d seq=%u plen=%u pad=%u time=%ld dur=%d\n",
415 (uint64_t)demux
->filepos
,flags
,segs
,sequence
,plen
,padding
,time
,duration
);
417 for(seg
=0;seg
<segs
;seg
++){
419 unsigned char streamno
;
421 unsigned int x
; // offset or timestamp
429 mp_msg(MSGT_DEMUX
,MSGL_V
,"Warning! invalid packet 1, aborting parsing...\n");
433 if( mp_msg_test(MSGT_DEMUX
,MSGL_DBG2
) ){
435 printf("seg %d:",seg
);
436 for(i
=0;i
<FFMIN(16, p_end
- p
);i
++) printf(" %02X",p
[i
]);
441 if(p
[0]&0x80) keyframe
=1;
444 // Read media object number (seq):
445 if(!check_varlen(p
, p_end
, segtype
>> 4)) break; // Not enough data
446 seq
= read_varlen(&p
, segtype
>> 4, 0);
448 // Read offset or timestamp:
449 if(!check_varlen(p
, p_end
, segtype
>> 2)) break; // Not enough data
450 x
= read_varlen(&p
, segtype
>> 2, 0);
452 // Read replic.data len:
453 if(!check_varlen(p
, p_end
, segtype
)) break; // Not enough data
454 rlen
= read_varlen(&p
, segtype
, 0);
456 // printf("### rlen=%d \n",rlen);
459 case 0x01: // 1 = special, means grouping
460 //printf("grouping: %02X \n",p[0]);
461 ++p
; // skip PTS delta
465 p
+=4; // skip object size
466 if (p
+3 >= p_end
) break; // Packet too short
467 time2
=AV_RL32(p
); // read PTS
468 if (asf
->asf_is_dvr_ms
)
469 get_payload_extension_data(demux
, &p
, streamno
, seq
, &keyframe
, &time2
);
472 mp_msg(MSGT_DEMUX
,MSGL_V
,"unknown segment type (rlen): 0x%02X \n",rlen
);
480 if(!check_varlen(p
, p_end
, segsizetype
)) break; // Not enough data
481 len
= read_varlen(&p
, segsizetype
, plen
-(p
-asf
->packet
));
484 len
=plen
-(p
-asf
->packet
);
486 if(len
<0 || (p
+len
)>p_end
){
487 mp_msg(MSGT_DEMUX
,MSGL_V
,"ASF_parser: warning! segment len=%d\n",len
);
490 mp_dbg(MSGT_DEMUX
,MSGL_DBG4
," seg #%d: streamno=%d seq=%d type=%02X len=%d\n",seg
,streamno
,seq
,rlen
,len
);
495 //printf("ASF_parser: warning! grouping (flag=1) not yet supported!\n",len);
496 //printf(" total: %d \n",len);
500 //printf(" group part: %d bytes\n",len2);
501 if(len2
> len
- 1 || len2
< 0) break; // Not enough data
502 len2
= FFMIN(len2
, asf
->packetsize
);
503 demux_asf_read_packet(demux
,p
,len2
,streamno
,seq
,x
,duration
,-1,keyframe
);
509 mp_msg(MSGT_DEMUX
,MSGL_V
,"ASF_parser: warning! groups total != len\n");
514 //printf("fragment offset: %d \n",sh->x);
516 if (!asf
->asf_is_dvr_ms
|| asf
->found_first_key_frame
) {
517 len
= FFMIN(len
, asf
->packetsize
);
518 demux_asf_read_packet(demux
,p
,len
,streamno
,seq
,time2
,duration
,x
,keyframe
);
528 mp_msg(MSGT_DEMUX
,MSGL_V
,"%08"PRIX64
": UNKNOWN TYPE %02X %02X %02X %02X %02X...\n",(int64_t)demux
->filepos
,asf
->packet
[0],asf
->packet
[1],asf
->packet
[2],asf
->packet
[3],asf
->packet
[4]);
532 #include "stheader.h"
534 void skip_audio_frame(sh_audio_t
*sh_audio
);
536 static void demux_seek_asf(demuxer_t
*demuxer
,float rel_seek_secs
,float audio_delay
,int flags
){
537 struct asf_priv
* asf
= demuxer
->priv
;
538 demux_stream_t
*d_audio
=demuxer
->audio
;
539 demux_stream_t
*d_video
=demuxer
->video
;
540 sh_audio_t
*sh_audio
=d_audio
->sh
;
541 // sh_video_t *sh_video=d_video->sh;
543 //FIXME: OFF_T - didn't test ASF case yet (don't have a large asf...)
544 //FIXME: reports good or bad to steve@daviesfam.org please
546 //================= seek in ASF ==========================
547 float p_rate
=asf
->packetrate
; // packets / sec
548 off_t rel_seek_packs
=(flags
&SEEK_FACTOR
)? // FIXME: int may be enough?
549 (rel_seek_secs
*(demuxer
->movi_end
-demuxer
->movi_start
)/asf
->packetsize
):
550 (rel_seek_secs
*p_rate
);
551 off_t rel_seek_bytes
=rel_seek_packs
*asf
->packetsize
;
553 //printf("ASF: packs: %d duration: %d \n",(int)fileh.packets,*((int*)&fileh.duration));
554 // printf("ASF_seek: %d secs -> %d packs -> %d bytes \n",
555 // rel_seek_secs,rel_seek_packs,rel_seek_bytes);
556 newpos
=((flags
&SEEK_ABSOLUTE
)?demuxer
->movi_start
:demuxer
->filepos
)+rel_seek_bytes
;
557 if(newpos
<0 || newpos
<demuxer
->movi_start
) newpos
=demuxer
->movi_start
;
558 // printf("\r -- asf: newpos=%d -- \n",newpos);
559 stream_seek(demuxer
->stream
,newpos
);
561 if (asf
->asf_is_dvr_ms
) asf
->dvr_last_vid_pts
= 0.0f
;
563 if (d_video
->id
>= 0)
564 ds_fill_buffer(d_video
);
566 ds_fill_buffer(d_audio
);
569 if (d_video
->id
>= 0)
571 if(sh_audio
&& !d_audio
->eof
){
572 float a_pts
=d_audio
->pts
;
573 a_pts
+=(ds_tell_pts(d_audio
)-sh_audio
->a_in_buffer_len
)/(float)sh_audio
->i_bps
;
575 if (d_video
->pts
> a_pts
){
576 skip_audio_frame(sh_audio
);
577 // if(!ds_fill_buffer(d_audio)) sh_audio=NULL; // skip audio. EOF?
581 if(d_video
->flags
&1) break; // found a keyframe!
582 if(!ds_fill_buffer(d_video
)) break; // skip frame. EOF?
588 static int demux_asf_control(demuxer_t
*demuxer
,int cmd
, void *arg
){
589 struct asf_priv
* asf
= demuxer
->priv
;
590 /* demux_stream_t *d_audio=demuxer->audio;
591 demux_stream_t *d_video=demuxer->video;
592 sh_audio_t *sh_audio=d_audio->sh;
593 sh_video_t *sh_video=d_video->sh;
596 case DEMUXER_CTRL_GET_TIME_LENGTH
:
597 *((double *)arg
)=(double)(asf
->movielength
);
598 return DEMUXER_CTRL_OK
;
600 case DEMUXER_CTRL_GET_PERCENT_POS
:
601 return DEMUXER_CTRL_DONTKNOW
;
604 return DEMUXER_CTRL_NOTIMPL
;
609 static demuxer_t
* demux_open_asf(demuxer_t
* demuxer
)
611 struct asf_priv
* asf
= demuxer
->priv
;
612 sh_audio_t
*sh_audio
=NULL
;
613 sh_video_t
*sh_video
=NULL
;
616 if(!asf
) return NULL
;
618 if (!read_asf_header(demuxer
,asf
))
620 stream_reset(demuxer
->stream
);
621 stream_seek(demuxer
->stream
,demuxer
->movi_start
);
622 // demuxer->idx_pos=0;
623 // demuxer->endpos=avi_header.movi_end;
624 if(demuxer
->video
->id
!= -2) {
625 if(!ds_fill_buffer(demuxer
->video
)){
626 mp_msg(MSGT_DEMUXER
,MSGL_WARN
,"ASF: " MSGTR_MissingVideoStream
);
627 demuxer
->video
->sh
=NULL
;
628 //printf("ASF: missing video stream!? contact the author, it may be a bug :(\n");
630 sh_video
=demuxer
->video
->sh
;sh_video
->ds
=demuxer
->video
;
631 sh_video
->fps
=1000.0f
; sh_video
->frametime
=0.001f
;
633 if (asf
->asf_is_dvr_ms
) {
634 sh_video
->bih
->biWidth
= 0;
635 sh_video
->bih
->biHeight
= 0;
640 if(demuxer
->audio
->id
!=-2){
641 mp_msg(MSGT_DEMUXER
,MSGL_V
,MSGTR_ASFSearchingForAudioStream
,demuxer
->audio
->id
);
642 if(!ds_fill_buffer(demuxer
->audio
)){
643 mp_msg(MSGT_DEMUXER
,MSGL_INFO
,"ASF: " MSGTR_MissingAudioStream
);
644 demuxer
->audio
->sh
=NULL
;
646 sh_audio
=demuxer
->audio
->sh
;sh_audio
->ds
=demuxer
->audio
;
647 sh_audio
->format
=sh_audio
->wf
->wFormatTag
;
650 if(!demuxer
->stream
->seek
)
657 static void demux_close_asf(demuxer_t
*demuxer
) {
658 struct asf_priv
* asf
= demuxer
->priv
;
662 if (asf
->aud_repdata_sizes
)
663 free(asf
->aud_repdata_sizes
);
665 if (asf
->vid_repdata_sizes
)
666 free(asf
->vid_repdata_sizes
);
671 const demuxer_desc_t demuxer_desc_asf
= {
678 1, // safe autodetect
680 demux_asf_fill_buffer
,