summaryrefslogtreecommitdiff
path: root/src/demuxers/demux_asf.c
blob: 8fe1c021dfb46a6ed17937a2df9bbb708fdf56ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
/*
 * Copyright (C) 2000, 2001 the xine project
 * 
 * This file is part of xine, a unix video player.
 * 
 * xine is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * xine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
 *
 * $Id: demux_asf.c,v 1.15 2001/11/26 13:29:54 miguelfreitas Exp $
 *
 * demultiplexer for asf streams
 *
 * based on ffmpeg's 
 * ASF compatible encoder and decoder.
 * Copyright (c) 2000, 2001 Gerard Lantau.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>

#include "xine_internal.h"
#include "demux.h"
#include "xineutils.h"

#define	WINE_TYPEDEFS_ONLY
#include "libw32dll/wine/avifmt.h"
#include "libw32dll/wine/windef.h"
#include "libw32dll/wine/vfw.h"
#include "libw32dll/wine/mmreg.h"

#define PACKET_SIZE        3200
#define PACKET_HEADER_SIZE   12
#define FRAME_HEADER_SIZE    17
#define CODEC_TYPE_AUDIO      0
#define CODEC_TYPE_VIDEO      1
#define MAX_NUM_STREAMS      23

#define DEFRAG_BUFSIZE    65536

typedef struct {
  int               num;
  int               seq;

  int               frag_offset;
  int               timestamp;
  int               ts_per_kbyte;
  int               defrag;
    
  uint32_t          buf_type;
  int               stream_id;
  fifo_buffer_t    *fifo;
  
  uint8_t          *buffer;
} asf_stream_t;

typedef struct demux_asf_s {
  demux_plugin_t    demux_plugin;

  fifo_buffer_t    *audio_fifo;
  fifo_buffer_t    *video_fifo;

  input_plugin_t   *input;

  int               keyframe_found;

  int               seqno;
  int               packet_size;
  int               packet_flags;
  
  asf_stream_t      streams[MAX_NUM_STREAMS];
  int               num_streams;
  int               num_audio_streams;
  int               num_video_streams;

  uint16_t          wavex[128];
  int               wavex_size;

  uint16_t          bih[128];
  int               bih_size;

  char              title[512];
  char              author[512];
  char              copyright[512];
  char              comment[512];

  uint32_t          length, rate;

  /* packet filling */
  int               packet_size_left;
  
  /* only for reading */
  int               packet_padsize;
  int               nb_frames;
  int               segtype;
  int               frame;
  
  pthread_t         thread;

  int               status;

  int               send_end_buffers;
  
  /* byte reordering from audio streams */
  int               reorder_h;
  int               reorder_w;
  int               reorder_b;

} demux_asf_t ;


typedef struct {
  uint32_t v1;
  uint16_t v2;
  uint16_t v3;
  uint8_t  v4[8];
} GUID;

static const GUID asf_header = {
  0x75B22630, 0x668E, 0x11CF, { 0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C },
};

static const GUID file_header = {
  0x8CABDCA1, 0xA947, 0x11CF, { 0x8E, 0xE4, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65 },
};

static const GUID stream_header = {
  0xB7DC0791, 0xA9B7, 0x11CF, { 0x8E, 0xE6, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65 },
};

static const GUID audio_stream = {
  0xF8699E40, 0x5B4D, 0x11CF, { 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B },
};

static const GUID audio_conceal_none = {
  0x49f1a440, 0x4ece, 0x11d0, { 0xa3, 0xac, 0x00, 0xa0, 0xc9, 0x03, 0x48, 0xf6 },
};

static const GUID audio_conceal_interleave = { 
  0xbfc3cd50, 0x618f, 0x11cf, {0x8b, 0xb2, 0x00, 0xaa, 0x00, 0xb4, 0xe2, 0x20} };

static const GUID video_stream = {
  0xBC19EFC0, 0x5B4D, 0x11CF, { 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B },
};

static const GUID video_conceal_none = {
  0x20FB5700, 0x5B55, 0x11CF, { 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B },
};


static const GUID comment_header = {
  0x75b22633, 0x668e, 0x11cf, { 0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c },
};

static const GUID codec_comment_header = {
  0x86D15240, 0x311D, 0x11D0, { 0xA3, 0xA4, 0x00, 0xA0, 0xC9, 0x03, 0x48, 0xF6 },
};
static const GUID codec_comment1_header = {
  0x86d15241, 0x311d, 0x11d0, { 0xa3, 0xa4, 0x00, 0xa0, 0xc9, 0x03, 0x48, 0xf6 },
};

