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.

1090 lines
39KB

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