1 // ASF file parser for DEMUXER v0.3 by A'rpi/ESP-team
11 #include "stream/stream.h"
15 #include "libvo/fastmemcpy.h"
16 #include "libavutil/intreadwrite.h"
18 // defined at asfheader.c:
20 extern int asf_check_header(demuxer_t
*demuxer
);
21 extern int read_asf_header(demuxer_t
*demuxer
,struct asf_priv
* asf
);
23 // based on asf file-format doc by Eugene [http://divx.euro.ru]
26 * \brief reads int stored in number of bytes given by len
27 * \param ptr pointer to read from, is incremented appropriately
28 * \param len lowest 2 bits indicate number of bytes to read
29 * \param def default value to return if len is invalid
31 static inline unsigned read_varlen(uint8_t **ptr
, int len
, int def
) {
32 const uint8_t *p
= *ptr
;
35 case 1: *ptr
+= 1; return *p
;
36 case 2: *ptr
+= 2; return AV_RL16(p
);
37 case 3: *ptr
+= 4; return AV_RL32(p
);
42 static void asf_descrambling(unsigned char **src
,unsigned len
, struct asf_priv
* asf
){
43 unsigned char *dst
=malloc(len
);
44 unsigned char *s2
=*src
;
46 while(len
>=asf
->scrambling_h
*asf
->scrambling_w
*asf
->scrambling_b
+i
){
47 // mp_msg(MSGT_DEMUX,MSGL_DBG4,"descrambling! (w=%d b=%d)\n",w,asf_scrambling_b);
48 //i+=asf_scrambling_h*asf_scrambling_w;
49 for(x
=0;x
<asf
->scrambling_w
;x
++)
50 for(y
=0;y
<asf
->scrambling_h
;y
++){
51 fast_memcpy(dst
+i
,s2
+(y
*asf
->scrambling_w
+x
)*asf
->scrambling_b
,asf
->scrambling_b
);
54 s2
+=asf
->scrambling_h
*asf
->scrambling_w
*asf
->scrambling_b
;
56 //if(i<len) fast_memcpy(dst+i,src+i,len-i);
61 /*****************************************************************
62 * \brief initializes asf private data
65 static void init_priv (struct asf_priv
* asf
){
67 asf
->vid_ext_timing_index
=-1;
68 asf
->aud_ext_timing_index
=-1;
69 asf
->vid_ext_frame_index
=-1;
72 #ifdef USE_LIBAVCODEC_SO
73 #include <ffmpeg/avcodec.h>
74 #elif defined(USE_LIBAVCODEC)
75 #include "libavcodec/avcodec.h"
77 #define FF_INPUT_BUFFER_PADDING_SIZE 8
81 static void demux_asf_append_to_packet(demux_packet_t
* dp
,unsigned char *data
,int len
,int offs
)
83 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
);
84 dp
->buffer
=realloc(dp
->buffer
,dp
->len
+len
+FF_INPUT_BUFFER_PADDING_SIZE
);
85 fast_memcpy(dp
->buffer
+dp
->len
,data
,len
);
86 memset(dp
->buffer
+dp
->len
+len
, 0, FF_INPUT_BUFFER_PADDING_SIZE
);
87 mp_dbg(MSGT_DEMUX
,MSGL_DBG4
,"data appended! %d+%d\n",dp
->len
,len
);
91 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
){
92 struct asf_priv
* asf
= demux
->priv
;
93 demux_stream_t
*ds
=NULL
;
96 mp_dbg(MSGT_DEMUX
,MSGL_DBG4
,"demux_asf.read_packet: id=%d seq=%d len=%d\n",id
,seq
,len
);
98 if(demux
->video
->id
==-1)
99 if(demux
->v_streams
[id
])
102 if(demux
->audio
->id
==-1)
103 if(demux
->a_streams
[id
])
106 if(id
==demux
->audio
->id
){
110 ds
->sh
=demux
->a_streams
[id
];
111 mp_msg(MSGT_DEMUX
,MSGL_V
,"Auto-selected ASF audio ID = %d\n",ds
->id
);
114 if(id
==demux
->video
->id
){
118 ds
->sh
=demux
->v_streams
[id
];
119 mp_msg(MSGT_DEMUX
,MSGL_V
,"Auto-selected ASF video ID = %d\n",ds
->id
);
125 demux_packet_t
* dp
=ds
->asf_packet
;
127 if (ds
==demux
->video
&& asf
->asf_is_dvr_ms
) {
128 if (asf
->new_vid_frame_seg
) {
129 dp
->pos
=demux
->filepos
;
131 } else seq
= ds
->asf_seq
;
132 } else close_seg
= ds
->asf_seq
!=seq
;
135 // closed segment, finalize packet:
137 if(asf
->scrambling_h
>1 && asf
->scrambling_w
>1 && asf
->scrambling_b
>0)
138 asf_descrambling(&ds
->asf_packet
->buffer
,ds
->asf_packet
->len
,asf
);
139 ds_add_packet(ds
,ds
->asf_packet
);
142 // append data to it!
143 demux_asf_append_to_packet(dp
,data
,len
,offs
);
148 // create new packet:
149 { demux_packet_t
* dp
;
151 mp_msg(MSGT_DEMUX
,MSGL_V
,"warning! broken fragment, %d bytes missing \n",offs
);
154 dp
=new_demux_packet(len
);
155 fast_memcpy(dp
->buffer
,data
,len
);
156 if (asf
->asf_is_dvr_ms
)
157 dp
->pts
=time
*0.0000001;
161 // if(ds==demux->video) printf("ASF time: %8d dur: %5d \n",time,dur);
162 dp
->pos
=demux
->filepos
;
173 /*****************************************************************
174 * \brief read the replicated data associated with each segment
175 * \parameter pp reference to replicated data
176 * \parameter id stream number
177 * \parameter seq media object number
178 * \parameter keyframe key frame indicator - set to zero if keyframe, non-zero otherwise
179 * \parameter seg_time set to payload time when valid, if audio or new video frame payload, zero otherwise
182 static void get_payload_extension_data(demuxer_t
*demux
, unsigned char** pp
, unsigned char id
, unsigned int seq
, int *keyframe
, uint64_t *seg_time
){
183 struct asf_priv
* asf
= demux
->priv
;
184 uint64_t payload_time
; //100ns units
185 int i
, ext_max
, ext_timing_index
;
188 if(demux
->video
->id
==-1)
189 if(demux
->v_streams
[id
])
192 if(demux
->audio
->id
==-1)
193 if(demux
->a_streams
[id
])
196 if (id
!=demux
->video
->id
&& id
!=demux
->audio
->id
) return;
198 if (id
==demux
->video
->id
) {
199 ext_max
= asf
->vid_repdata_count
;
200 ext_timing_index
= asf
->vid_ext_timing_index
;
202 ext_max
= asf
->aud_repdata_count
;
203 ext_timing_index
= asf
->aud_ext_timing_index
;
207 asf
->new_vid_frame_seg
= 0;
209 for (i
=0; i
<ext_max
; i
++) {
211 uint8_t segment_marker
;
213 if (id
==demux
->video
->id
)
214 payextsize
= asf
->vid_repdata_sizes
[i
];
216 payextsize
= asf
->aud_repdata_sizes
[i
];
218 if (payextsize
== 65535) {
219 payextsize
= AV_RL16(pi
);
223 // if this is the timing info extension then read the payload time
224 if (i
== ext_timing_index
)
225 payload_time
= AV_RL64(pi
+8);
227 // if this is the video frame info extension then
228 // set the keyframe indicator, the 'new frame segment' indicator
229 // and (initially) the 'frame time'
230 if (i
== asf
->vid_ext_frame_index
&& id
==demux
->video
->id
) {
231 segment_marker
= pi
[0];
232 // Known video stream segment_marker values that
233 // contain useful information:
235 // NTSC/ATSC (29.97fps): 0X4A 01001010
239 // PAL/ATSC (25fps): 0X3A 00111010
243 // ATSC progressive (29.97fps): 0X7A 01111010
247 // ^ this is new video frame marker
249 // ^^^^ these bits indicate the framerate
250 // 0X4 is 29.97i, 0X3 is 25i, 0X7 is 29.97p, ???=25p
252 // ^^^ these bits indicate the frame type:
254 // 010 and 011 probably mean P and B
256 asf
->new_vid_frame_seg
= (0X08 & segment_marker
) && seq
!= asf
->last_vid_seq
;
258 if (asf
->new_vid_frame_seg
) asf
->last_vid_seq
= seq
;
260 if (asf
->avg_vid_frame_time
== 0) {
261 // set the average frame time initially (in 100ns units).
262 // This is based on what works for known samples.
263 // It can be extended if more samples of different types can be obtained.
264 if (((segment_marker
& 0XF0) >> 4) == 4) {
265 asf
->avg_vid_frame_time
= (uint64_t)((1.001 / 30.0) * 10000000.0);
266 asf
->know_frame_time
=1;
267 } else if (((segment_marker
& 0XF0) >> 4) == 3) {
268 asf
->avg_vid_frame_time
= (uint64_t)(0.04 * 10000000.0);
269 asf
->know_frame_time
=1;
270 } else if (((segment_marker
& 0XF0) >> 4) == 6) {
271 asf
->avg_vid_frame_time
= (uint64_t)(0.02 * 10000000.0);
272 asf
->know_frame_time
=1;
273 } else if (((segment_marker
& 0XF0) >> 4) == 7) {
274 asf
->avg_vid_frame_time
= (uint64_t)((1.001 / 60.0) * 10000000.0);
275 asf
->know_frame_time
=1;
277 // we dont know the frame time initially so
278 // make a guess and then recalculate as we go.
279 asf
->avg_vid_frame_time
= (uint64_t)((1.001 / 60.0) * 10000000.0);
280 asf
->know_frame_time
=0;
283 *keyframe
= (asf
->new_vid_frame_seg
&& (segment_marker
& 0X07) == 1);
288 if (id
==demux
->video
->id
&& asf
->new_vid_frame_seg
) {
290 // Some samples only have timings on key frames and
291 // the rest contain non-cronological timestamps. Interpolating
292 // the values between key frames works for all samples.
294 asf
->found_first_key_frame
=1;
295 if (!asf
->know_frame_time
&& asf
->last_key_payload_time
> 0) {
296 // We dont know average frametime so recalculate.
297 // Giving precedence to the 'weight' of the existing
298 // average limits damage done to new value when there is
299 // a sudden time jump which happens occasionally.
300 asf
->avg_vid_frame_time
=
301 (0.9 * asf
->avg_vid_frame_time
) +
302 (0.1 * ((payload_time
- asf
->last_key_payload_time
) / asf
->vid_frame_ct
));
304 asf
->last_key_payload_time
= payload_time
;
305 asf
->vid_frame_ct
= 1;
306 *seg_time
= payload_time
;
308 *seg_time
= (asf
->last_key_payload_time
+ (asf
->avg_vid_frame_time
* (asf
->vid_frame_ct
-1)));
311 if (id
==demux
->audio
->id
) {
312 if (payload_time
!= -1)
313 asf
->last_aud_diff
= payload_time
- asf
->last_aud_pts
;
314 asf
->last_aud_pts
+= asf
->last_aud_diff
;
315 *seg_time
= asf
->last_aud_pts
;
318 //static int num_elementary_packets100=0;
319 //static int num_elementary_packets101=0;
322 // 0 = EOF or no stream found
323 // 1 = successfully read a packet
324 static int demux_asf_fill_buffer(demuxer_t
*demux
, demux_stream_t
*ds
){
325 struct asf_priv
* asf
= demux
->priv
;
327 demux
->filepos
=stream_tell(demux
->stream
);
328 // Brodcast stream have movi_start==movi_end
330 if((demux
->movi_start
< demux
->movi_end
) && (demux
->filepos
>=demux
->movi_end
)){
331 demux
->stream
->eof
=1;
335 stream_read(demux
->stream
,asf
->packet
,asf
->packetsize
);
336 if(demux
->stream
->eof
) return 0; // EOF
339 unsigned char* p
=asf
->packet
;
340 unsigned char* p_end
=asf
->packet
+asf
->packetsize
;
341 unsigned char flags
=p
[0];
342 unsigned char segtype
=p
[1];
346 unsigned long time
=0;
347 unsigned short duration
=0;
350 unsigned char segsizetype
=0x80;
353 if( mp_msg_test(MSGT_DEMUX
,MSGL_DBG2
) ){
355 for(i
=0;i
<16;i
++) printf(" %02X",asf
->packet
[i
]);
359 // skip ECC data if present by testing bit 7 of flags
360 // 1xxxbbbb -> ecc data present, skip bbbb byte(s)
361 // 0xxxxxxx -> payload parsing info starts
364 p
+= (flags
& 0x0f)+1;
369 //if(segtype!=0x5d) printf("Warning! packet[4] != 0x5d \n");
371 p
+=2; // skip flags & segtype
373 // Read packet size (plen):
374 plen
= read_varlen(&p
, flags
>> 5, 0);
377 sequence
= read_varlen(&p
, flags
>> 1, 0);
379 // Read padding size (padding):
380 padding
= read_varlen(&p
, flags
>> 3, 0);
382 if(((flags
>>5)&3)!=0){
383 // Explicit (absoulte) packet size
384 mp_dbg(MSGT_DEMUX
,MSGL_DBG2
,"Explicit packet size specified: %d \n",plen
);
385 if(plen
>asf
->packetsize
) mp_msg(MSGT_DEMUX
,MSGL_V
,"Warning! plen>packetsize! (%d>%d) \n",plen
,asf
->packetsize
);
387 // Padding (relative) size
388 plen
=asf
->packetsize
-padding
;
391 // Read time & duration:
392 time
= AV_RL32(p
); p
+=4;
393 duration
= AV_RL16(p
); p
+=2;
395 // Read payload flags:
397 // multiple sub-packets
402 mp_dbg(MSGT_DEMUX
,MSGL_DBG4
,"%08"PRIu64
": flag=%02X segs=%d seq=%u plen=%u pad=%u time=%ld dur=%d\n",
403 (uint64_t)demux
->filepos
,flags
,segs
,sequence
,plen
,padding
,time
,duration
);
405 for(seg
=0;seg
<segs
;seg
++){
407 unsigned char streamno
;
409 unsigned int x
; // offset or timestamp
417 mp_msg(MSGT_DEMUX
,MSGL_V
,"Warning! invalid packet 1, aborting parsing...\n");
421 if( mp_msg_test(MSGT_DEMUX
,MSGL_DBG2
) ){
423 printf("seg %d:",seg
);
424 for(i
=0;i
<16;i
++) printf(" %02X",p
[i
]);
429 if(p
[0]&0x80) keyframe
=1;
432 // Read media object number (seq):
433 seq
= read_varlen(&p
, segtype
>> 4, 0);
435 // Read offset or timestamp:
436 x
= read_varlen(&p
, segtype
>> 2, 0);
438 // Read replic.data len:
439 rlen
= read_varlen(&p
, segtype
, 0);
441 // printf("### rlen=%d \n",rlen);
444 case 0x01: // 1 = special, means grouping
445 //printf("grouping: %02X \n",p[0]);
446 ++p
; // skip PTS delta
450 p
+=4; // skip object size
451 time2
=AV_RL32(p
); // read PTS
452 if (asf
->asf_is_dvr_ms
)
453 get_payload_extension_data(demux
, &p
, streamno
, seq
, &keyframe
, &time2
);
456 mp_msg(MSGT_DEMUX
,MSGL_V
,"unknown segment type (rlen): 0x%02X \n",rlen
);
464 len
= read_varlen(&p
, segsizetype
, plen
-(p
-asf
->packet
));
467 len
=plen
-(p
-asf
->packet
);
469 if(len
<0 || (p
+len
)>p_end
){
470 mp_msg(MSGT_DEMUX
,MSGL_V
,"ASF_parser: warning! segment len=%d\n",len
);
472 mp_dbg(MSGT_DEMUX
,MSGL_DBG4
," seg #%d: streamno=%d seq=%d type=%02X len=%d\n",seg
,streamno
,seq
,rlen
,len
);
477 //printf("ASF_parser: warning! grouping (flag=1) not yet supported!\n",len);
478 //printf(" total: %d \n",len);
482 //printf(" group part: %d bytes\n",len2);
483 demux_asf_read_packet(demux
,p
,len2
,streamno
,seq
,x
,duration
,-1,keyframe
);
489 mp_msg(MSGT_DEMUX
,MSGL_V
,"ASF_parser: warning! groups total != len\n");
494 //printf("fragment offset: %d \n",sh->x);
495 if (!asf
->asf_is_dvr_ms
|| asf
->found_first_key_frame
)
496 demux_asf_read_packet(demux
,p
,len
,streamno
,seq
,time2
,duration
,x
,keyframe
);
505 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]);
509 #include "stheader.h"
511 extern void skip_audio_frame(sh_audio_t
*sh_audio
);
513 static void demux_seek_asf(demuxer_t
*demuxer
,float rel_seek_secs
,float audio_delay
,int flags
){
514 struct asf_priv
* asf
= demuxer
->priv
;
515 demux_stream_t
*d_audio
=demuxer
->audio
;
516 demux_stream_t
*d_video
=demuxer
->video
;
517 sh_audio_t
*sh_audio
=d_audio
->sh
;
518 // sh_video_t *sh_video=d_video->sh;
520 //FIXME: OFF_T - didn't test ASF case yet (don't have a large asf...)
521 //FIXME: reports good or bad to steve@daviesfam.org please
523 //================= seek in ASF ==========================
524 float p_rate
=asf
->packetrate
; // packets / sec
525 off_t rel_seek_packs
=(flags
&2)? // FIXME: int may be enough?
526 (rel_seek_secs
*(demuxer
->movi_end
-demuxer
->movi_start
)/asf
->packetsize
):
527 (rel_seek_secs
*p_rate
);
528 off_t rel_seek_bytes
=rel_seek_packs
*asf
->packetsize
;
530 //printf("ASF: packs: %d duration: %d \n",(int)fileh.packets,*((int*)&fileh.duration));
531 // printf("ASF_seek: %d secs -> %d packs -> %d bytes \n",
532 // rel_seek_secs,rel_seek_packs,rel_seek_bytes);
533 newpos
=((flags
&1)?demuxer
->movi_start
:demuxer
->filepos
)+rel_seek_bytes
;
534 if(newpos
<0 || newpos
<demuxer
->movi_start
) newpos
=demuxer
->movi_start
;
535 // printf("\r -- asf: newpos=%d -- \n",newpos);
536 stream_seek(demuxer
->stream
,newpos
);
538 if (asf
->asf_is_dvr_ms
) asf
->dvr_last_vid_pts
= 0.0f
;
540 if (d_video
->id
>= 0)
541 ds_fill_buffer(d_video
);
543 ds_fill_buffer(d_audio
);
546 if (d_video
->id
>= 0)
548 if(sh_audio
&& !d_audio
->eof
){
549 float a_pts
=d_audio
->pts
;
550 a_pts
+=(ds_tell_pts(d_audio
)-sh_audio
->a_in_buffer_len
)/(float)sh_audio
->i_bps
;
552 if (d_video
->pts
> a_pts
){
553 skip_audio_frame(sh_audio
);
554 // if(!ds_fill_buffer(d_audio)) sh_audio=NULL; // skip audio. EOF?
558 if(d_video
->flags
&1) break; // found a keyframe!
559 if(!ds_fill_buffer(d_video
)) break; // skip frame. EOF?
565 static int demux_asf_control(demuxer_t
*demuxer
,int cmd
, void *arg
){
566 struct asf_priv
* asf
= demuxer
->priv
;
567 /* demux_stream_t *d_audio=demuxer->audio;
568 demux_stream_t *d_video=demuxer->video;
569 sh_audio_t *sh_audio=d_audio->sh;
570 sh_video_t *sh_video=d_video->sh;
573 case DEMUXER_CTRL_GET_TIME_LENGTH
:
574 *((double *)arg
)=(double)(asf
->movielength
);
575 return DEMUXER_CTRL_OK
;
577 case DEMUXER_CTRL_GET_PERCENT_POS
:
578 return DEMUXER_CTRL_DONTKNOW
;
581 return DEMUXER_CTRL_NOTIMPL
;
586 static demuxer_t
* demux_open_asf(demuxer_t
* demuxer
)
588 struct asf_priv
* asf
= demuxer
->priv
;
589 sh_audio_t
*sh_audio
=NULL
;
590 sh_video_t
*sh_video
=NULL
;
593 if(!asf
) return NULL
;
595 if (!read_asf_header(demuxer
,asf
))
597 stream_reset(demuxer
->stream
);
598 stream_seek(demuxer
->stream
,demuxer
->movi_start
);
599 // demuxer->idx_pos=0;
600 // demuxer->endpos=avi_header.movi_end;
601 if(demuxer
->video
->id
!= -2) {
602 if(!ds_fill_buffer(demuxer
->video
)){
603 mp_msg(MSGT_DEMUXER
,MSGL_WARN
,"ASF: " MSGTR_MissingVideoStream
);
604 demuxer
->video
->sh
=NULL
;
605 //printf("ASF: missing video stream!? contact the author, it may be a bug :(\n");
607 sh_video
=demuxer
->video
->sh
;sh_video
->ds
=demuxer
->video
;
608 sh_video
->fps
=1000.0f
; sh_video
->frametime
=0.001f
;
610 if (asf
->asf_is_dvr_ms
) {
611 sh_video
->bih
->biWidth
= 0;
612 sh_video
->bih
->biHeight
= 0;
617 if(demuxer
->audio
->id
!=-2){
618 mp_msg(MSGT_DEMUXER
,MSGL_V
,MSGTR_ASFSearchingForAudioStream
,demuxer
->audio
->id
);
619 if(!ds_fill_buffer(demuxer
->audio
)){
620 mp_msg(MSGT_DEMUXER
,MSGL_INFO
,"ASF: " MSGTR_MissingAudioStream
);
621 demuxer
->audio
->sh
=NULL
;
623 sh_audio
=demuxer
->audio
->sh
;sh_audio
->ds
=demuxer
->audio
;
624 sh_audio
->format
=sh_audio
->wf
->wFormatTag
;
627 if(!demuxer
->stream
->seek
)
634 static void demux_close_asf(demuxer_t
*demuxer
) {
635 struct asf_priv
* asf
= demuxer
->priv
;
639 if (asf
->aud_repdata_sizes
)
640 free(asf
->aud_repdata_sizes
);
642 if (asf
->vid_repdata_sizes
)
643 free(asf
->vid_repdata_sizes
);
648 demuxer_desc_t demuxer_desc_asf
= {
655 1, // safe autodetect
657 demux_asf_fill_buffer
,