static const GUID data_header = {
  0x75b22636, 0x668e, 0x11cf, { 0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c },
};

static const GUID index_guid = {
  0x33000890, 0xe5b1, 0x11cf, { 0x89, 0xf4, 0x00, 0xa0, 0xc9, 0x03, 0x49, 0xcb },
};

static const GUID head1_guid = {
  0x5fbf03b5, 0xa92e, 0x11cf, { 0x8e, 0xe3, 0x00, 0xc0, 0x0c, 0x20, 0x53, 0x65 },
};

static const GUID head2_guid = {
  0xabd3d211, 0xa9ba, 0x11cf, { 0x8e, 0xe6, 0x00, 0xc0, 0x0c, 0x20, 0x53, 0x65 },
};
    
/* I am not a number !!! This GUID is the one found on the PC used to
   generate the stream */
static const GUID my_guid = {
  0, 0, 0, { 0, 0, 0, 0, 0, 0, 0, 0 },
};

static uint8_t get_byte (demux_asf_t *this) {

  uint8_t buf;
  int     i;

  i = this->input->read (this->input, &buf, 1);

  /* printf ("%02x ", buf); */
  
  if (i != 1) {
    printf ("demux_asf: end of data\n");
    this->status = DEMUX_FINISHED;
  }

  return buf;
}

static uint16_t get_le16 (demux_asf_t *this) {

  uint8_t buf[2];
  int     i;

  i = this->input->read (this->input, buf, 2);

  /* printf (" [%02x %02x] ", buf[0], buf[1]); */

  if (i != 2) {
    printf ("demux_asf: end of data\n");
    this->status = DEMUX_FINISHED;
  }

  return buf[0] | (buf[1] << 8);
}

static uint32_t get_le32 (demux_asf_t *this) {

  uint8_t buf[4];
  int     i;

  i = this->input->read (this->input, buf, 4);

  /* printf ("%02x %02x %02x %02x ", buf[0], buf[1], buf[2], buf[3]); */

  if (i != 4) {
    printf ("demux_asf: end of data\n");
    this->status = DEMUX_FINISHED;
  }

  return buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
}

static uint64_t get_le64 (demux_asf_t *this) {

  uint8_t buf[8];
  int     i;

  i = this->input->read (this->input, buf, 8);

  if (i != 8) {
    printf ("demux_asf: end of data\n");
    this->status = DEMUX_FINISHED;
  }

  return (uint64_t) buf[0] 
    | ((uint64_t) buf[1] << 8)
    | ((uint64_t) buf[2] << 16)
    | ((uint64_t) buf[3] << 24)
    | ((uint64_t) buf[4] << 32)
    | ((uint64_t) buf[5] << 40)
    | ((uint64_t) buf[6] << 48)
    | ((uint64_t) buf[7] << 54) ;
}

static void get_guid (demux_asf_t *this, GUID *g) {
  int i; 

  g->v1 = get_le32(this);
  g->v2 = get_le16(this);
  g->v3 = get_le16(this);
  for(i=0;i<8;i++)
    g->v4[i] = get_byte(this);

}

static void get_str16_nolen(demux_asf_t *this, int len, 
			    char *buf, int buf_size) {

  int c;
  char *q;

  q = buf;
  while (len > 0) {
    c = get_le16(this);
    if ((q - buf) < buf_size - 1)
      *q++ = c;
    len-=2;
  }
  *q = '\0';
}

static void asf_send_audio_header (demux_asf_t *this, int stream_id) {

  buf_element_t *buf;
  WAVEFORMATEX  *wavex = (WAVEFORMATEX *) this->wavex ;

  if (!this->audio_fifo)
    return;

  this->streams[this->num_streams].buf_type = 
    formattag_to_buf_audio ( wavex->wFormatTag );
    
  if ( !this->streams[this->num_streams].buf_type ) {
    printf ("demux_asf: unknown audio type 0x%x\n", wavex->wFormatTag);
    this->streams[this->num_streams].buf_type     = BUF_CONTROL_NOP;
  }
  else
    printf ("demux_asf: audio format : %s (wFormatTag 0x%x)\n", 
	    buf_audio_name(this->streams[this->num_streams].buf_type),
	    wavex->wFormatTag);

  this->streams[this->num_streams].buf_type   |= this->num_audio_streams;
  this->streams[this->num_streams].fifo        = this->audio_fifo;
  this->streams[this->num_streams].stream_id   = stream_id;
  this->streams[this->num_streams].frag_offset = 0;
  this->streams[this->num_streams].seq         = 0;
  if (this->reorder_h > 1 && this->reorder_w > 1 ) {
    if( !this->streams[this->num_streams].buffer )
      this->streams[this->num_streams].buffer = malloc( DEFRAG_BUFSIZE );
    this->streams[this->num_streams].defrag = 1;
  } else
    this->streams[this->num_streams].defrag = 0;
  
  buf = this->audio_fifo->buffer_pool_alloc (this->audio_fifo);
  buf->content = buf->mem;
  memcpy (buf->content, this->wavex, this->wavex_size);

  printf ("demux_asf: wavex header is %d bytes long\n", this->wavex_size);

  buf->size = this->wavex_size;
  buf->type = this->streams[this->num_streams].buf_type;
  buf->decoder_info[0] = 0; /* first package, containing wavex */
  buf->decoder_info[1] = wavex->nSamplesPerSec;
  buf->decoder_info[2] = wavex->wBitsPerSample;
  buf->decoder_info[3] = wavex->nChannels;
  this->audio_fifo->put (this->audio_fifo, buf);

  this->num_streams++;
  this->num_audio_streams++;
}

