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.

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