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.

1050 lines
38KB

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