You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1042 lines
37KB

  1. /*
  2. * ASF compatible demuxer
  3. * Copyright (c) 2000, 2001 Fabrice Bellard.
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avformat.h"
  22. #include "riff.h"
  23. #include "mpegaudio.h"
  24. #include "asf.h"
  25. #include "common.h"
  26. #undef NDEBUG
  27. #include <assert.h>
  28. #define FRAME_HEADER_SIZE 17
  29. // Fix Me! FRAME_HEADER_SIZE may be different.
  30. static const GUID index_guid = {
  31. 0x90, 0x08, 0x00, 0x33, 0xb1, 0xe5, 0xcf, 0x11, 0x89, 0xf4, 0x00, 0xa0, 0xc9, 0x03, 0x49, 0xcb
  32. };
  33. static const GUID stream_bitrate_guid = { /* (http://get.to/sdp) */
  34. 0xce, 0x75, 0xf8, 0x7b, 0x8d, 0x46, 0xd1, 0x11, 0x8d, 0x82, 0x00, 0x60, 0x97, 0xc9, 0xa2, 0xb2
  35. };
  36. /**********************************/
  37. /* decoding */
  38. //#define DEBUG
  39. #ifdef DEBUG
  40. #define PRINT_IF_GUID(g,cmp) \
  41. if (!memcmp(g, &cmp, sizeof(GUID))) \
  42. printf("(GUID: %s) ", #cmp)
  43. static void print_guid(const GUID *g)
  44. {
  45. int i;
  46. PRINT_IF_GUID(g, asf_header);
  47. else PRINT_IF_GUID(g, file_header);
  48. else PRINT_IF_GUID(g, stream_header);
  49. else PRINT_IF_GUID(g, audio_stream);
  50. else PRINT_IF_GUID(g, audio_conceal_none);
  51. else PRINT_IF_GUID(g, video_stream);
  52. else PRINT_IF_GUID(g, video_conceal_none);
  53. else PRINT_IF_GUID(g, command_stream);
  54. else PRINT_IF_GUID(g, comment_header);
  55. else PRINT_IF_GUID(g, codec_comment_header);
  56. else PRINT_IF_GUID(g, codec_comment1_header);
  57. else PRINT_IF_GUID(g, data_header);
  58. else PRINT_IF_GUID(g, index_guid);
  59. else PRINT_IF_GUID(g, head1_guid);
  60. else PRINT_IF_GUID(g, head2_guid);
  61. else PRINT_IF_GUID(g, my_guid);
  62. else PRINT_IF_GUID(g, ext_stream_header);
  63. else PRINT_IF_GUID(g, extended_content_header);
  64. else PRINT_IF_GUID(g, ext_stream_embed_stream_header);
  65. else PRINT_IF_GUID(g, ext_stream_audio_stream);
  66. else PRINT_IF_GUID(g, metadata_header);
  67. else PRINT_IF_GUID(g, stream_bitrate_guid);
  68. else
  69. printf("(GUID: unknown) ");
  70. for(i=0;i<16;i++)
  71. printf(" 0x%02x,", (*g)[i]);
  72. printf("}\n");
  73. }
  74. #undef PRINT_IF_GUID
  75. #endif
  76. static void get_guid(ByteIOContext *s, GUID *g)
  77. {
  78. assert(sizeof(*g) == 16);
  79. get_buffer(s, g, sizeof(*g));
  80. }
  81. #if 0
  82. static void get_str16(ByteIOContext *pb, char *buf, int buf_size)
  83. {
  84. int len, c;
  85. char *q;
  86. len = get_le16(pb);
  87. q = buf;
  88. while (len > 0) {
  89. c = get_le16(pb);
  90. if ((q - buf) < buf_size - 1)
  91. *q++ = c;
  92. len--;
  93. }
  94. *q = '\0';
  95. }
  96. #endif
  97. static void get_str16_nolen(ByteIOContext *pb, int len, char *buf, int buf_size)
  98. {
  99. char* q = buf;
  100. len /= 2;
  101. while (len--) {
  102. uint8_t tmp;
  103. PUT_UTF8(get_le16(pb), tmp, if (q - buf < buf_size - 1) *q++ = tmp;)
  104. }
  105. *q = '\0';
  106. }
  107. static int asf_probe(AVProbeData *pd)
  108. {
  109. /* check file header */
  110. if (!memcmp(pd->buf, &asf_header, sizeof(GUID)))
  111. return AVPROBE_SCORE_MAX;
  112. else
  113. return 0;
  114. }
  115. static int get_value(ByteIOContext *pb, int type){
  116. switch(type){
  117. case 2: return get_le32(pb);
  118. case 3: return get_le32(pb);
  119. case 4: return get_le64(pb);
  120. case 5: return get_le16(pb);
  121. default:return INT_MIN;
  122. }
  123. }
  124. static int asf_read_header(AVFormatContext *s, AVFormatParameters *ap)
  125. {
  126. ASFContext *asf = s->priv_data;
  127. GUID g;
  128. ByteIOContext *pb = &s->pb;
  129. AVStream *st;
  130. ASFStream *asf_st;
  131. int size, i;
  132. int64_t gsize;
  133. AVRational dar[128];
  134. memset(dar, 0, sizeof(dar));
  135. get_guid(pb, &g);
  136. if (memcmp(&g, &asf_header, sizeof(GUID)))
  137. goto fail;
  138. get_le64(pb);
  139. get_le32(pb);
  140. get_byte(pb);
  141. get_byte(pb);
  142. memset(&asf->asfid2avid, -1, sizeof(asf->asfid2avid));
  143. for(;;) {
  144. get_guid(pb, &g);
  145. gsize = get_le64(pb);
  146. #ifdef DEBUG
  147. printf("%08"PRIx64": ", url_ftell(pb) - 24);
  148. print_guid(&g);
  149. printf(" size=0x%"PRIx64"\n", gsize);
  150. #endif
  151. if (!memcmp(&g, &data_header, sizeof(GUID))) {
  152. asf->data_object_offset = url_ftell(pb);
  153. // if not streaming, gsize is not unlimited (how?), and there is enough space in the file..
  154. if (!(asf->hdr.flags & 0x01) && gsize >= 100) {
  155. asf->data_object_size = gsize - 24;
  156. } else {
  157. asf->data_object_size = (uint64_t)-1;
  158. }
  159. break;
  160. }
  161. if (gsize < 24)
  162. goto fail;
  163. if (!memcmp(&g, &file_header, sizeof(GUID))) {
  164. get_guid(pb, &asf->hdr.guid);
  165. asf->hdr.file_size = get_le64(pb);
  166. asf->hdr.create_time = get_le64(pb);
  167. asf->nb_packets = get_le64(pb);
  168. asf->hdr.send_time = get_le64(pb);
  169. asf->hdr.play_time = get_le64(pb);
  170. asf->hdr.preroll = get_le32(pb);
  171. asf->hdr.ignore = get_le32(pb);
  172. asf->hdr.flags = get_le32(pb);
  173. asf->hdr.min_pktsize = get_le32(pb);
  174. asf->hdr.max_pktsize = get_le32(pb);
  175. asf->hdr.max_bitrate = get_le32(pb);
  176. asf->packet_size = asf->hdr.max_pktsize;
  177. } else if (!memcmp(&g, &stream_header, sizeof(GUID))) {
  178. int type, type_specific_size, sizeX;
  179. uint64_t total_size;
  180. unsigned int tag1;
  181. int64_t pos1, pos2;
  182. int test_for_ext_stream_audio, is_dvr_ms_audio=0;
  183. pos1 = url_ftell(pb);
  184. st = av_new_stream(s, 0);
  185. if (!st)
  186. goto fail;
  187. av_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
  188. asf_st = av_mallocz(sizeof(ASFStream));
  189. if (!asf_st)
  190. goto fail;
  191. st->priv_data = asf_st;
  192. st->start_time = asf->hdr.preroll;
  193. if(!(asf->hdr.flags & 0x01)) { // if we aren't streaming...
  194. st->duration = asf->hdr.send_time /
  195. (10000000 / 1000) - st->start_time;
  196. }
  197. get_guid(pb, &g);
  198. test_for_ext_stream_audio = 0;
  199. if (!memcmp(&g, &audio_stream, sizeof(GUID))) {
  200. type = CODEC_TYPE_AUDIO;
  201. } else if (!memcmp(&g, &video_stream, sizeof(GUID))) {
  202. type = CODEC_TYPE_VIDEO;
  203. } else if (!memcmp(&g, &command_stream, sizeof(GUID))) {
  204. type = CODEC_TYPE_UNKNOWN;
  205. } else if (!memcmp(&g, &ext_stream_embed_stream_header, sizeof(GUID))) {
  206. test_for_ext_stream_audio = 1;
  207. type = CODEC_TYPE_UNKNOWN;
  208. } else {
  209. goto fail;
  210. }
  211. get_guid(pb, &g);
  212. total_size = get_le64(pb);
  213. type_specific_size = get_le32(pb);
  214. get_le32(pb);
  215. st->id = get_le16(pb) & 0x7f; /* stream id */
  216. // mapping of asf ID to AV stream ID;
  217. asf->asfid2avid[st->id] = s->nb_streams - 1;
  218. get_le32(pb);
  219. if (test_for_ext_stream_audio) {
  220. get_guid(pb, &g);
  221. if (!memcmp(&g, &ext_stream_audio_stream, sizeof(GUID))) {
  222. type = CODEC_TYPE_AUDIO;
  223. is_dvr_ms_audio=1;
  224. get_guid(pb, &g);
  225. get_le32(pb);
  226. get_le32(pb);
  227. get_le32(pb);
  228. get_guid(pb, &g);
  229. get_le32(pb);
  230. }
  231. }
  232. st->codec->codec_type = type;
  233. if (type == CODEC_TYPE_AUDIO) {
  234. get_wav_header(pb, st->codec, type_specific_size);
  235. if (is_dvr_ms_audio) {
  236. // codec_id and codec_tag are unreliable in dvr_ms
  237. // files. Set them later by probing stream.
  238. st->codec->codec_id = CODEC_ID_NONE;
  239. st->codec->codec_tag = 0;
  240. }
  241. st->need_parsing = AVSTREAM_PARSE_FULL;
  242. /* We have to init the frame size at some point .... */
  243. pos2 = url_ftell(pb);
  244. if (gsize >= (pos2 + 8 - pos1 + 24)) {
  245. asf_st->ds_span = get_byte(pb);
  246. asf_st->ds_packet_size = get_le16(pb);
  247. asf_st->ds_chunk_size = get_le16(pb);
  248. get_le16(pb); //ds_data_size
  249. get_byte(pb); //ds_silence_data
  250. }
  251. //printf("Descrambling: ps:%d cs:%d ds:%d s:%d sd:%d\n",
  252. // asf_st->ds_packet_size, asf_st->ds_chunk_size,
  253. // asf_st->ds_data_size, asf_st->ds_span, asf_st->ds_silence_data);
  254. if (asf_st->ds_span > 1) {
  255. if (!asf_st->ds_chunk_size
  256. || (asf_st->ds_packet_size/asf_st->ds_chunk_size <= 1)
  257. || asf_st->ds_packet_size % asf_st->ds_chunk_size)
  258. asf_st->ds_span = 0; // disable descrambling
  259. }
  260. switch (st->codec->codec_id) {
  261. case CODEC_ID_MP3:
  262. st->codec->frame_size = MPA_FRAME_SIZE;
  263. break;
  264. case CODEC_ID_PCM_S16LE:
  265. case CODEC_ID_PCM_S16BE:
  266. case CODEC_ID_PCM_U16LE:
  267. case CODEC_ID_PCM_U16BE:
  268. case CODEC_ID_PCM_S8:
  269. case CODEC_ID_PCM_U8:
  270. case CODEC_ID_PCM_ALAW:
  271. case CODEC_ID_PCM_MULAW:
  272. st->codec->frame_size = 1;
  273. break;
  274. default:
  275. /* This is probably wrong, but it prevents a crash later */
  276. st->codec->frame_size = 1;
  277. break;
  278. }
  279. } else if (type == CODEC_TYPE_VIDEO) {
  280. get_le32(pb);
  281. get_le32(pb);
  282. get_byte(pb);
  283. size = get_le16(pb); /* size */
  284. sizeX= get_le32(pb); /* size */
  285. st->codec->width = get_le32(pb);
  286. st->codec->height = get_le32(pb);
  287. /* not available for asf */
  288. get_le16(pb); /* panes */
  289. st->codec->bits_per_sample = get_le16(pb); /* depth */
  290. tag1 = get_le32(pb);
  291. url_fskip(pb, 20);
  292. // av_log(NULL, AV_LOG_DEBUG, "size:%d tsize:%d sizeX:%d\n", size, total_size, sizeX);
  293. size= sizeX;
  294. if (size > 40) {
  295. st->codec->extradata_size = size - 40;
  296. st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  297. get_buffer(pb, st->codec->extradata, st->codec->extradata_size);
  298. }
  299. /* Extract palette from extradata if bpp <= 8 */
  300. /* This code assumes that extradata contains only palette */
  301. /* This is true for all paletted codecs implemented in ffmpeg */
  302. if (st->codec->extradata_size && (st->codec->bits_per_sample <= 8)) {
  303. st->codec->palctrl = av_mallocz(sizeof(AVPaletteControl));
  304. #ifdef WORDS_BIGENDIAN
  305. for (i = 0; i < FFMIN(st->codec->extradata_size, AVPALETTE_SIZE)/4; i++)
  306. st->codec->palctrl->palette[i] = bswap_32(((uint32_t*)st->codec->extradata)[i]);
  307. #else
  308. memcpy(st->codec->palctrl->palette, st->codec->extradata,
  309. FFMIN(st->codec->extradata_size, AVPALETTE_SIZE));
  310. #endif
  311. st->codec->palctrl->palette_changed = 1;
  312. }
  313. st->codec->codec_tag = tag1;
  314. st->codec->codec_id = codec_get_id(codec_bmp_tags, tag1);
  315. if(tag1 == MKTAG('D', 'V', 'R', ' '))
  316. st->need_parsing = AVSTREAM_PARSE_FULL;
  317. }
  318. pos2 = url_ftell(pb);
  319. url_fskip(pb, gsize - (pos2 - pos1 + 24));
  320. } else if (!memcmp(&g, &comment_header, sizeof(GUID))) {
  321. int len1, len2, len3, len4, len5;
  322. len1 = get_le16(pb);
  323. len2 = get_le16(pb);
  324. len3 = get_le16(pb);
  325. len4 = get_le16(pb);
  326. len5 = get_le16(pb);
  327. get_str16_nolen(pb, len1, s->title , sizeof(s->title));
  328. get_str16_nolen(pb, len2, s->author , sizeof(s->author));
  329. get_str16_nolen(pb, len3, s->copyright, sizeof(s->copyright));
  330. get_str16_nolen(pb, len4, s->comment , sizeof(s->comment));
  331. url_fskip(pb, len5);
  332. } else if (!memcmp(&g, &stream_bitrate_guid, sizeof(GUID))) {
  333. int stream_count = get_le16(pb);
  334. int j;
  335. // av_log(NULL, AV_LOG_ERROR, "stream bitrate properties\n");
  336. // av_log(NULL, AV_LOG_ERROR, "streams %d\n", streams);
  337. for(j = 0; j < stream_count; j++) {
  338. int flags, bitrate, stream_id;
  339. flags= get_le16(pb);
  340. bitrate= get_le32(pb);
  341. stream_id= (flags & 0x7f);
  342. // av_log(NULL, AV_LOG_ERROR, "flags: 0x%x stream id %d, bitrate %d\n", flags, stream_id, bitrate);
  343. asf->stream_bitrates[stream_id-1]= bitrate;
  344. }
  345. } else if (!memcmp(&g, &extended_content_header, sizeof(GUID))) {
  346. int desc_count, i;
  347. desc_count = get_le16(pb);
  348. for(i=0;i<desc_count;i++)
  349. {
  350. int name_len,value_type,value_len;
  351. uint64_t value_num = 0;
  352. char name[1024];
  353. name_len = get_le16(pb);
  354. get_str16_nolen(pb, name_len, name, sizeof(name));
  355. value_type = get_le16(pb);
  356. value_len = get_le16(pb);
  357. if ((value_type == 0) || (value_type == 1)) // unicode or byte
  358. {
  359. if (!strcmp(name,"WM/AlbumTitle")) get_str16_nolen(pb, value_len, s->album, sizeof(s->album));
  360. else if(!strcmp(name,"WM/Genre" )) get_str16_nolen(pb, value_len, s->genre, sizeof(s->genre));
  361. else url_fskip(pb, value_len);
  362. }
  363. if ((value_type >= 2) && (value_type <= 5)) // boolean or DWORD or QWORD or WORD
  364. {
  365. value_num= get_value(pb, value_type);
  366. if (!strcmp(name,"WM/Track" )) s->track = value_num + 1;
  367. if (!strcmp(name,"WM/TrackNumber")) s->track = value_num;
  368. }
  369. }
  370. } else if (!memcmp(&g, &metadata_header, sizeof(GUID))) {
  371. int n, stream_num, name_len, value_len, value_type, value_num;
  372. n = get_le16(pb);
  373. for(i=0;i<n;i++) {
  374. char name[1024];
  375. get_le16(pb); //lang_list_index
  376. stream_num= get_le16(pb);
  377. name_len= get_le16(pb);
  378. value_type= get_le16(pb);
  379. value_len= get_le32(pb);
  380. get_str16_nolen(pb, name_len, name, sizeof(name));
  381. //av_log(NULL, AV_LOG_ERROR, "%d %d %d %d %d <%s>\n", i, stream_num, name_len, value_type, value_len, name);
  382. value_num= get_le16(pb);//we should use get_value() here but it doesnt work 2 is le16 here but le32 elsewhere
  383. url_fskip(pb, value_len - 2);
  384. if(stream_num<128){
  385. if (!strcmp(name, "AspectRatioX")) dar[stream_num].num= value_num;
  386. else if(!strcmp(name, "AspectRatioY")) dar[stream_num].den= value_num;
  387. }
  388. }
  389. } else if (!memcmp(&g, &ext_stream_header, sizeof(GUID))) {
  390. int ext_len, payload_ext_ct, stream_ct;
  391. uint32_t ext_d;
  392. int64_t pos_ex_st;
  393. pos_ex_st = url_ftell(pb);
  394. get_le64(pb); // starttime
  395. get_le64(pb); // endtime
  396. get_le32(pb); // leak-datarate
  397. get_le32(pb); // bucket-datasize
  398. get_le32(pb); // init-bucket-fullness
  399. get_le32(pb); // alt-leak-datarate
  400. get_le32(pb); // alt-bucket-datasize
  401. get_le32(pb); // alt-init-bucket-fullness
  402. get_le32(pb); // max-object-size
  403. get_le32(pb); // flags (reliable,seekable,no_cleanpoints?,resend-live-cleanpoints, rest of bits reserved)
  404. get_le16(pb); // stream-num
  405. get_le16(pb); // stream-language-id-index
  406. get_le64(pb); // avg frametime in 100ns units
  407. stream_ct = get_le16(pb); //stream-name-count
  408. payload_ext_ct = get_le16(pb); //payload-extension-system-count
  409. for (i=0; i<stream_ct; i++){
  410. get_le16(pb);
  411. ext_len = get_le16(pb);
  412. url_fseek(pb, ext_len, SEEK_CUR);
  413. }
  414. for (i=0; i<payload_ext_ct; i++){
  415. get_guid(pb, &g);
  416. ext_d=get_le16(pb);
  417. ext_len=get_le32(pb);
  418. url_fseek(pb, ext_len, SEEK_CUR);
  419. }
  420. // there could be a optional stream properties object to follow
  421. // if so the next iteration will pick it up
  422. } else if (!memcmp(&g, &head1_guid, sizeof(GUID))) {
  423. int v1, v2;
  424. get_guid(pb, &g);
  425. v1 = get_le32(pb);
  426. v2 = get_le16(pb);
  427. #if 0
  428. } else if (!memcmp(&g, &codec_comment_header, sizeof(GUID))) {
  429. int len, v1, n, num;
  430. char str[256], *q;
  431. char tag[16];
  432. get_guid(pb, &g);
  433. print_guid(&g);
  434. n = get_le32(pb);
  435. for(i=0;i<n;i++) {
  436. num = get_le16(pb); /* stream number */
  437. get_str16(pb, str, sizeof(str));
  438. get_str16(pb, str, sizeof(str));
  439. len = get_le16(pb);
  440. q = tag;
  441. while (len > 0) {
  442. v1 = get_byte(pb);
  443. if ((q - tag) < sizeof(tag) - 1)
  444. *q++ = v1;
  445. len--;
  446. }
  447. *q = '\0';
  448. }
  449. #endif
  450. } else if (url_feof(pb)) {
  451. goto fail;
  452. } else {
  453. url_fseek(pb, gsize - 24, SEEK_CUR);
  454. }
  455. }
  456. get_guid(pb, &g);
  457. get_le64(pb);
  458. get_byte(pb);
  459. get_byte(pb);
  460. if (url_feof(pb))
  461. goto fail;
  462. asf->data_offset = url_ftell(pb);
  463. asf->packet_size_left = 0;
  464. for(i=0; i<128; i++){
  465. int stream_num= asf->asfid2avid[i];
  466. if(stream_num>=0 && dar[i].num>0 && dar[i].den>0){
  467. AVCodecContext *codec= s->streams[stream_num]->codec;
  468. av_reduce(&codec->sample_aspect_ratio.num,
  469. &codec->sample_aspect_ratio.den,
  470. dar[i].num, dar[i].den, INT_MAX);
  471. //av_log(NULL, AV_LOG_ERROR, "dar %d:%d sar=%d:%d\n", dar[i].num, dar[i].den, codec->sample_aspect_ratio.num, codec->sample_aspect_ratio.den);
  472. }
  473. }
  474. return 0;
  475. fail:
  476. for(i=0;i<s->nb_streams;i++) {
  477. AVStream *st = s->streams[i];
  478. if (st) {
  479. av_free(st->priv_data);
  480. av_free(st->codec->extradata);
  481. }
  482. av_free(st);
  483. }
  484. return -1;
  485. }
  486. #define DO_2BITS(bits, var, defval) \
  487. switch (bits & 3) \
  488. { \
  489. case 3: var = get_le32(pb); rsize += 4; break; \
  490. case 2: var = get_le16(pb); rsize += 2; break; \
  491. case 1: var = get_byte(pb); rsize++; break; \
  492. default: var = defval; break; \
  493. }
  494. /**
  495. *
  496. * @return <0 in case of an error
  497. */
  498. static int asf_get_packet(AVFormatContext *s)
  499. {
  500. ASFContext *asf = s->priv_data;
  501. ByteIOContext *pb = &s->pb;
  502. uint32_t packet_length, padsize;
  503. int rsize = 8;
  504. int c, d, e, off;
  505. off= (url_ftell(&s->pb) - s->data_offset) % asf->packet_size + 3;
  506. c=d=e=-1;
  507. while(off-- > 0){
  508. c=d; d=e;
  509. e= get_byte(pb);
  510. if(c == 0x82 && !d && !e)
  511. break;
  512. }
  513. if (c != 0x82) {
  514. if (!url_feof(pb))
  515. av_log(s, AV_LOG_ERROR, "ff asf bad header %x at:%"PRId64"\n", c, url_ftell(pb));
  516. }
  517. if ((c & 0x8f) == 0x82) {
  518. if (d || e) {
  519. if (!url_feof(pb))
  520. av_log(s, AV_LOG_ERROR, "ff asf bad non zero\n");
  521. return -1;
  522. }
  523. c= get_byte(pb);
  524. d= get_byte(pb);
  525. rsize+=3;
  526. }else{
  527. url_fseek(pb, -1, SEEK_CUR); //FIXME
  528. }
  529. asf->packet_flags = c;
  530. asf->packet_property = d;
  531. DO_2BITS(asf->packet_flags >> 5, packet_length, asf->packet_size);
  532. DO_2BITS(asf->packet_flags >> 1, padsize, 0); // sequence ignored
  533. DO_2BITS(asf->packet_flags >> 3, padsize, 0); // padding length
  534. //the following checks prevent overflows and infinite loops
  535. if(packet_length >= (1U<<29)){
  536. av_log(s, AV_LOG_ERROR, "invalid packet_length %d at:%"PRId64"\n", packet_length, url_ftell(pb));
  537. return -1;
  538. }
  539. if(padsize >= packet_length){
  540. av_log(s, AV_LOG_ERROR, "invalid padsize %d at:%"PRId64"\n", padsize, url_ftell(pb));
  541. return -1;
  542. }
  543. asf->packet_timestamp = get_le32(pb);
  544. get_le16(pb); /* duration */
  545. // rsize has at least 11 bytes which have to be present
  546. if (asf->packet_flags & 0x01) {
  547. asf->packet_segsizetype = get_byte(pb); rsize++;
  548. asf->packet_segments = asf->packet_segsizetype & 0x3f;
  549. } else {
  550. asf->packet_segments = 1;
  551. asf->packet_segsizetype = 0x80;
  552. }
  553. asf->packet_size_left = packet_length - padsize - rsize;
  554. if (packet_length < asf->hdr.min_pktsize)
  555. padsize += asf->hdr.min_pktsize - packet_length;
  556. asf->packet_padsize = padsize;
  557. #ifdef DEBUG
  558. printf("packet: size=%d padsize=%d left=%d\n", asf->packet_size, asf->packet_padsize, asf->packet_size_left);
  559. #endif
  560. return 0;
  561. }
  562. /**
  563. *
  564. * @return <0 if error
  565. */
  566. static int asf_read_frame_header(AVFormatContext *s){
  567. ASFContext *asf = s->priv_data;
  568. ByteIOContext *pb = &s->pb;
  569. int rsize = 1;
  570. int num = get_byte(pb);
  571. int64_t ts0, ts1;
  572. asf->packet_segments--;
  573. asf->packet_key_frame = num >> 7;
  574. asf->stream_index = asf->asfid2avid[num & 0x7f];
  575. // sequence should be ignored!
  576. DO_2BITS(asf->packet_property >> 4, asf->packet_seq, 0);
  577. DO_2BITS(asf->packet_property >> 2, asf->packet_frag_offset, 0);
  578. DO_2BITS(asf->packet_property, asf->packet_replic_size, 0);
  579. //printf("key:%d stream:%d seq:%d offset:%d replic_size:%d\n", asf->packet_key_frame, asf->stream_index, asf->packet_seq, //asf->packet_frag_offset, asf->packet_replic_size);
  580. if (asf->packet_replic_size >= 8) {
  581. asf->packet_obj_size = get_le32(pb);
  582. if(asf->packet_obj_size >= (1<<24) || asf->packet_obj_size <= 0){
  583. av_log(s, AV_LOG_ERROR, "packet_obj_size invalid\n");
  584. return -1;
  585. }
  586. asf->packet_frag_timestamp = get_le32(pb); // timestamp
  587. if(asf->packet_replic_size >= 8+38+4){
  588. // for(i=0; i<asf->packet_replic_size-8; i++)
  589. // av_log(s, AV_LOG_DEBUG, "%02X ",get_byte(pb));
  590. // av_log(s, AV_LOG_DEBUG, "\n");
  591. url_fskip(pb, 10);
  592. ts0= get_le64(pb);
  593. ts1= get_le64(pb);
  594. url_fskip(pb, 12);
  595. get_le32(pb);
  596. url_fskip(pb, asf->packet_replic_size - 8 - 38 - 4);
  597. if(ts0!= -1) asf->packet_frag_timestamp= ts0/10000;
  598. else asf->packet_frag_timestamp= AV_NOPTS_VALUE;
  599. }else
  600. url_fskip(pb, asf->packet_replic_size - 8);
  601. rsize += asf->packet_replic_size; // FIXME - check validity
  602. } else if (asf->packet_replic_size==1){
  603. // multipacket - frag_offset is begining timestamp
  604. asf->packet_time_start = asf->packet_frag_offset;
  605. asf->packet_frag_offset = 0;
  606. asf->packet_frag_timestamp = asf->packet_timestamp;
  607. asf->packet_time_delta = get_byte(pb);
  608. rsize++;
  609. }else if(asf->packet_replic_size!=0){
  610. av_log(s, AV_LOG_ERROR, "unexpected packet_replic_size of %d\n", asf->packet_replic_size);
  611. return -1;
  612. }
  613. if (asf->packet_flags & 0x01) {
  614. DO_2BITS(asf->packet_segsizetype >> 6, asf->packet_frag_size, 0); // 0 is illegal
  615. if(asf->packet_frag_size > asf->packet_size_left - rsize){
  616. av_log(s, AV_LOG_ERROR, "packet_frag_size is invalid\n");
  617. return -1;
  618. }
  619. //printf("Fragsize %d\n", asf->packet_frag_size);
  620. } else {
  621. asf->packet_frag_size = asf->packet_size_left - rsize;
  622. //printf("Using rest %d %d %d\n", asf->packet_frag_size, asf->packet_size_left, rsize);
  623. }
  624. if (asf->packet_replic_size == 1) {
  625. asf->packet_multi_size = asf->packet_frag_size;
  626. if (asf->packet_multi_size > asf->packet_size_left)
  627. return -1;
  628. }
  629. asf->packet_size_left -= rsize;
  630. //printf("___objsize____ %d %d rs:%d\n", asf->packet_obj_size, asf->packet_frag_offset, rsize);
  631. return 0;
  632. }
  633. static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
  634. {
  635. ASFContext *asf = s->priv_data;
  636. ASFStream *asf_st = 0;
  637. ByteIOContext *pb = &s->pb;
  638. //static int pc = 0;
  639. for (;;) {
  640. if(url_feof(pb))
  641. return AVERROR_IO;
  642. if (asf->packet_size_left < FRAME_HEADER_SIZE
  643. || asf->packet_segments < 1) {
  644. //asf->packet_size_left <= asf->packet_padsize) {
  645. int ret = asf->packet_size_left + asf->packet_padsize;
  646. //printf("PacketLeftSize:%d Pad:%d Pos:%"PRId64"\n", asf->packet_size_left, asf->packet_padsize, url_ftell(pb));
  647. assert(ret>=0);
  648. /* fail safe */
  649. url_fskip(pb, ret);
  650. asf->packet_pos= url_ftell(&s->pb);
  651. if (asf->data_object_size != (uint64_t)-1 &&
  652. (asf->packet_pos - asf->data_object_offset >= asf->data_object_size))
  653. return AVERROR_IO; /* Do not exceed the size of the data object */
  654. ret = asf_get_packet(s);
  655. //printf("READ ASF PACKET %d r:%d c:%d\n", ret, asf->packet_size_left, pc++);
  656. if (ret < 0)
  657. assert(asf->packet_size_left < FRAME_HEADER_SIZE || asf->packet_segments < 1);
  658. asf->packet_time_start = 0;
  659. continue;
  660. }
  661. if (asf->packet_time_start == 0) {
  662. if(asf_read_frame_header(s) < 0){
  663. asf->packet_segments= 0;
  664. continue;
  665. }
  666. if (asf->stream_index < 0
  667. || s->streams[asf->stream_index]->discard >= AVDISCARD_ALL
  668. || (!asf->packet_key_frame && s->streams[asf->stream_index]->discard >= AVDISCARD_NONKEY)
  669. ) {
  670. asf->packet_time_start = 0;
  671. /* unhandled packet (should not happen) */
  672. url_fskip(pb, asf->packet_frag_size);
  673. asf->packet_size_left -= asf->packet_frag_size;
  674. if(asf->stream_index < 0)
  675. av_log(s, AV_LOG_ERROR, "ff asf skip %d (unknown stream)\n", asf->packet_frag_size);
  676. continue;
  677. }
  678. asf->asf_st = s->streams[asf->stream_index]->priv_data;
  679. }
  680. asf_st = asf->asf_st;
  681. if (asf->packet_replic_size == 1) {
  682. // frag_offset is here used as the begining timestamp
  683. asf->packet_frag_timestamp = asf->packet_time_start;
  684. asf->packet_time_start += asf->packet_time_delta;
  685. asf->packet_obj_size = asf->packet_frag_size = get_byte(pb);
  686. asf->packet_size_left--;
  687. asf->packet_multi_size--;
  688. if (asf->packet_multi_size < asf->packet_obj_size)
  689. {
  690. asf->packet_time_start = 0;
  691. url_fskip(pb, asf->packet_multi_size);
  692. asf->packet_size_left -= asf->packet_multi_size;
  693. continue;
  694. }
  695. asf->packet_multi_size -= asf->packet_obj_size;
  696. //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);
  697. }
  698. if ( asf_st->pkt.size != asf->packet_obj_size
  699. || asf_st->frag_offset + asf->packet_frag_size > asf_st->pkt.size) { //FIXME is this condition sufficient?
  700. if(asf_st->pkt.data){
  701. av_log(s, AV_LOG_INFO, "freeing incomplete packet size %d, new %d\n", asf_st->pkt.size, asf->packet_obj_size);
  702. asf_st->frag_offset = 0;
  703. av_free_packet(&asf_st->pkt);
  704. }
  705. /* new packet */
  706. av_new_packet(&asf_st->pkt, asf->packet_obj_size);
  707. asf_st->seq = asf->packet_seq;
  708. asf_st->pkt.pts = asf->packet_frag_timestamp;
  709. asf_st->pkt.stream_index = asf->stream_index;
  710. asf_st->pkt.pos =
  711. asf_st->packet_pos= asf->packet_pos;
  712. //printf("new packet: stream:%d key:%d packet_key:%d audio:%d size:%d\n",
  713. //asf->stream_index, asf->packet_key_frame, asf_st->pkt.flags & PKT_FLAG_KEY,
  714. //s->streams[asf->stream_index]->codec->codec_type == CODEC_TYPE_AUDIO, asf->packet_obj_size);
  715. if (s->streams[asf->stream_index]->codec->codec_type == CODEC_TYPE_AUDIO)
  716. asf->packet_key_frame = 1;
  717. if (asf->packet_key_frame)
  718. asf_st->pkt.flags |= PKT_FLAG_KEY;
  719. }
  720. /* read data */
  721. //printf("READ PACKET s:%d os:%d o:%d,%d l:%d DATA:%p\n",
  722. // asf->packet_size, asf_st->pkt.size, asf->packet_frag_offset,
  723. // asf_st->frag_offset, asf->packet_frag_size, asf_st->pkt.data);
  724. asf->packet_size_left -= asf->packet_frag_size;
  725. if (asf->packet_size_left < 0)
  726. continue;
  727. if( asf->packet_frag_offset >= asf_st->pkt.size
  728. || asf->packet_frag_size > asf_st->pkt.size - asf->packet_frag_offset){
  729. av_log(s, AV_LOG_ERROR, "packet fragment position invalid %u,%u not in %u\n",
  730. asf->packet_frag_offset, asf->packet_frag_size, asf_st->pkt.size);
  731. continue;
  732. }
  733. get_buffer(pb, asf_st->pkt.data + asf->packet_frag_offset,
  734. asf->packet_frag_size);
  735. asf_st->frag_offset += asf->packet_frag_size;
  736. /* test if whole packet is read */
  737. if (asf_st->frag_offset == asf_st->pkt.size) {
  738. /* return packet */
  739. if (asf_st->ds_span > 1) {
  740. if(asf_st->pkt.size != asf_st->ds_packet_size * asf_st->ds_span){
  741. av_log(s, AV_LOG_ERROR, "pkt.size != ds_packet_size * ds_span\n");
  742. }else{
  743. /* packet descrambling */
  744. uint8_t *newdata = av_malloc(asf_st->pkt.size);
  745. if (newdata) {
  746. int offset = 0;
  747. while (offset < asf_st->pkt.size) {
  748. int off = offset / asf_st->ds_chunk_size;
  749. int row = off / asf_st->ds_span;
  750. int col = off % asf_st->ds_span;
  751. int idx = row + col * asf_st->ds_packet_size / asf_st->ds_chunk_size;
  752. //printf("off:%d row:%d col:%d idx:%d\n", off, row, col, idx);
  753. assert(offset + asf_st->ds_chunk_size <= asf_st->pkt.size);
  754. assert(idx+1 <= asf_st->pkt.size / asf_st->ds_chunk_size);
  755. memcpy(newdata + offset,
  756. asf_st->pkt.data + idx * asf_st->ds_chunk_size,
  757. asf_st->ds_chunk_size);
  758. offset += asf_st->ds_chunk_size;
  759. }
  760. av_free(asf_st->pkt.data);
  761. asf_st->pkt.data = newdata;
  762. }
  763. }
  764. }
  765. asf_st->frag_offset = 0;
  766. *pkt= asf_st->pkt;
  767. //printf("packet %d %d\n", asf_st->pkt.size, asf->packet_frag_size);
  768. asf_st->pkt.size = 0;
  769. asf_st->pkt.data = 0;
  770. break; // packet completed
  771. }
  772. }
  773. return 0;
  774. }
  775. static int asf_read_close(AVFormatContext *s)
  776. {
  777. int i;
  778. for(i=0;i<s->nb_streams;i++) {
  779. AVStream *st = s->streams[i];
  780. av_free(st->priv_data);
  781. av_free(st->codec->palctrl);
  782. }
  783. return 0;
  784. }
  785. // Added to support seeking after packets have been read
  786. // If information is not reset, read_packet fails due to
  787. // leftover information from previous reads
  788. static void asf_reset_header(AVFormatContext *s)
  789. {
  790. ASFContext *asf = s->priv_data;
  791. ASFStream *asf_st;
  792. int i;
  793. asf->packet_nb_frames = 0;
  794. asf->packet_size_left = 0;
  795. asf->packet_segments = 0;
  796. asf->packet_flags = 0;
  797. asf->packet_property = 0;
  798. asf->packet_timestamp = 0;
  799. asf->packet_segsizetype = 0;
  800. asf->packet_segments = 0;
  801. asf->packet_seq = 0;
  802. asf->packet_replic_size = 0;
  803. asf->packet_key_frame = 0;
  804. asf->packet_padsize = 0;
  805. asf->packet_frag_offset = 0;
  806. asf->packet_frag_size = 0;
  807. asf->packet_frag_timestamp = 0;
  808. asf->packet_multi_size = 0;
  809. asf->packet_obj_size = 0;
  810. asf->packet_time_delta = 0;
  811. asf->packet_time_start = 0;
  812. for(i=0; i<s->nb_streams; i++){
  813. asf_st= s->streams[i]->priv_data;
  814. av_free_packet(&asf_st->pkt);
  815. asf_st->frag_offset=0;
  816. asf_st->seq=0;
  817. }
  818. asf->asf_st= NULL;
  819. }
  820. static int64_t asf_read_pts(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit)
  821. {
  822. ASFContext *asf = s->priv_data;
  823. AVPacket pkt1, *pkt = &pkt1;
  824. ASFStream *asf_st;
  825. int64_t pts;
  826. int64_t pos= *ppos;
  827. int i;
  828. int64_t start_pos[s->nb_streams];
  829. for(i=0; i<s->nb_streams; i++){
  830. start_pos[i]= pos;
  831. }
  832. pos= (pos+asf->packet_size-1-s->data_offset)/asf->packet_size*asf->packet_size+ s->data_offset;
  833. *ppos= pos;
  834. url_fseek(&s->pb, pos, SEEK_SET);
  835. //printf("asf_read_pts\n");
  836. asf_reset_header(s);
  837. for(;;){
  838. if (av_read_frame(s, pkt) < 0){
  839. av_log(s, AV_LOG_INFO, "asf_read_pts failed\n");
  840. return AV_NOPTS_VALUE;
  841. }
  842. pts= pkt->pts;
  843. av_free_packet(pkt);
  844. if(pkt->flags&PKT_FLAG_KEY){
  845. i= pkt->stream_index;
  846. asf_st= s->streams[i]->priv_data;
  847. // assert((asf_st->packet_pos - s->data_offset) % asf->packet_size == 0);
  848. pos= asf_st->packet_pos;
  849. av_add_index_entry(s->streams[i], pos, pts, pkt->size, pos - start_pos[i] + 1, AVINDEX_KEYFRAME);
  850. start_pos[i]= asf_st->packet_pos + 1;
  851. if(pkt->stream_index == stream_index)
  852. break;
  853. }
  854. }
  855. *ppos= pos;
  856. //printf("found keyframe at %"PRId64" stream %d stamp:%"PRId64"\n", *ppos, stream_index, pts);
  857. return pts;
  858. }
  859. static void asf_build_simple_index(AVFormatContext *s, int stream_index)
  860. {
  861. GUID g;
  862. ASFContext *asf = s->priv_data;
  863. int64_t gsize, itime;
  864. int64_t pos, current_pos, index_pts;
  865. int i;
  866. int pct,ict;
  867. current_pos = url_ftell(&s->pb);
  868. url_fseek(&s->pb, asf->data_object_offset + asf->data_object_size, SEEK_SET);
  869. get_guid(&s->pb, &g);
  870. if (!memcmp(&g, &index_guid, sizeof(GUID))) {
  871. gsize = get_le64(&s->pb);
  872. get_guid(&s->pb, &g);
  873. itime=get_le64(&s->pb);
  874. pct=get_le32(&s->pb);
  875. ict=get_le32(&s->pb);
  876. av_log(NULL, AV_LOG_DEBUG, "itime:0x%"PRIx64", pct:%d, ict:%d\n",itime,pct,ict);
  877. for (i=0;i<ict;i++){
  878. int pktnum=get_le32(&s->pb);
  879. int pktct =get_le16(&s->pb);
  880. av_log(NULL, AV_LOG_DEBUG, "pktnum:%d, pktct:%d\n", pktnum, pktct);
  881. pos=s->data_offset + asf->packet_size*(int64_t)pktnum;
  882. index_pts=av_rescale(itime, i, 10000);
  883. av_add_index_entry(s->streams[stream_index], pos, index_pts, asf->packet_size, 0, AVINDEX_KEYFRAME);
  884. }
  885. asf->index_read= 1;
  886. }
  887. url_fseek(&s->pb, current_pos, SEEK_SET);
  888. }
  889. static int asf_read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
  890. {
  891. ASFContext *asf = s->priv_data;
  892. AVStream *st = s->streams[stream_index];
  893. int64_t pos;
  894. int index;
  895. if (asf->packet_size <= 0)
  896. return -1;
  897. if (!asf->index_read)
  898. asf_build_simple_index(s, stream_index);
  899. if(!(asf->index_read && st->index_entries)){
  900. if(av_seek_frame_binary(s, stream_index, pts, flags)<0)
  901. return -1;
  902. }else{
  903. index= av_index_search_timestamp(st, pts, flags);
  904. if(index<0)
  905. return -1;
  906. /* find the position */
  907. pos = st->index_entries[index].pos;
  908. pts = st->index_entries[index].timestamp;
  909. // various attempts to find key frame have failed so far
  910. // asf_reset_header(s);
  911. // url_fseek(&s->pb, pos, SEEK_SET);
  912. // key_pos = pos;
  913. // for(i=0;i<16;i++){
  914. // pos = url_ftell(&s->pb);
  915. // if (av_read_frame(s, &pkt) < 0){
  916. // av_log(s, AV_LOG_INFO, "seek failed\n");
  917. // return -1;
  918. // }
  919. // asf_st = s->streams[stream_index]->priv_data;
  920. // pos += st->parser->frame_offset;
  921. //
  922. // if (pkt.size > b) {
  923. // b = pkt.size;
  924. // key_pos = pos;
  925. // }
  926. //
  927. // av_free_packet(&pkt);
  928. // }
  929. /* do the seek */
  930. av_log(NULL, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos);
  931. url_fseek(&s->pb, pos, SEEK_SET);
  932. }
  933. asf_reset_header(s);
  934. return 0;
  935. }
  936. AVInputFormat asf_demuxer = {
  937. "asf",
  938. "asf format",
  939. sizeof(ASFContext),
  940. asf_probe,
  941. asf_read_header,
  942. asf_read_packet,
  943. asf_read_close,
  944. asf_read_seek,
  945. asf_read_pts,
  946. };