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.

1102 lines
40KB

  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/Track") && s->track == 0) {
  371. char track[8];
  372. get_str16_nolen(pb, value_len, track, sizeof(track));
  373. s->track = strtol(track, NULL, 10) + 1;
  374. }
  375. else if(!strcmp(name,"WM/TrackNumber")) {
  376. char track[8];
  377. get_str16_nolen(pb, value_len, track, sizeof(track));
  378. s->track = strtol(track, NULL, 10);
  379. }
  380. else url_fskip(pb, value_len);
  381. }
  382. if ((value_type >= 2) && (value_type <= 5)) // boolean or DWORD or QWORD or WORD
  383. {
  384. value_num= get_value(pb, value_type);
  385. if (!strcmp(name,"WM/Track" ) && s->track == 0) s->track = value_num + 1;
  386. if (!strcmp(name,"WM/TrackNumber")) s->track = value_num;
  387. }
  388. }
  389. } else if (!memcmp(&g, &metadata_header, sizeof(GUID))) {
  390. int n, stream_num, name_len, value_len, value_type, value_num;
  391. n = get_le16(pb);
  392. for(i=0;i<n;i++) {
  393. char name[1024];
  394. get_le16(pb); //lang_list_index
  395. stream_num= get_le16(pb);
  396. name_len= get_le16(pb);
  397. value_type= get_le16(pb);
  398. value_len= get_le32(pb);
  399. get_str16_nolen(pb, name_len, name, sizeof(name));
  400. //av_log(NULL, AV_LOG_ERROR, "%d %d %d %d %d <%s>\n", i, stream_num, name_len, value_type, value_len, name);
  401. value_num= get_le16(pb);//we should use get_value() here but it does not work 2 is le16 here but le32 elsewhere
  402. url_fskip(pb, value_len - 2);
  403. if(stream_num<128){
  404. if (!strcmp(name, "AspectRatioX")) dar[stream_num].num= value_num;
  405. else if(!strcmp(name, "AspectRatioY")) dar[stream_num].den= value_num;
  406. }
  407. }
  408. } else if (!memcmp(&g, &ext_stream_header, sizeof(GUID))) {
  409. int ext_len, payload_ext_ct, stream_ct;
  410. uint32_t ext_d, leak_rate, stream_num;
  411. int64_t pos_ex_st;
  412. pos_ex_st = url_ftell(pb);
  413. get_le64(pb); // starttime
  414. get_le64(pb); // endtime
  415. leak_rate = get_le32(pb); // leak-datarate
  416. get_le32(pb); // bucket-datasize
  417. get_le32(pb); // init-bucket-fullness
  418. get_le32(pb); // alt-leak-datarate
  419. get_le32(pb); // alt-bucket-datasize
  420. get_le32(pb); // alt-init-bucket-fullness
  421. get_le32(pb); // max-object-size
  422. get_le32(pb); // flags (reliable,seekable,no_cleanpoints?,resend-live-cleanpoints, rest of bits reserved)
  423. stream_num = get_le16(pb); // stream-num
  424. get_le16(pb); // stream-language-id-index
  425. get_le64(pb); // avg frametime in 100ns units
  426. stream_ct = get_le16(pb); //stream-name-count
  427. payload_ext_ct = get_le16(pb); //payload-extension-system-count
  428. if (stream_num < 128)
  429. bitrate[stream_num] = leak_rate;
  430. for (i=0; i<stream_ct; i++){
  431. get_le16(pb);
  432. ext_len = get_le16(pb);
  433. url_fseek(pb, ext_len, SEEK_CUR);
  434. }
  435. for (i=0; i<payload_ext_ct; i++){
  436. get_guid(pb, &g);
  437. ext_d=get_le16(pb);
  438. ext_len=get_le32(pb);
  439. url_fseek(pb, ext_len, SEEK_CUR);
  440. }
  441. // there could be a optional stream properties object to follow
  442. // if so the next iteration will pick it up
  443. } else if (!memcmp(&g, &head1_guid, sizeof(GUID))) {
  444. int v1, v2;
  445. get_guid(pb, &g);
  446. v1 = get_le32(pb);
  447. v2 = get_le16(pb);
  448. #if 0
  449. } else if (!memcmp(&g, &codec_comment_header, sizeof(GUID))) {
  450. int len, v1, n, num;
  451. char str[256], *q;
  452. char tag[16];
  453. get_guid(pb, &g);
  454. print_guid(&g);
  455. n = get_le32(pb);
  456. for(i=0;i<n;i++) {
  457. num = get_le16(pb); /* stream number */
  458. get_str16(pb, str, sizeof(str));
  459. get_str16(pb, str, sizeof(str));
  460. len = get_le16(pb);
  461. q = tag;
  462. while (len > 0) {
  463. v1 = get_byte(pb);
  464. if ((q - tag) < sizeof(tag) - 1)
  465. *q++ = v1;
  466. len--;
  467. }
  468. *q = '\0';
  469. }
  470. #endif
  471. } else if (url_feof(pb)) {
  472. goto fail;
  473. } else {
  474. url_fseek(pb, gsize - 24, SEEK_CUR);
  475. }
  476. }
  477. get_guid(pb, &g);
  478. get_le64(pb);
  479. get_byte(pb);
  480. get_byte(pb);
  481. if (url_feof(pb))
  482. goto fail;
  483. asf->data_offset = url_ftell(pb);
  484. asf->packet_size_left = 0;
  485. for(i=0; i<128; i++){
  486. int stream_num= asf->asfid2avid[i];
  487. if(stream_num>=0){
  488. AVCodecContext *codec= s->streams[stream_num]->codec;
  489. if (!codec->bit_rate)
  490. codec->bit_rate = bitrate[i];
  491. if (dar[i].num > 0 && dar[i].den > 0)
  492. av_reduce(&codec->sample_aspect_ratio.num,
  493. &codec->sample_aspect_ratio.den,
  494. dar[i].num, dar[i].den, INT_MAX);
  495. //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);
  496. }
  497. }
  498. #ifdef CONFIG_MMSH_PROTOCOL
  499. /* Give info about ourselves to the mms protocol */
  500. if(is_mms(pb))
  501. ff_mms_set_stream_selection(url_fileno(pb), s);
  502. #endif
  503. return 0;
  504. fail:
  505. for(i=0;i<s->nb_streams;i++) {
  506. AVStream *st = s->streams[i];
  507. if (st) {
  508. av_free(st->priv_data);
  509. av_free(st->codec->extradata);
  510. }
  511. av_free(st);
  512. }
  513. return -1;
  514. }
  515. #define DO_2BITS(bits, var, defval) \
  516. switch (bits & 3) \
  517. { \
  518. case 3: var = get_le32(pb); rsize += 4; break; \
  519. case 2: var = get_le16(pb); rsize += 2; break; \
  520. case 1: var = get_byte(pb); rsize++; break; \
  521. default: var = defval; break; \
  522. }
  523. /**
  524. *
  525. * @return <0 in case of an error
  526. */
  527. static int asf_get_packet(AVFormatContext *s)
  528. {
  529. ASFContext *asf = s->priv_data;
  530. ByteIOContext *pb = &s->pb;
  531. uint32_t packet_length, padsize;
  532. int rsize = 8;
  533. int c, d, e, off;
  534. off= (url_ftell(&s->pb) - s->data_offset) % asf->packet_size + 3;
  535. c=d=e=-1;
  536. while(off-- > 0){
  537. c=d; d=e;
  538. e= get_byte(pb);
  539. if(c == 0x82 && !d && !e)
  540. break;
  541. }
  542. if (c != 0x82) {
  543. if (!url_feof(pb))
  544. av_log(s, AV_LOG_ERROR, "ff asf bad header %x at:%"PRId64"\n", c, url_ftell(pb));
  545. }
  546. if ((c & 0x8f) == 0x82) {
  547. if (d || e) {
  548. if (!url_feof(pb))
  549. av_log(s, AV_LOG_ERROR, "ff asf bad non zero\n");
  550. return -1;
  551. }
  552. c= get_byte(pb);
  553. d= get_byte(pb);
  554. rsize+=3;
  555. }else{
  556. url_fseek(pb, -1, SEEK_CUR); //FIXME
  557. }
  558. asf->packet_flags = c;
  559. asf->packet_property = d;
  560. DO_2BITS(asf->packet_flags >> 5, packet_length, asf->packet_size);
  561. DO_2BITS(asf->packet_flags >> 1, padsize, 0); // sequence ignored
  562. DO_2BITS(asf->packet_flags >> 3, padsize, 0); // padding length
  563. //the following checks prevent overflows and infinite loops
  564. if(packet_length >= (1U<<29)){
  565. av_log(s, AV_LOG_ERROR, "invalid packet_length %d at:%"PRId64"\n", packet_length, url_ftell(pb));
  566. return -1;
  567. }
  568. if(padsize >= packet_length){
  569. av_log(s, AV_LOG_ERROR, "invalid padsize %d at:%"PRId64"\n", padsize, url_ftell(pb));
  570. return -1;
  571. }
  572. asf->packet_timestamp = get_le32(pb);
  573. get_le16(pb); /* duration */
  574. // rsize has at least 11 bytes which have to be present
  575. if (asf->packet_flags & 0x01) {
  576. asf->packet_segsizetype = get_byte(pb); rsize++;
  577. asf->packet_segments = asf->packet_segsizetype & 0x3f;
  578. } else {
  579. asf->packet_segments = 1;
  580. asf->packet_segsizetype = 0x80;
  581. }
  582. asf->packet_size_left = packet_length - padsize - rsize;
  583. if (packet_length < asf->hdr.min_pktsize)
  584. padsize += asf->hdr.min_pktsize - packet_length;
  585. asf->packet_padsize = padsize;
  586. #ifdef DEBUG
  587. printf("packet: size=%d padsize=%d left=%d\n", asf->packet_size, asf->packet_padsize, asf->packet_size_left);
  588. #endif
  589. return 0;
  590. }
  591. /**
  592. *
  593. * @return <0 if error
  594. */
  595. static int asf_read_frame_header(AVFormatContext *s){
  596. ASFContext *asf = s->priv_data;
  597. ByteIOContext *pb = &s->pb;
  598. int rsize = 1;
  599. int num = get_byte(pb);
  600. int64_t ts0, ts1;
  601. asf->packet_segments--;
  602. asf->packet_key_frame = num >> 7;
  603. asf->stream_index = asf->asfid2avid[num & 0x7f];
  604. // sequence should be ignored!
  605. DO_2BITS(asf->packet_property >> 4, asf->packet_seq, 0);
  606. DO_2BITS(asf->packet_property >> 2, asf->packet_frag_offset, 0);
  607. DO_2BITS(asf->packet_property, asf->packet_replic_size, 0);
  608. //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);
  609. if (asf->packet_replic_size >= 8) {
  610. asf->packet_obj_size = get_le32(pb);
  611. if(asf->packet_obj_size >= (1<<24) || asf->packet_obj_size <= 0){
  612. av_log(s, AV_LOG_ERROR, "packet_obj_size invalid\n");
  613. return -1;
  614. }
  615. asf->packet_frag_timestamp = get_le32(pb); // timestamp
  616. if(asf->packet_replic_size >= 8+38+4){
  617. // for(i=0; i<asf->packet_replic_size-8; i++)
  618. // av_log(s, AV_LOG_DEBUG, "%02X ",get_byte(pb));
  619. // av_log(s, AV_LOG_DEBUG, "\n");
  620. url_fskip(pb, 10);
  621. ts0= get_le64(pb);
  622. ts1= get_le64(pb);
  623. url_fskip(pb, 12);
  624. get_le32(pb);
  625. url_fskip(pb, asf->packet_replic_size - 8 - 38 - 4);
  626. if(ts0!= -1) asf->packet_frag_timestamp= ts0/10000;
  627. else asf->packet_frag_timestamp= AV_NOPTS_VALUE;
  628. }else
  629. url_fskip(pb, asf->packet_replic_size - 8);
  630. rsize += asf->packet_replic_size; // FIXME - check validity
  631. } else if (asf->packet_replic_size==1){
  632. // multipacket - frag_offset is begining timestamp
  633. asf->packet_time_start = asf->packet_frag_offset;
  634. asf->packet_frag_offset = 0;
  635. asf->packet_frag_timestamp = asf->packet_timestamp;
  636. asf->packet_time_delta = get_byte(pb);
  637. rsize++;
  638. }else if(asf->packet_replic_size!=0){
  639. av_log(s, AV_LOG_ERROR, "unexpected packet_replic_size of %d\n", asf->packet_replic_size);
  640. return -1;
  641. }
  642. if (asf->packet_flags & 0x01) {
  643. DO_2BITS(asf->packet_segsizetype >> 6, asf->packet_frag_size, 0); // 0 is illegal
  644. if(asf->packet_frag_size > asf->packet_size_left - rsize){
  645. av_log(s, AV_LOG_ERROR, "packet_frag_size is invalid\n");
  646. return -1;
  647. }
  648. //printf("Fragsize %d\n", asf->packet_frag_size);
  649. } else {
  650. asf->packet_frag_size = asf->packet_size_left - rsize;
  651. //printf("Using rest %d %d %d\n", asf->packet_frag_size, asf->packet_size_left, rsize);
  652. }
  653. if (asf->packet_replic_size == 1) {
  654. asf->packet_multi_size = asf->packet_frag_size;
  655. if (asf->packet_multi_size > asf->packet_size_left)
  656. return -1;
  657. }
  658. asf->packet_size_left -= rsize;
  659. //printf("___objsize____ %d %d rs:%d\n", asf->packet_obj_size, asf->packet_frag_offset, rsize);
  660. return 0;
  661. }
  662. static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
  663. {
  664. ASFContext *asf = s->priv_data;
  665. ASFStream *asf_st = 0;
  666. ByteIOContext *pb = &s->pb;
  667. //static int pc = 0;
  668. for (;;) {
  669. if(url_feof(pb))
  670. return AVERROR(EIO);
  671. if (asf->packet_size_left < FRAME_HEADER_SIZE
  672. || asf->packet_segments < 1) {
  673. //asf->packet_size_left <= asf->packet_padsize) {
  674. int ret = asf->packet_size_left + asf->packet_padsize;
  675. //printf("PacketLeftSize:%d Pad:%d Pos:%"PRId64"\n", asf->packet_size_left, asf->packet_padsize, url_ftell(pb));
  676. assert(ret>=0);
  677. /* fail safe */
  678. url_fskip(pb, ret);
  679. asf->packet_pos= url_ftell(&s->pb);
  680. if (asf->data_object_size != (uint64_t)-1 &&
  681. (asf->packet_pos - asf->data_object_offset >= asf->data_object_size))
  682. return AVERROR(EIO); /* Do not exceed the size of the data object */
  683. ret = asf_get_packet(s);
  684. //printf("READ ASF PACKET %d r:%d c:%d\n", ret, asf->packet_size_left, pc++);
  685. if (ret < 0)
  686. assert(asf->packet_size_left < FRAME_HEADER_SIZE || asf->packet_segments < 1);
  687. asf->packet_time_start = 0;
  688. continue;
  689. }
  690. if (asf->packet_time_start == 0) {
  691. if(asf_read_frame_header(s) < 0){
  692. asf->packet_segments= 0;
  693. continue;
  694. }
  695. if (asf->stream_index < 0
  696. || s->streams[asf->stream_index]->discard >= AVDISCARD_ALL
  697. || (!asf->packet_key_frame && s->streams[asf->stream_index]->discard >= AVDISCARD_NONKEY)
  698. ) {
  699. asf->packet_time_start = 0;
  700. /* unhandled packet (should not happen) */
  701. url_fskip(pb, asf->packet_frag_size);
  702. asf->packet_size_left -= asf->packet_frag_size;
  703. if(asf->stream_index < 0)
  704. av_log(s, AV_LOG_ERROR, "ff asf skip %d (unknown stream)\n", asf->packet_frag_size);
  705. continue;
  706. }
  707. asf->asf_st = s->streams[asf->stream_index]->priv_data;
  708. }
  709. asf_st = asf->asf_st;
  710. if (asf->packet_replic_size == 1) {
  711. // frag_offset is here used as the begining timestamp
  712. asf->packet_frag_timestamp = asf->packet_time_start;
  713. asf->packet_time_start += asf->packet_time_delta;
  714. asf->packet_obj_size = asf->packet_frag_size = get_byte(pb);
  715. asf->packet_size_left--;
  716. asf->packet_multi_size--;
  717. if (asf->packet_multi_size < asf->packet_obj_size)
  718. {
  719. asf->packet_time_start = 0;
  720. url_fskip(pb, asf->packet_multi_size);
  721. asf->packet_size_left -= asf->packet_multi_size;
  722. continue;
  723. }
  724. asf->packet_multi_size -= asf->packet_obj_size;
  725. //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);
  726. }
  727. if( /*asf->packet_frag_size == asf->packet_obj_size*/
  728. asf_st->frag_offset + asf->packet_frag_size <= asf_st->pkt.size
  729. && asf_st->frag_offset + asf->packet_frag_size > asf->packet_obj_size){
  730. av_log(s, AV_LOG_INFO, "ignoring invalid packet_obj_size (%d %d %d %d)\n",
  731. asf_st->frag_offset, asf->packet_frag_size,
  732. asf->packet_obj_size, asf_st->pkt.size);
  733. asf->packet_obj_size= asf_st->pkt.size;
  734. }
  735. if ( asf_st->pkt.size != asf->packet_obj_size
  736. || asf_st->frag_offset + asf->packet_frag_size > asf_st->pkt.size) { //FIXME is this condition sufficient?
  737. if(asf_st->pkt.data){
  738. av_log(s, AV_LOG_INFO, "freeing incomplete packet size %d, new %d\n", asf_st->pkt.size, asf->packet_obj_size);
  739. asf_st->frag_offset = 0;
  740. av_free_packet(&asf_st->pkt);
  741. }
  742. /* new packet */
  743. av_new_packet(&asf_st->pkt, asf->packet_obj_size);
  744. asf_st->seq = asf->packet_seq;
  745. asf_st->pkt.pts = asf->packet_frag_timestamp;
  746. asf_st->pkt.stream_index = asf->stream_index;
  747. asf_st->pkt.pos =
  748. asf_st->packet_pos= asf->packet_pos;
  749. //printf("new packet: stream:%d key:%d packet_key:%d audio:%d size:%d\n",
  750. //asf->stream_index, asf->packet_key_frame, asf_st->pkt.flags & PKT_FLAG_KEY,
  751. //s->streams[asf->stream_index]->codec->codec_type == CODEC_TYPE_AUDIO, asf->packet_obj_size);
  752. if (s->streams[asf->stream_index]->codec->codec_type == CODEC_TYPE_AUDIO)
  753. asf->packet_key_frame = 1;
  754. if (asf->packet_key_frame)
  755. asf_st->pkt.flags |= PKT_FLAG_KEY;
  756. }
  757. /* read data */
  758. //printf("READ PACKET s:%d os:%d o:%d,%d l:%d DATA:%p\n",
  759. // asf->packet_size, asf_st->pkt.size, asf->packet_frag_offset,
  760. // asf_st->frag_offset, asf->packet_frag_size, asf_st->pkt.data);
  761. asf->packet_size_left -= asf->packet_frag_size;
  762. if (asf->packet_size_left < 0)
  763. continue;
  764. if( asf->packet_frag_offset >= asf_st->pkt.size
  765. || asf->packet_frag_size > asf_st->pkt.size - asf->packet_frag_offset){
  766. av_log(s, AV_LOG_ERROR, "packet fragment position invalid %u,%u not in %u\n",
  767. asf->packet_frag_offset, asf->packet_frag_size, asf_st->pkt.size);
  768. continue;
  769. }
  770. get_buffer(pb, asf_st->pkt.data + asf->packet_frag_offset,
  771. asf->packet_frag_size);
  772. if (s->key && s->keylen == 20)
  773. ff_asfcrypt_dec(s->key, asf_st->pkt.data + asf->packet_frag_offset,
  774. asf->packet_frag_size);
  775. asf_st->frag_offset += asf->packet_frag_size;
  776. /* test if whole packet is read */
  777. if (asf_st->frag_offset == asf_st->pkt.size) {
  778. //workaround for macroshit radio DVR-MS files
  779. if( s->streams[asf->stream_index]->codec->codec_id == CODEC_ID_MPEG2VIDEO
  780. && asf_st->pkt.size > 100){
  781. int i;
  782. for(i=0; i<asf_st->pkt.size && !asf_st->pkt.data[i]; i++);
  783. if(i == asf_st->pkt.size){
  784. av_log(s, AV_LOG_DEBUG, "discarding ms fart\n");
  785. asf_st->frag_offset = 0;
  786. av_free_packet(&asf_st->pkt);
  787. continue;
  788. }
  789. }
  790. /* return packet */
  791. if (asf_st->ds_span > 1) {
  792. if(asf_st->pkt.size != asf_st->ds_packet_size * asf_st->ds_span){
  793. 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);
  794. }else{
  795. /* packet descrambling */
  796. uint8_t *newdata = av_malloc(asf_st->pkt.size);
  797. if (newdata) {
  798. int offset = 0;
  799. while (offset < asf_st->pkt.size) {
  800. int off = offset / asf_st->ds_chunk_size;
  801. int row = off / asf_st->ds_span;
  802. int col = off % asf_st->ds_span;
  803. int idx = row + col * asf_st->ds_packet_size / asf_st->ds_chunk_size;
  804. //printf("off:%d row:%d col:%d idx:%d\n", off, row, col, idx);
  805. assert(offset + asf_st->ds_chunk_size <= asf_st->pkt.size);
  806. assert(idx+1 <= asf_st->pkt.size / asf_st->ds_chunk_size);
  807. memcpy(newdata + offset,
  808. asf_st->pkt.data + idx * asf_st->ds_chunk_size,
  809. asf_st->ds_chunk_size);
  810. offset += asf_st->ds_chunk_size;
  811. }
  812. av_free(asf_st->pkt.data);
  813. asf_st->pkt.data = newdata;
  814. }
  815. }
  816. }
  817. asf_st->frag_offset = 0;
  818. *pkt= asf_st->pkt;
  819. //printf("packet %d %d\n", asf_st->pkt.size, asf->packet_frag_size);
  820. asf_st->pkt.size = 0;
  821. asf_st->pkt.data = 0;
  822. break; // packet completed
  823. }
  824. }
  825. return 0;
  826. }
  827. // Added to support seeking after packets have been read
  828. // If information is not reset, read_packet fails due to
  829. // leftover information from previous reads
  830. static void asf_reset_header(AVFormatContext *s)
  831. {
  832. ASFContext *asf = s->priv_data;
  833. ASFStream *asf_st;
  834. int i;
  835. asf->packet_nb_frames = 0;
  836. asf->packet_size_left = 0;
  837. asf->packet_segments = 0;
  838. asf->packet_flags = 0;
  839. asf->packet_property = 0;
  840. asf->packet_timestamp = 0;
  841. asf->packet_segsizetype = 0;
  842. asf->packet_segments = 0;
  843. asf->packet_seq = 0;
  844. asf->packet_replic_size = 0;
  845. asf->packet_key_frame = 0;
  846. asf->packet_padsize = 0;
  847. asf->packet_frag_offset = 0;
  848. asf->packet_frag_size = 0;
  849. asf->packet_frag_timestamp = 0;
  850. asf->packet_multi_size = 0;
  851. asf->packet_obj_size = 0;
  852. asf->packet_time_delta = 0;
  853. asf->packet_time_start = 0;
  854. for(i=0; i<s->nb_streams; i++){
  855. asf_st= s->streams[i]->priv_data;
  856. av_free_packet(&asf_st->pkt);
  857. asf_st->frag_offset=0;
  858. asf_st->seq=0;
  859. }
  860. asf->asf_st= NULL;
  861. }
  862. static int asf_read_close(AVFormatContext *s)
  863. {
  864. int i;
  865. asf_reset_header(s);
  866. for(i=0;i<s->nb_streams;i++) {
  867. AVStream *st = s->streams[i];
  868. av_free(st->priv_data);
  869. av_free(st->codec->palctrl);
  870. }
  871. return 0;
  872. }
  873. static int64_t asf_read_pts(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit)
  874. {
  875. ASFContext *asf = s->priv_data;
  876. AVPacket pkt1, *pkt = &pkt1;
  877. ASFStream *asf_st;
  878. int64_t pts;
  879. int64_t pos= *ppos;
  880. int i;
  881. int64_t start_pos[s->nb_streams];
  882. for(i=0; i<s->nb_streams; i++){
  883. start_pos[i]= pos;
  884. }
  885. pos= (pos+asf->packet_size-1-s->data_offset)/asf->packet_size*asf->packet_size+ s->data_offset;
  886. *ppos= pos;
  887. url_fseek(&s->pb, pos, SEEK_SET);
  888. //printf("asf_read_pts\n");
  889. asf_reset_header(s);
  890. for(;;){
  891. if (av_read_frame(s, pkt) < 0){
  892. av_log(s, AV_LOG_INFO, "asf_read_pts failed\n");
  893. return AV_NOPTS_VALUE;
  894. }
  895. pts= pkt->pts;
  896. av_free_packet(pkt);
  897. if(pkt->flags&PKT_FLAG_KEY){
  898. i= pkt->stream_index;
  899. asf_st= s->streams[i]->priv_data;
  900. // assert((asf_st->packet_pos - s->data_offset) % asf->packet_size == 0);
  901. pos= asf_st->packet_pos;
  902. av_add_index_entry(s->streams[i], pos, pts, pkt->size, pos - start_pos[i] + 1, AVINDEX_KEYFRAME);
  903. start_pos[i]= asf_st->packet_pos + 1;
  904. if(pkt->stream_index == stream_index)
  905. break;
  906. }
  907. }
  908. *ppos= pos;
  909. //printf("found keyframe at %"PRId64" stream %d stamp:%"PRId64"\n", *ppos, stream_index, pts);
  910. return pts;
  911. }
  912. static void asf_build_simple_index(AVFormatContext *s, int stream_index)
  913. {
  914. GUID g;
  915. ASFContext *asf = s->priv_data;
  916. int64_t gsize, itime;
  917. int64_t pos, current_pos, index_pts;
  918. int i;
  919. int pct,ict;
  920. current_pos = url_ftell(&s->pb);
  921. url_fseek(&s->pb, asf->data_object_offset + asf->data_object_size, SEEK_SET);
  922. get_guid(&s->pb, &g);
  923. if (!memcmp(&g, &index_guid, sizeof(GUID))) {
  924. gsize = get_le64(&s->pb);
  925. get_guid(&s->pb, &g);
  926. itime=get_le64(&s->pb);
  927. pct=get_le32(&s->pb);
  928. ict=get_le32(&s->pb);
  929. av_log(NULL, AV_LOG_DEBUG, "itime:0x%"PRIx64", pct:%d, ict:%d\n",itime,pct,ict);
  930. for (i=0;i<ict;i++){
  931. int pktnum=get_le32(&s->pb);
  932. int pktct =get_le16(&s->pb);
  933. av_log(NULL, AV_LOG_DEBUG, "pktnum:%d, pktct:%d\n", pktnum, pktct);
  934. pos=s->data_offset + asf->packet_size*(int64_t)pktnum;
  935. index_pts=av_rescale(itime, i, 10000);
  936. av_add_index_entry(s->streams[stream_index], pos, index_pts, asf->packet_size, 0, AVINDEX_KEYFRAME);
  937. }
  938. asf->index_read= 1;
  939. }
  940. url_fseek(&s->pb, current_pos, SEEK_SET);
  941. }
  942. static int asf_read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
  943. {
  944. ASFContext *asf = s->priv_data;
  945. AVStream *st = s->streams[stream_index];
  946. int64_t pos;
  947. int index;
  948. if (asf->packet_size <= 0)
  949. return -1;
  950. if (!asf->index_read)
  951. asf_build_simple_index(s, stream_index);
  952. if(!(asf->index_read && st->index_entries)){
  953. if(av_seek_frame_binary(s, stream_index, pts, flags)<0)
  954. return -1;
  955. }else{
  956. index= av_index_search_timestamp(st, pts, flags);
  957. if(index<0)
  958. return -1;
  959. /* find the position */
  960. pos = st->index_entries[index].pos;
  961. pts = st->index_entries[index].timestamp;
  962. // various attempts to find key frame have failed so far
  963. // asf_reset_header(s);
  964. // url_fseek(&s->pb, pos, SEEK_SET);
  965. // key_pos = pos;
  966. // for(i=0;i<16;i++){
  967. // pos = url_ftell(&s->pb);
  968. // if (av_read_frame(s, &pkt) < 0){
  969. // av_log(s, AV_LOG_INFO, "seek failed\n");
  970. // return -1;
  971. // }
  972. // asf_st = s->streams[stream_index]->priv_data;
  973. // pos += st->parser->frame_offset;
  974. //
  975. // if (pkt.size > b) {
  976. // b = pkt.size;
  977. // key_pos = pos;
  978. // }
  979. //
  980. // av_free_packet(&pkt);
  981. // }
  982. /* do the seek */
  983. av_log(NULL, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos);
  984. url_fseek(&s->pb, pos, SEEK_SET);
  985. }
  986. asf_reset_header(s);
  987. return 0;
  988. }
  989. AVInputFormat asf_demuxer = {
  990. "asf",
  991. "asf format",
  992. sizeof(ASFContext),
  993. asf_probe,
  994. asf_read_header,
  995. asf_read_packet,
  996. asf_read_close,
  997. asf_read_seek,
  998. asf_read_pts,
  999. };