static unsigned long str2ulong(unsigned char *str) {
  return ( str[0] | (str[1]<<8) | (str[2]<<16) | (str[3]<<24) );
}

static void asf_send_video_header (demux_asf_t *this, int stream_id) {

  buf_element_t    *buf;
  BITMAPINFOHEADER *bih = (BITMAPINFOHEADER *) this->bih;

  this->streams[this->num_streams].buf_type = 
    fourcc_to_buf_video((void*)&bih->biCompression);
    
  if( !this->streams[this->num_streams].buf_type ) {
    printf ("demux_asf: unknown video format %.4s\n",
	    (char*)&bih->biCompression);
    
    this->status = DEMUX_FINISHED;
    return;
  }
  
  this->streams[this->num_streams].buf_type    |= this->num_video_streams;
  this->streams[this->num_streams].fifo         = this->video_fifo;
  this->streams[this->num_streams].stream_id    = stream_id;
  this->streams[this->num_streams].frag_offset  = 0;
  this->streams[this->num_streams].defrag = 0;
  
  /* 
  printf ("demux_asf: video format : %.4s\n", (char*)&bih->biCompression);
  */
  printf ("demux_asf: video format : %s\n", 
           buf_video_name(this->streams[this->num_streams].buf_type));

  buf = this->video_fifo->buffer_pool_alloc (this->video_fifo);
  buf->content = buf->mem;
  buf->decoder_info[0] = 0; /* first package, containing bih */
  buf->decoder_info[1] = 3000; /* FIXME ? */
  memcpy (buf->content, &this->bih, this->bih_size);
  buf->size = this->bih_size;
  buf->type = this->streams[this->num_streams].buf_type ;
  this->video_fifo->put (this->video_fifo, buf);

  this->num_streams++;
  this->num_video_streams++;
  
}

