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.

1116 lines
41KB

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