static int asf_read_header (demux_asf_t *this) {

  GUID           g;
  uint64_t       gsize;

  get_guid(this, &g);
  if (memcmp(&g, &asf_header, sizeof(GUID))) {
    printf ("demux_asf: file doesn't start with an asf header\n");
    return 0;
  }
  get_le64(this);
  get_le32(this);
  get_byte(this);
  get_byte(this);

  for(;;) {
    get_guid(this, &g);

    gsize = get_le64(this);

    if (gsize < 24)
      goto fail;

    if (!memcmp(&g, &file_header, sizeof(GUID))) {

      uint64_t start_time, end_time;

      get_guid(this, &g);
      get_le64(this); /* file size */
      get_le64(this); /* file time */
      get_le64(this); /* nb_packets */

      end_time =  get_le64 (this); 
      
      this->length = get_le64(this) / 10000000; 
      if (this->input->get_capabilities(this->input) & INPUT_CAP_SEEKABLE) {
	this->rate = this->input->get_length (this->input) / this->length;
      } else
	this->rate = 0;

      printf ("demux_asf: stream length is %d sec, rate is %d bytes/sec\n",
	      this->length, this->rate);

      start_time = get_le32(this); /* start timestamp in 1/1000 s*/

      get_le32(this);
      get_le32(this);
      this->packet_size = get_le32(this);
      get_le32(this);
      get_le32(this);

    } else if (!memcmp(&g, &stream_header, sizeof(GUID))) {

      int           type, total_size, stream_data_size, stream_id;
      uint64_t      pos1, pos2;
            
      pos1 = this->input->get_current_pos (this->input);

      get_guid(this, &g);
      if (!memcmp(&g, &audio_stream, sizeof(GUID))) {
	type = CODEC_TYPE_AUDIO;
      } else if (!memcmp(&g, &video_stream, sizeof(GUID))) {
	type = CODEC_TYPE_VIDEO;
      } else {
	goto fail;
      }
      get_guid(this, &g);
      get_le64(this);
      total_size = get_le32(this);
      stream_data_size = get_le32(this); 
      stream_id = get_le16(this); /* stream id */
      get_le32(this);

      if (type == CODEC_TYPE_AUDIO) {
        uint8_t buffer[6];

	this->input->read (this->input, (uint8_t *) this->wavex, total_size);
	
        /*
	printf ("total size: %d bytes\n", total_size);
	*/
        
	/*
	this->input->read (this->input, (uint8_t *) &this->wavex[9], this->wavex[8]);
	*/
        if (!memcmp(&g, &audio_conceal_interleave, sizeof(GUID))) {
	  this->input->read (this->input, buffer, 6);
          this->reorder_h=buffer[0];
          this->reorder_w=(buffer[2]<<8)|buffer[1];
          this->reorder_b=(buffer[4]<<8)|buffer[3];
  	  this->reorder_w/=this->reorder_b;
          printf("demux_asf: audio conceal interleave detected (%d x %d x %d)\n",
             this->reorder_w, this->reorder_h, this->reorder_b );
	} else {
	  this->reorder_b=this->reorder_h=this->reorder_w=1;        
        }

	this->wavex_size = total_size; /* 18 + this->wavex[8]; */

	asf_send_audio_header (this, stream_id);

      } else {

	get_le32(this); /* width */
	get_le32(this); /* height */
	get_byte(this);
	this->bih_size = get_le16(this); /* size */

	this->input->read (this->input, (uint8_t *) this->bih, this->bih_size);

	asf_send_video_header (this, stream_id);
      }
      pos2 = this->input->get_current_pos (this->input);
      this->input->seek (this->input, gsize - (pos2 - pos1 + 24), SEEK_CUR);

    } else if (!memcmp(&g, &data_header, sizeof(GUID))) {
      break;

    } else if (!memcmp(&g, &comment_header, sizeof(GUID))) {
      int len1, len2, len3, len4, len5;

      len1 = get_le16(this);
      len2 = get_le16(this);
      len3 = get_le16(this);
      len4 = get_le16(this);
      len5 = get_le16(this);
      get_str16_nolen(this, len1, this->title, sizeof(this->title));
      get_str16_nolen(this, len2, this->author, sizeof(this->author));
      get_str16_nolen(this, len3, this->copyright, sizeof(this->copyright));
      get_str16_nolen(this, len4, this->comment, sizeof(this->comment));
      this->input->seek (this->input, len5, SEEK_CUR);
      /*
        } else if (url_feof(this)) {
	goto fail;
      */
    } else {
      this->input->seek (this->input, gsize - 24, SEEK_CUR);
    }
  }
  get_guid(this, &g);
  get_le64(this);
  get_byte(this);
  get_byte(this);

  this->packet_size_left = 0;

  return 1;

 fail:
  return 0;
}

static void asf_reorder(demux_asf_t *this, uint8_t *src, int len){
  uint8_t *dst=malloc(len);
  uint8_t *s2=src;
  int i=0,x,y;
  
  while(len-i >= this->reorder_h*this->reorder_w*this->reorder_b){
	for(x=0;x<this->reorder_w;x++)
	  for(y=0;y<this->reorder_h;y++){
	    memcpy(dst+i,s2+(y*this->reorder_w+x)*this->reorder_b,this->reorder_b);
		i+=this->reorder_b;
	  }
	s2+=this->reorder_h*this->reorder_w*this->reorder_b;
  }

  xine_fast_memcpy(src,dst,i);
  free(dst);
}

static int asf_get_packet(demux_asf_t *this) {

  int timestamp, hdr_size;

  uint32_t sig = 0;

  hdr_size = 11;

  while ( (this->status == DEMUX_OK) && (sig != 0x820000) ) {
    sig = ((sig << 8) | get_byte(this)) & 0xFFFFFF;
  }

  this->packet_flags = get_byte(this);
  this->segtype = get_byte(this);
  this->packet_padsize = 0;
  
  if (this->packet_flags & 0x40) {
    get_le16(this);
    printf("demux_asf: absolute size ignored\n");
    hdr_size += 2;
  }
      
  if (this->packet_flags & 0x10) {
    this->packet_padsize = get_le16(this);
    hdr_size += 2;
  } else if (this->packet_flags & 0x08) {
    this->packet_padsize = get_byte(this);
    hdr_size++;
  }
  timestamp = get_le32(this);
  get_le16(this); /* duration */
  if (this->packet_flags & 0x01) {
    this->nb_frames = get_byte(this); /* nb_frames */
    hdr_size++;
  }
  else
    this->nb_frames = 1;
  this->frame = 0;
    
  this->packet_size_left = this->packet_size - hdr_size;

  /*
  printf ("demux_asf: new packet, size = %d, flags = 0x%02x, padsize = %d\n",
	  this->packet_size_left, this->packet_flags, this->packet_padsize);
  */

  return 1;
}

static void hexdump (unsigned char *data, int len) {
  int i;

  for (i=0; i<len; i++)
    printf ("%02x ", data[i]);
  printf ("\n");

}

static void asf_send_buffer_nodefrag (demux_asf_t *this, asf_stream_t *stream, 
			     int frag_offset, int seq, int timestamp, 
			     int frag_len, int payload_size) {

  buf_element_t *buf;
  int bufsize;

  if (stream->frag_offset == 0) {
    /* new packet */
    stream->seq = seq;
  } else {
    if (seq == stream->seq && 
	frag_offset == stream->frag_offset) {
      /* continuing packet */
    } else {
      /* cannot continue current packet: free it */
      stream->frag_offset = 0;
      if (frag_offset != 0) {
	/* cannot create new packet */
	this->input->seek (this->input, frag_len, SEEK_CUR);
	return ;
      } else {
	/* create new packet */
	stream->seq = seq;
      }
    }
  }
  
  
  while( frag_len ) {
    if ( frag_len < stream->fifo->buffer_pool_buf_size )
      bufsize = frag_len;
    else
      bufsize = stream->fifo->buffer_pool_buf_size;
  
    buf = stream->fifo->buffer_pool_alloc (stream->fifo);
    buf->content = buf->mem;
    this->input->read (this->input, buf->content, bufsize);

    if (stream->fifo == this->video_fifo) {
      buf->input_pos  = this->input->get_current_pos (this->input);
      buf->input_time = buf->input_pos / this->rate;
    } else {
      buf->input_pos  = 0 ;
      buf->input_time = 0 ;
    }
    buf->PTS        = timestamp * 90;
    buf->SCR        = timestamp * 90;
    buf->type       = stream->buf_type;
    buf->size       = bufsize;
    timestamp       = 0;
  
    stream->frag_offset += bufsize;
    frag_len -= bufsize;

    /* test if whole packet read */
    if (stream->frag_offset == payload_size) {
      buf->decoder_info[0] = 2;
      stream->frag_offset = 0;
    } else 
      buf->decoder_info[0] = 1;

    stream->fifo->put (stream->fifo, buf);
  }
}


static void asf_send_buffer_defrag (demux_asf_t *this, asf_stream_t *stream, 
			     int frag_offset, int seq, int timestamp, 
			     int frag_len, int payload_size) {

  buf_element_t *buf;

  /*
  printf("asf_send_buffer seq=%d frag_offset=%d frag_len=%d\n",
    seq, frag_offset, frag_len );
  */
  
  if (stream->frag_offset == 0) {
    /* new packet */
    stream->seq = seq;
  } else {
    if (seq == stream->seq && 
	frag_offset == stream->frag_offset) {
      /* continuing packet */
    } else {
      /* cannot continue current packet: free it */
      if( stream->frag_offset )
      {
        int bufsize;
        uint8_t *p;
               
        if (stream->fifo == this->audio_fifo && 
            this->reorder_h > 1 && this->reorder_w > 1 ) {
          asf_reorder(this,stream->buffer,stream->frag_offset);
        }
        
        p = stream->buffer;
        while( stream->frag_offset ) {
          if ( stream->frag_offset < stream->fifo->buffer_pool_buf_size )
            bufsize = stream->frag_offset;
          else
            bufsize = stream->fifo->buffer_pool_buf_size;

          buf = stream->fifo->buffer_pool_alloc (stream->fifo);
          buf->content = buf->mem;
          xine_fast_memcpy (buf->content, p, bufsize);

          if (stream->fifo == this->video_fifo) {
            buf->input_pos  = this->input->get_current_pos (this->input);
            buf->input_time = buf->input_pos / this->rate;
          } else {
            buf->input_pos  = 0 ;
            buf->input_time = 0 ;
          }
          
          buf->PTS        = stream->timestamp * 90 + stream->ts_per_kbyte * 
                            (p-stream->buffer) / 1024; 
          buf->SCR        = buf->PTS;
          buf->type       = stream->buf_type;
          buf->size       = bufsize;
          
          stream->frag_offset -= bufsize;
          p+=bufsize;
          
          /* test if whole packet read */
          if ( !stream->frag_offset )
            buf->decoder_info[0] = 2;
          else 
            buf->decoder_info[0] = 1;

          stream->fifo->put (stream->fifo, buf);
        }
      }
      
      stream->frag_offset = 0;
      if (frag_offset != 0) {
	/* cannot create new packet */
	this->input->seek (this->input, frag_len, SEEK_CUR);
	return ;
      } else {
	/* create new packet */
	stream->seq = seq;
      }
    }
  }
      
  
  if( frag_offset ) {
    if( timestamp )
      stream->ts_per_kbyte = (timestamp - stream->timestamp) * 1024 * 90 / frag_offset;
  } else {
    stream->ts_per_kbyte = 0;
    stream->timestamp = timestamp;
  }
  
  if( stream->frag_offset + frag_len > DEFRAG_BUFSIZE )
    printf("demux_asf: buffer overflow on defrag!\n");
  else {  
    this->input->read (this->input, &stream->buffer[stream->frag_offset], frag_len);
    stream->frag_offset += frag_len;
  }
  
  /*
  printf ("demux_asf: read %d bytes :", frag_len);
  hexdump (buf->content, frag_len);
  */
}


static void asf_read_packet(demux_asf_t *this) {

  int            raw_id, stream_id, seq, frag_offset, payload_size, frag_len;
  int            timestamp, flags, i;
  asf_stream_t  *stream;

  if ((this->packet_size_left < FRAME_HEADER_SIZE) ||
      (this->packet_size_left <= this->packet_padsize) ||
      (++this->frame == (this->nb_frames & 0x3f)) ) {
    /* fail safe */

    /* printf ("demux_asf: reading new packet, packet size left %d\n", this->packet_size_left);
    */
    if (this->packet_size_left)
      this->input->seek (this->input, this->packet_size_left, SEEK_CUR);
    
    if (!asf_get_packet(this)) {
      printf ("demux_asf: get_packet failed\n");
      this->status = DEMUX_FINISHED;
      return ;
    }
  }
  
  /* read segment header, find stream */

  raw_id     = get_byte(this);
  stream_id  = raw_id & 0x7f;

  stream    = NULL;
  if ( (raw_id & 0x80) || this->keyframe_found ) {
    for (i=0; i<this->num_streams; i++)
      if (this->streams[i].stream_id == stream_id)
	stream = &this->streams[i];
    this->keyframe_found = 1;
  }

  seq           = get_byte(this);
  switch (this->segtype){
  case 0x55:
    frag_offset = get_byte(this);
    this->packet_size_left -= 1;
    break;
  case 0x59:
    frag_offset = get_le16(this);
    this->packet_size_left -= 2;
    break;
  case 0x5D:
    frag_offset = get_le32(this);
    this->packet_size_left -= 4;
    break;
  default:
    printf("demux_asf: unknow segtype %x\n",this->segtype);
    frag_offset = get_le32(this);
    this->packet_size_left -= 4;
    break;
  }
  flags         = get_byte(this); 

  /*
  printf ("\ndemux_asf: segment header, stream id %02x, frag_offset %d, flags : %02x\n", 
	  stream_id, frag_offset, flags);
  */

  if (flags == 1) {
    int data_length, data_sent=0;

    timestamp = frag_offset;
    get_byte (this);


    if (this->packet_flags & 0x01) {
      if( (this->nb_frames & 0xc0) == 0x40 ) {
        data_length = get_byte (this);
	this->packet_size_left --;
      } else {
        data_length = get_le16 (this);
	this->packet_size_left -= 2;
      }
      this->packet_size_left -= data_length + 4;
      
      /*
      printf ("demux_asf: reading grouping part segment, size = %d\n",
	      data_length);
      */

    } else {

      data_length = this->packet_size_left - 4 - this->packet_padsize; 
      this->packet_size_left = this->packet_padsize;

      /*
      printf ("demux_asf: reading grouping single segment, size = %d\n", data_length); 
      */
    }

    while (data_sent < data_length) {
      int object_length = get_byte(this);

      /*
      printf ("demux_asf: sending grouped object, len = %d\n", object_length);
      */

      if (stream) {
	if (stream->defrag)
          asf_send_buffer_defrag (this, stream, 0, seq, timestamp, 
			 object_length, object_length);
        else
          asf_send_buffer_nodefrag (this, stream, 0, seq, timestamp, 
			 object_length, object_length);
      }
      else {
        /*
	printf ("demux_asf: unhandled stream type, id %d\n", stream_id);
        */
	this->input->seek (this->input, object_length, SEEK_CUR);
      }

      seq++;
      data_sent += object_length+1;
      timestamp = 0;

    }

  } else {

    payload_size  = get_le32(this);
    timestamp     = get_le32(this);
    if (this->packet_flags & 0x01) {
      if( (this->nb_frames & 0xc0) == 0x40 ) {
        frag_len    = get_byte(this);
        this->packet_size_left--;
      } else {
        frag_len      = get_le16(this);
        this->packet_size_left -= 2;
      }        
      this->packet_size_left -= FRAME_HEADER_SIZE + frag_len - 6;
      
/*
      printf ("demux_asf: reading part segment, size = %d\n",
	      frag_len);
      */

    } else {
      frag_len = this->packet_size_left - 11 - this->packet_padsize; 
      this->packet_size_left = this->packet_padsize;
      
      /*
      printf ("demux_asf: reading single segment, size = %d\n", frag_len); 
      */

    }

    
    if (stream) {
      if (stream->defrag)
        asf_send_buffer_defrag (this, stream, frag_offset, seq, timestamp, 
                                frag_len, payload_size);
      else
        asf_send_buffer_nodefrag (this, stream, frag_offset, seq, timestamp,
                                  frag_len, payload_size);
    } else {
      /*
      printf ("demux_asf: unhandled stream type, id %d\n", stream_id);
      */
      this->input->seek (this->input, frag_len, SEEK_CUR);
    }  
  }
}
  
/* 
 * xine specific functions start here
 */

static void *demux_asf_loop (void *this_gen) {
  buf_element_t *buf;

  demux_asf_t *this = (demux_asf_t *) this_gen;

  /* printf ("demux_asf: demux loop starting...\n"); */

  this->send_end_buffers = 1;

  while (this->status == DEMUX_OK) {

    asf_read_packet (this);

  }

  /*
    printf ("demux_asf: demux loop finished (status: %d)\n",
    this->status);
  */

  this->status = DEMUX_FINISHED;

  if (this->send_end_buffers) {
    buf = this->video_fifo->buffer_pool_alloc (this->video_fifo);
    buf->type            = BUF_CONTROL_END;
    buf->decoder_info[0] = 0; /* stream finished */
    this->video_fifo->put (this->video_fifo, buf);
    
    if(this->audio_fifo) {
      buf = this->audio_fifo->buffer_pool_alloc (this->audio_fifo);
      buf->type            = BUF_CONTROL_END;
      buf->decoder_info[0] = 0; /* stream finished */
      this->audio_fifo->put (this->audio_fifo, buf);
    }

  }

  pthread_exit(NULL);

  return NULL;
}

static void demux_asf_close (demux_plugin_t *this_gen) {

  demux_asf_t *this = (demux_asf_t *) this_gen;
  free (this);
  
}

static void demux_asf_stop (demux_plugin_t *this_gen) {

  demux_asf_t *this = (demux_asf_t *) this_gen;
  buf_element_t *buf;
  int i;
  void *p;

  if (this->status != DEMUX_OK) {
    printf ("demux_asf: stop...ignored\n");
    return;
  }

  this->send_end_buffers = 0;
  this->status = DEMUX_FINISHED;

  pthread_cancel (this->thread);
  pthread_join (this->thread, &p);

  this->video_fifo->clear(this->video_fifo);
  if (this->audio_fifo)
    this->audio_fifo->clear(this->audio_fifo);

  buf = this->video_fifo->buffer_pool_alloc (this->video_fifo);
  buf->type            = BUF_CONTROL_END;
  buf->decoder_info[0] = 1; /* forced */

  this->video_fifo->put (this->video_fifo, buf);

  if(this->audio_fifo) {
    buf = this->audio_fifo->buffer_pool_alloc (this->audio_fifo);
    buf->type            = BUF_CONTROL_END;
    buf->decoder_info[0] = 1; /* forced */
    this->audio_fifo->put (this->audio_fifo, buf);
  }
  
  for (i=0; i<this->num_streams; i++) {
    if( this->streams[i].buffer ) {
      free( this->streams[i].buffer );
      this->streams[i].buffer = NULL;
    }
  }
}

static int demux_asf_get_status (demux_plugin_t *this_gen) {
  demux_asf_t *this = (demux_asf_t *) this_gen;

  return this->status;
}

static void demux_asf_start (demux_plugin_t *this_gen,
			     fifo_buffer_t *video_fifo, 
			     fifo_buffer_t *audio_fifo,
			     off_t start_pos, int start_time) {

  demux_asf_t *this = (demux_asf_t *) this_gen;
  buf_element_t *buf;
  int err;

  this->video_fifo  = video_fifo;
  this->audio_fifo  = audio_fifo;

  /* 
   * send start buffer
   */

  buf = this->video_fifo->buffer_pool_alloc (this->video_fifo);
  buf->type    = BUF_CONTROL_START;
  this->video_fifo->put (this->video_fifo, buf);

  if(this->audio_fifo) {
    buf = this->audio_fifo->buffer_pool_alloc (this->audio_fifo);
    buf->type    = BUF_CONTROL_START;
    this->audio_fifo->put (this->audio_fifo, buf);
  }

  /*
   * initialize asf engine
   */

  this->num_streams              = 0;
  this->num_audio_streams        = 0;
  this->num_video_streams        = 0;
  this->packet_size              = 0;
  this->seqno                    = 0;
  
  if (this->input->get_capabilities (this->input) & INPUT_CAP_SEEKABLE)
    this->input->seek (this->input, 0, SEEK_SET);

  if (!asf_read_header (this)) {
    
    this->status = DEMUX_FINISHED;
    return;
  } 

  printf ("demux_asf: title        : %s\n", this->title);
  printf ("demux_asf: author       : %s\n", this->author);
  printf ("demux_asf: copyright    : %s\n", this->copyright);
  printf ("demux_asf: comment      : %s\n", this->comment);

  /*
   * seek to start position
   */

  if (this->input->get_capabilities(this->input) & INPUT_CAP_SEEKABLE) {

    off_t cur_pos = this->input->get_current_pos (this->input);
    
    if ( (!start_pos) && (start_time))
      start_pos = start_time * this->rate;

    if (start_pos<cur_pos)
      start_pos = cur_pos;

    this->input->seek (this->input, start_pos, SEEK_SET);
  }

  /*
   * now start demuxing
   */

  this->keyframe_found = 0;
  this->status = DEMUX_OK;

  if ((err = pthread_create (&this->thread,
			     NULL, demux_asf_loop, this)) != 0) {
    fprintf (stderr, "demux_asf: can't create new thread (%s)\n",
	     strerror(err));
    exit (1);
  }
}

static int demux_asf_open(demux_plugin_t *this_gen,
			  input_plugin_t *input, int stage) {

  demux_asf_t *this = (demux_asf_t *) this_gen;

  switch(stage) {

  case STAGE_BY_CONTENT:
    return DEMUX_CANNOT_HANDLE;
    break;

  case STAGE_BY_EXTENSION: {
    char *ending;
    char *MRL;
    
    MRL = input->get_mrl (input);
    
    /*
     * check ending
     */
    
    ending = strrchr(MRL, '.');
    
    if(!ending)
      return DEMUX_CANNOT_HANDLE;
    
    if(!strcasecmp(ending, ".asf")) {
      this->input = input;
      return DEMUX_CAN_HANDLE;
    } else if(!strcasecmp(ending, ".wmv")) {
      this->input = input;
      return DEMUX_CAN_HANDLE;
    }
  }
  break;
  }

  return DEMUX_CANNOT_HANDLE;
}

static char *demux_asf_get_id(void) {
  return "ASF";
}

static char *demux_asf_get_mimetypes(void) {
  return "video/x-ms-asf: asf: ASF animation;"
         "video/x-ms-wmv: wmv: WMV animation;";
}

static int demux_asf_get_stream_length (demux_plugin_t *this_gen) {

  demux_asf_t *this = (demux_asf_t *) this_gen;

  return this->length;
}

demux_plugin_t *init_demuxer_plugin(int iface, xine_t *xine) {

  demux_asf_t     *this;
  config_values_t *config;

  if (iface != 6) {
    printf( "demux_asf: plugin doesn't support plugin API version %d.\n"
	    "demux_asf: this means there's a version mismatch between xine and this "
	    "demux_asf: demuxer plugin.\nInstalling current demux plugins should help.\n",
	    iface);
    return NULL;
  }

  this        = xine_xmalloc (sizeof (demux_asf_t));
  config      = xine->config;

  this->demux_plugin.interface_version = DEMUXER_PLUGIN_IFACE_VERSION;
  this->demux_plugin.open              = demux_asf_open;
  this->demux_plugin.start             = demux_asf_start;
  this->demux_plugin.stop              = demux_asf_stop;
  this->demux_plugin.close             = demux_asf_close;
  this->demux_plugin.get_status        = demux_asf_get_status;
  this->demux_plugin.get_identifier    = demux_asf_get_id;
  this->demux_plugin.get_stream_length = demux_asf_get_stream_length;
  this->demux_plugin.get_mimetypes     = demux_asf_get_mimetypes;
  
  return (demux_plugin_t *) this;
}