asfdec.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295
  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. //#define DEBUG
  22. #include "libavutil/bswap.h"
  23. #include "libavutil/common.h"
  24. #include "libavutil/avstring.h"
  25. #include "libavutil/dict.h"
  26. #include "libavutil/mathematics.h"
  27. #include "libavcodec/mpegaudio.h"
  28. #include "avformat.h"
  29. #include "avio_internal.h"
  30. #include "riff.h"
  31. #include "asf.h"
  32. #include "asfcrypt.h"
  33. #include "avlanguage.h"
  34. typedef struct {
  35. int asfid2avid[128]; ///< conversion table from asf ID 2 AVStream ID
  36. ASFStream streams[128]; ///< it's max number and it's not that big
  37. uint32_t stream_bitrates[128]; ///< max number of streams, bitrate for each (for streaming)
  38. AVRational dar[128];
  39. char stream_languages[128][6]; ///< max number of streams, language for each (RFC1766, e.g. en-US)
  40. /* non streamed additonnal info */
  41. /* packet filling */
  42. int packet_size_left;
  43. /* only for reading */
  44. uint64_t data_offset; ///< beginning of the first data packet
  45. uint64_t data_object_offset; ///< data object offset (excl. GUID & size)
  46. uint64_t data_object_size; ///< size of the data object
  47. int index_read;
  48. ASFMainHeader hdr;
  49. int packet_flags;
  50. int packet_property;
  51. int packet_timestamp;
  52. int packet_segsizetype;
  53. int packet_segments;
  54. int packet_seq;
  55. int packet_replic_size;
  56. int packet_key_frame;
  57. int packet_padsize;
  58. unsigned int packet_frag_offset;
  59. unsigned int packet_frag_size;
  60. int64_t packet_frag_timestamp;
  61. int packet_multi_size;
  62. int packet_obj_size;
  63. int packet_time_delta;
  64. int packet_time_start;
  65. int64_t packet_pos;
  66. int stream_index;
  67. ASFStream* asf_st; ///< currently decoded stream
  68. } ASFContext;
  69. #undef NDEBUG
  70. #include <assert.h>
  71. #define ASF_MAX_STREAMS 127
  72. #define FRAME_HEADER_SIZE 17
  73. // Fix Me! FRAME_HEADER_SIZE may be different.
  74. static const ff_asf_guid index_guid = {
  75. 0x90, 0x08, 0x00, 0x33, 0xb1, 0xe5, 0xcf, 0x11, 0x89, 0xf4, 0x00, 0xa0, 0xc9, 0x03, 0x49, 0xcb
  76. };
  77. #ifdef DEBUG
  78. static const ff_asf_guid stream_bitrate_guid = { /* (http://get.to/sdp) */
  79. 0xce, 0x75, 0xf8, 0x7b, 0x8d, 0x46, 0xd1, 0x11, 0x8d, 0x82, 0x00, 0x60, 0x97, 0xc9, 0xa2, 0xb2
  80. };
  81. #define PRINT_IF_GUID(g,cmp) \
  82. if (!ff_guidcmp(g, &cmp)) \
  83. av_dlog(NULL, "(GUID: %s) ", #cmp)
  84. static void print_guid(const ff_asf_guid *g)
  85. {
  86. int i;
  87. PRINT_IF_GUID(g, ff_asf_header);
  88. else PRINT_IF_GUID(g, ff_asf_file_header);
  89. else PRINT_IF_GUID(g, ff_asf_stream_header);
  90. else PRINT_IF_GUID(g, ff_asf_audio_stream);
  91. else PRINT_IF_GUID(g, ff_asf_audio_conceal_none);
  92. else PRINT_IF_GUID(g, ff_asf_video_stream);
  93. else PRINT_IF_GUID(g, ff_asf_video_conceal_none);
  94. else PRINT_IF_GUID(g, ff_asf_command_stream);
  95. else PRINT_IF_GUID(g, ff_asf_comment_header);
  96. else PRINT_IF_GUID(g, ff_asf_codec_comment_header);
  97. else PRINT_IF_GUID(g, ff_asf_codec_comment1_header);
  98. else PRINT_IF_GUID(g, ff_asf_data_header);
  99. else PRINT_IF_GUID(g, index_guid);
  100. else PRINT_IF_GUID(g, ff_asf_head1_guid);
  101. else PRINT_IF_GUID(g, ff_asf_head2_guid);
  102. else PRINT_IF_GUID(g, ff_asf_my_guid);
  103. else PRINT_IF_GUID(g, ff_asf_ext_stream_header);
  104. else PRINT_IF_GUID(g, ff_asf_extended_content_header);
  105. else PRINT_IF_GUID(g, ff_asf_ext_stream_embed_stream_header);
  106. else PRINT_IF_GUID(g, ff_asf_ext_stream_audio_stream);
  107. else PRINT_IF_GUID(g, ff_asf_metadata_header);
  108. else PRINT_IF_GUID(g, ff_asf_marker_header);
  109. else PRINT_IF_GUID(g, stream_bitrate_guid);
  110. else PRINT_IF_GUID(g, ff_asf_language_guid);
  111. else
  112. av_dlog(NULL, "(GUID: unknown) ");
  113. for(i=0;i<16;i++)
  114. av_dlog(NULL, " 0x%02x,", (*g)[i]);
  115. av_dlog(NULL, "}\n");
  116. }
  117. #undef PRINT_IF_GUID
  118. #else
  119. #define print_guid(g)
  120. #endif
  121. static int asf_probe(AVProbeData *pd)
  122. {
  123. /* check file header */
  124. if (!ff_guidcmp(pd->buf, &ff_asf_header))
  125. return AVPROBE_SCORE_MAX;
  126. else
  127. return 0;
  128. }
  129. static int get_value(AVIOContext *pb, int type){
  130. switch(type){
  131. case 2: return avio_rl32(pb);
  132. case 3: return avio_rl32(pb);
  133. case 4: return avio_rl64(pb);
  134. case 5: return avio_rl16(pb);
  135. default:return INT_MIN;
  136. }
  137. }
  138. static void get_tag(AVFormatContext *s, const char *key, int type, int len)
  139. {
  140. char *value;
  141. int64_t off = avio_tell(s->pb);
  142. if ((unsigned)len >= (UINT_MAX - 1)/2)
  143. return;
  144. value = av_malloc(2*len+1);
  145. if (!value)
  146. goto finish;
  147. if (type == 0) { // UTF16-LE
  148. avio_get_str16le(s->pb, len, value, 2*len + 1);
  149. } else if (type > 1 && type <= 5) { // boolean or DWORD or QWORD or WORD
  150. uint64_t num = get_value(s->pb, type);
  151. snprintf(value, len, "%"PRIu64, num);
  152. } else {
  153. av_log(s, AV_LOG_DEBUG, "Unsupported value type %d in tag %s.\n", type, key);
  154. goto finish;
  155. }
  156. if (*value)
  157. av_dict_set(&s->metadata, key, value, 0);
  158. finish:
  159. av_freep(&value);
  160. avio_seek(s->pb, off + len, SEEK_SET);
  161. }
  162. static int asf_read_file_properties(AVFormatContext *s, int64_t size)
  163. {
  164. ASFContext *asf = s->priv_data;
  165. AVIOContext *pb = s->pb;
  166. ff_get_guid(pb, &asf->hdr.guid);
  167. asf->hdr.file_size = avio_rl64(pb);
  168. asf->hdr.create_time = avio_rl64(pb);
  169. avio_rl64(pb); /* number of packets */
  170. asf->hdr.play_time = avio_rl64(pb);
  171. asf->hdr.send_time = avio_rl64(pb);
  172. asf->hdr.preroll = avio_rl32(pb);
  173. asf->hdr.ignore = avio_rl32(pb);
  174. asf->hdr.flags = avio_rl32(pb);
  175. asf->hdr.min_pktsize = avio_rl32(pb);
  176. asf->hdr.max_pktsize = avio_rl32(pb);
  177. asf->hdr.max_bitrate = avio_rl32(pb);
  178. s->packet_size = asf->hdr.max_pktsize;
  179. return 0;
  180. }
  181. static int asf_read_stream_properties(AVFormatContext *s, int64_t size)
  182. {
  183. ASFContext *asf = s->priv_data;
  184. AVIOContext *pb = s->pb;
  185. AVStream *st;
  186. ASFStream *asf_st;
  187. ff_asf_guid g;
  188. enum AVMediaType type;
  189. int type_specific_size, sizeX;
  190. unsigned int tag1;
  191. int64_t pos1, pos2, start_time;
  192. int test_for_ext_stream_audio, is_dvr_ms_audio=0;
  193. if (s->nb_streams == ASF_MAX_STREAMS) {
  194. av_log(s, AV_LOG_ERROR, "too many streams\n");
  195. return AVERROR(EINVAL);
  196. }
  197. pos1 = avio_tell(pb);
  198. st = av_new_stream(s, 0);
  199. if (!st)
  200. return AVERROR(ENOMEM);
  201. av_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
  202. asf_st = av_mallocz(sizeof(ASFStream));
  203. if (!asf_st)
  204. return AVERROR(ENOMEM);
  205. st->priv_data = asf_st;
  206. start_time = asf->hdr.preroll;
  207. asf_st->stream_language_index = 128; // invalid stream index means no language info
  208. if(!(asf->hdr.flags & 0x01)) { // if we aren't streaming...
  209. st->duration = asf->hdr.play_time /
  210. (10000000 / 1000) - start_time;
  211. }
  212. ff_get_guid(pb, &g);
  213. test_for_ext_stream_audio = 0;
  214. if (!ff_guidcmp(&g, &ff_asf_audio_stream)) {
  215. type = AVMEDIA_TYPE_AUDIO;
  216. } else if (!ff_guidcmp(&g, &ff_asf_video_stream)) {
  217. type = AVMEDIA_TYPE_VIDEO;
  218. } else if (!ff_guidcmp(&g, &ff_asf_jfif_media)) {
  219. type = AVMEDIA_TYPE_VIDEO;
  220. st->codec->codec_id = CODEC_ID_MJPEG;
  221. } else if (!ff_guidcmp(&g, &ff_asf_command_stream)) {
  222. type = AVMEDIA_TYPE_DATA;
  223. } else if (!ff_guidcmp(&g, &ff_asf_ext_stream_embed_stream_header)) {
  224. test_for_ext_stream_audio = 1;
  225. type = AVMEDIA_TYPE_UNKNOWN;
  226. } else {
  227. return -1;
  228. }
  229. ff_get_guid(pb, &g);
  230. avio_skip(pb, 8); /* total_size */
  231. type_specific_size = avio_rl32(pb);
  232. avio_rl32(pb);
  233. st->id = avio_rl16(pb) & 0x7f; /* stream id */
  234. // mapping of asf ID to AV stream ID;
  235. asf->asfid2avid[st->id] = s->nb_streams - 1;
  236. avio_rl32(pb);
  237. if (test_for_ext_stream_audio) {
  238. ff_get_guid(pb, &g);
  239. if (!ff_guidcmp(&g, &ff_asf_ext_stream_audio_stream)) {
  240. type = AVMEDIA_TYPE_AUDIO;
  241. is_dvr_ms_audio=1;
  242. ff_get_guid(pb, &g);
  243. avio_rl32(pb);
  244. avio_rl32(pb);
  245. avio_rl32(pb);
  246. ff_get_guid(pb, &g);
  247. avio_rl32(pb);
  248. }
  249. }
  250. st->codec->codec_type = type;
  251. if (type == AVMEDIA_TYPE_AUDIO) {
  252. int ret = ff_get_wav_header(pb, st->codec, type_specific_size);
  253. if (ret < 0)
  254. return ret;
  255. if (is_dvr_ms_audio) {
  256. // codec_id and codec_tag are unreliable in dvr_ms
  257. // files. Set them later by probing stream.
  258. st->request_probe= 1;
  259. st->codec->codec_tag = 0;
  260. }
  261. if (st->codec->codec_id == CODEC_ID_AAC) {
  262. st->need_parsing = AVSTREAM_PARSE_NONE;
  263. } else {
  264. st->need_parsing = AVSTREAM_PARSE_FULL;
  265. }
  266. /* We have to init the frame size at some point .... */
  267. pos2 = avio_tell(pb);
  268. if (size >= (pos2 + 8 - pos1 + 24)) {
  269. asf_st->ds_span = avio_r8(pb);
  270. asf_st->ds_packet_size = avio_rl16(pb);
  271. asf_st->ds_chunk_size = avio_rl16(pb);
  272. avio_rl16(pb); //ds_data_size
  273. avio_r8(pb); //ds_silence_data
  274. }
  275. //printf("Descrambling: ps:%d cs:%d ds:%d s:%d sd:%d\n",
  276. // asf_st->ds_packet_size, asf_st->ds_chunk_size,
  277. // asf_st->ds_data_size, asf_st->ds_span, asf_st->ds_silence_data);
  278. if (asf_st->ds_span > 1) {
  279. if (!asf_st->ds_chunk_size
  280. || (asf_st->ds_packet_size/asf_st->ds_chunk_size <= 1)
  281. || asf_st->ds_packet_size % asf_st->ds_chunk_size)
  282. asf_st->ds_span = 0; // disable descrambling
  283. }
  284. switch (st->codec->codec_id) {
  285. case CODEC_ID_MP3:
  286. st->codec->frame_size = MPA_FRAME_SIZE;
  287. break;
  288. case CODEC_ID_PCM_S16LE:
  289. case CODEC_ID_PCM_S16BE:
  290. case CODEC_ID_PCM_U16LE:
  291. case CODEC_ID_PCM_U16BE:
  292. case CODEC_ID_PCM_S8:
  293. case CODEC_ID_PCM_U8:
  294. case CODEC_ID_PCM_ALAW:
  295. case CODEC_ID_PCM_MULAW:
  296. st->codec->frame_size = 1;
  297. break;
  298. default:
  299. /* This is probably wrong, but it prevents a crash later */
  300. st->codec->frame_size = 1;
  301. break;
  302. }
  303. } else if (type == AVMEDIA_TYPE_VIDEO &&
  304. size - (avio_tell(pb) - pos1 + 24) >= 51) {
  305. avio_rl32(pb);
  306. avio_rl32(pb);
  307. avio_r8(pb);
  308. avio_rl16(pb); /* size */
  309. sizeX= avio_rl32(pb); /* size */
  310. st->codec->width = avio_rl32(pb);
  311. st->codec->height = avio_rl32(pb);
  312. /* not available for asf */
  313. avio_rl16(pb); /* panes */
  314. st->codec->bits_per_coded_sample = avio_rl16(pb); /* depth */
  315. tag1 = avio_rl32(pb);
  316. avio_skip(pb, 20);
  317. // av_log(s, AV_LOG_DEBUG, "size:%d tsize:%d sizeX:%d\n", size, total_size, sizeX);
  318. if (sizeX > 40) {
  319. st->codec->extradata_size = sizeX - 40;
  320. st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  321. avio_read(pb, st->codec->extradata, st->codec->extradata_size);
  322. }
  323. /* Extract palette from extradata if bpp <= 8 */
  324. /* This code assumes that extradata contains only palette */
  325. /* This is true for all paletted codecs implemented in ffmpeg */
  326. if (st->codec->extradata_size && (st->codec->bits_per_coded_sample <= 8)) {
  327. int av_unused i;
  328. st->codec->palctrl = av_mallocz(sizeof(AVPaletteControl));
  329. #if HAVE_BIGENDIAN
  330. for (i = 0; i < FFMIN(st->codec->extradata_size, AVPALETTE_SIZE)/4; i++)
  331. st->codec->palctrl->palette[i] = av_bswap32(((uint32_t*)st->codec->extradata)[i]);
  332. #else
  333. memcpy(st->codec->palctrl->palette, st->codec->extradata,
  334. FFMIN(st->codec->extradata_size, AVPALETTE_SIZE));
  335. #endif
  336. st->codec->palctrl->palette_changed = 1;
  337. }
  338. st->codec->codec_tag = tag1;
  339. st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags, tag1);
  340. if(tag1 == MKTAG('D', 'V', 'R', ' ')){
  341. st->need_parsing = AVSTREAM_PARSE_FULL;
  342. // issue658 containse wrong w/h and MS even puts a fake seq header with wrong w/h in extradata while a correct one is in te stream. maximum lameness
  343. st->codec->width =
  344. st->codec->height = 0;
  345. av_freep(&st->codec->extradata);
  346. st->codec->extradata_size=0;
  347. }
  348. if(st->codec->codec_id == CODEC_ID_H264)
  349. st->need_parsing = AVSTREAM_PARSE_FULL_ONCE;
  350. }
  351. pos2 = avio_tell(pb);
  352. avio_skip(pb, size - (pos2 - pos1 + 24));
  353. return 0;
  354. }
  355. static int asf_read_ext_stream_properties(AVFormatContext *s, int64_t size)
  356. {
  357. ASFContext *asf = s->priv_data;
  358. AVIOContext *pb = s->pb;
  359. ff_asf_guid g;
  360. int ext_len, payload_ext_ct, stream_ct, i;
  361. uint32_t leak_rate, stream_num;
  362. unsigned int stream_languageid_index;
  363. avio_rl64(pb); // starttime
  364. avio_rl64(pb); // endtime
  365. leak_rate = avio_rl32(pb); // leak-datarate
  366. avio_rl32(pb); // bucket-datasize
  367. avio_rl32(pb); // init-bucket-fullness
  368. avio_rl32(pb); // alt-leak-datarate
  369. avio_rl32(pb); // alt-bucket-datasize
  370. avio_rl32(pb); // alt-init-bucket-fullness
  371. avio_rl32(pb); // max-object-size
  372. avio_rl32(pb); // flags (reliable,seekable,no_cleanpoints?,resend-live-cleanpoints, rest of bits reserved)
  373. stream_num = avio_rl16(pb); // stream-num
  374. stream_languageid_index = avio_rl16(pb); // stream-language-id-index
  375. if (stream_num < 128)
  376. asf->streams[stream_num].stream_language_index = stream_languageid_index;
  377. avio_rl64(pb); // avg frametime in 100ns units
  378. stream_ct = avio_rl16(pb); //stream-name-count
  379. payload_ext_ct = avio_rl16(pb); //payload-extension-system-count
  380. if (stream_num < 128)
  381. asf->stream_bitrates[stream_num] = leak_rate;
  382. for (i=0; i<stream_ct; i++){
  383. avio_rl16(pb);
  384. ext_len = avio_rl16(pb);
  385. avio_skip(pb, ext_len);
  386. }
  387. for (i=0; i<payload_ext_ct; i++){
  388. ff_get_guid(pb, &g);
  389. avio_skip(pb, 2);
  390. ext_len=avio_rl32(pb);
  391. avio_skip(pb, ext_len);
  392. }
  393. return 0;
  394. }
  395. static int asf_read_content_desc(AVFormatContext *s, int64_t size)
  396. {
  397. AVIOContext *pb = s->pb;
  398. int len1, len2, len3, len4, len5;
  399. len1 = avio_rl16(pb);
  400. len2 = avio_rl16(pb);
  401. len3 = avio_rl16(pb);
  402. len4 = avio_rl16(pb);
  403. len5 = avio_rl16(pb);
  404. get_tag(s, "title" , 0, len1);
  405. get_tag(s, "author" , 0, len2);
  406. get_tag(s, "copyright", 0, len3);
  407. get_tag(s, "comment" , 0, len4);
  408. avio_skip(pb, len5);
  409. return 0;
  410. }
  411. static int asf_read_ext_content_desc(AVFormatContext *s, int64_t size)
  412. {
  413. AVIOContext *pb = s->pb;
  414. ASFContext *asf = s->priv_data;
  415. int desc_count, i, ret;
  416. desc_count = avio_rl16(pb);
  417. for(i=0;i<desc_count;i++) {
  418. int name_len,value_type,value_len;
  419. char name[1024];
  420. name_len = avio_rl16(pb);
  421. if (name_len%2) // must be even, broken lavf versions wrote len-1
  422. name_len += 1;
  423. if ((ret = avio_get_str16le(pb, name_len, name, sizeof(name))) < name_len)
  424. avio_skip(pb, name_len - ret);
  425. value_type = avio_rl16(pb);
  426. value_len = avio_rl16(pb);
  427. if (!value_type && value_len%2)
  428. value_len += 1;
  429. /**
  430. * My sample has that stream set to 0 maybe that mean the container.
  431. * Asf stream count start at 1. I am using 0 to the container value since it's unused
  432. */
  433. if (!strcmp(name, "AspectRatioX")){
  434. asf->dar[0].num= get_value(s->pb, value_type);
  435. } else if(!strcmp(name, "AspectRatioY")){
  436. asf->dar[0].den= get_value(s->pb, value_type);
  437. } else
  438. get_tag(s, name, value_type, value_len);
  439. }
  440. return 0;
  441. }
  442. static int asf_read_language_list(AVFormatContext *s, int64_t size)
  443. {
  444. AVIOContext *pb = s->pb;
  445. ASFContext *asf = s->priv_data;
  446. int j, ret;
  447. int stream_count = avio_rl16(pb);
  448. for(j = 0; j < stream_count; j++) {
  449. char lang[6];
  450. unsigned int lang_len = avio_r8(pb);
  451. if ((ret = avio_get_str16le(pb, lang_len, lang, sizeof(lang))) < lang_len)
  452. avio_skip(pb, lang_len - ret);
  453. if (j < 128)
  454. av_strlcpy(asf->stream_languages[j], lang, sizeof(*asf->stream_languages));
  455. }
  456. return 0;
  457. }
  458. static int asf_read_metadata(AVFormatContext *s, int64_t size)
  459. {
  460. AVIOContext *pb = s->pb;
  461. ASFContext *asf = s->priv_data;
  462. int n, stream_num, name_len, value_len, value_num;
  463. int ret, i;
  464. n = avio_rl16(pb);
  465. for(i=0;i<n;i++) {
  466. char name[1024];
  467. avio_rl16(pb); //lang_list_index
  468. stream_num= avio_rl16(pb);
  469. name_len= avio_rl16(pb);
  470. avio_skip(pb, 2); /* value_type */
  471. value_len= avio_rl32(pb);
  472. if ((ret = avio_get_str16le(pb, name_len, name, sizeof(name))) < name_len)
  473. avio_skip(pb, name_len - ret);
  474. //av_log(s, AV_LOG_ERROR, "%d %d %d %d %d <%s>\n", i, stream_num, name_len, value_type, value_len, name);
  475. value_num= avio_rl16(pb);//we should use get_value() here but it does not work 2 is le16 here but le32 elsewhere
  476. avio_skip(pb, value_len - 2);
  477. if(stream_num<128){
  478. if (!strcmp(name, "AspectRatioX")) asf->dar[stream_num].num= value_num;
  479. else if(!strcmp(name, "AspectRatioY")) asf->dar[stream_num].den= value_num;
  480. }
  481. }
  482. return 0;
  483. }
  484. static int asf_read_marker(AVFormatContext *s, int64_t size)
  485. {
  486. AVIOContext *pb = s->pb;
  487. int i, count, name_len, ret;
  488. char name[1024];
  489. avio_rl64(pb); // reserved 16 bytes
  490. avio_rl64(pb); // ...
  491. count = avio_rl32(pb); // markers count
  492. avio_rl16(pb); // reserved 2 bytes
  493. name_len = avio_rl16(pb); // name length
  494. for(i=0;i<name_len;i++){
  495. avio_r8(pb); // skip the name
  496. }
  497. for(i=0;i<count;i++){
  498. int64_t pres_time;
  499. int name_len;
  500. avio_rl64(pb); // offset, 8 bytes
  501. pres_time = avio_rl64(pb); // presentation time
  502. avio_rl16(pb); // entry length
  503. avio_rl32(pb); // send time
  504. avio_rl32(pb); // flags
  505. name_len = avio_rl32(pb); // name length
  506. if ((ret = avio_get_str16le(pb, name_len * 2, name, sizeof(name))) < name_len)
  507. avio_skip(pb, name_len - ret);
  508. ff_new_chapter(s, i, (AVRational){1, 10000000}, pres_time, AV_NOPTS_VALUE, name );
  509. }
  510. return 0;
  511. }
  512. static int asf_read_header(AVFormatContext *s, AVFormatParameters *ap)
  513. {
  514. ASFContext *asf = s->priv_data;
  515. ff_asf_guid g;
  516. AVIOContext *pb = s->pb;
  517. int i;
  518. int64_t gsize;
  519. ff_get_guid(pb, &g);
  520. if (ff_guidcmp(&g, &ff_asf_header))
  521. return -1;
  522. avio_rl64(pb);
  523. avio_rl32(pb);
  524. avio_r8(pb);
  525. avio_r8(pb);
  526. memset(&asf->asfid2avid, -1, sizeof(asf->asfid2avid));
  527. for(;;) {
  528. uint64_t gpos= avio_tell(pb);
  529. ff_get_guid(pb, &g);
  530. gsize = avio_rl64(pb);
  531. av_dlog(s, "%08"PRIx64": ", gpos);
  532. print_guid(&g);
  533. av_dlog(s, " size=0x%"PRIx64"\n", gsize);
  534. if (!ff_guidcmp(&g, &ff_asf_data_header)) {
  535. asf->data_object_offset = avio_tell(pb);
  536. // if not streaming, gsize is not unlimited (how?), and there is enough space in the file..
  537. if (!(asf->hdr.flags & 0x01) && gsize >= 100) {
  538. asf->data_object_size = gsize - 24;
  539. } else {
  540. asf->data_object_size = (uint64_t)-1;
  541. }
  542. break;
  543. }
  544. if (gsize < 24)
  545. return -1;
  546. if (!ff_guidcmp(&g, &ff_asf_file_header)) {
  547. asf_read_file_properties(s, gsize);
  548. } else if (!ff_guidcmp(&g, &ff_asf_stream_header)) {
  549. asf_read_stream_properties(s, gsize);
  550. } else if (!ff_guidcmp(&g, &ff_asf_comment_header)) {
  551. asf_read_content_desc(s, gsize);
  552. } else if (!ff_guidcmp(&g, &ff_asf_language_guid)) {
  553. asf_read_language_list(s, gsize);
  554. } else if (!ff_guidcmp(&g, &ff_asf_extended_content_header)) {
  555. asf_read_ext_content_desc(s, gsize);
  556. } else if (!ff_guidcmp(&g, &ff_asf_metadata_header)) {
  557. asf_read_metadata(s, gsize);
  558. } else if (!ff_guidcmp(&g, &ff_asf_ext_stream_header)) {
  559. asf_read_ext_stream_properties(s, gsize);
  560. // there could be a optional stream properties object to follow
  561. // if so the next iteration will pick it up
  562. continue;
  563. } else if (!ff_guidcmp(&g, &ff_asf_head1_guid)) {
  564. ff_get_guid(pb, &g);
  565. avio_skip(pb, 6);
  566. continue;
  567. } else if (!ff_guidcmp(&g, &ff_asf_marker_header)) {
  568. asf_read_marker(s, gsize);
  569. } else if (url_feof(pb)) {
  570. return -1;
  571. } else {
  572. if (!s->keylen) {
  573. if (!ff_guidcmp(&g, &ff_asf_content_encryption)) {
  574. av_log(s, AV_LOG_WARNING, "DRM protected stream detected, decoding will likely fail!\n");
  575. } else if (!ff_guidcmp(&g, &ff_asf_ext_content_encryption)) {
  576. av_log(s, AV_LOG_WARNING, "Ext DRM protected stream detected, decoding will likely fail!\n");
  577. } else if (!ff_guidcmp(&g, &ff_asf_digital_signature)) {
  578. av_log(s, AV_LOG_WARNING, "Digital signature detected, decoding will likely fail!\n");
  579. }
  580. }
  581. }
  582. if(avio_tell(pb) != gpos + gsize)
  583. av_log(s, AV_LOG_DEBUG, "gpos mismatch our pos=%"PRIu64", end=%"PRIu64"\n", avio_tell(pb)-gpos, gsize);
  584. avio_seek(pb, gpos + gsize, SEEK_SET);
  585. }
  586. ff_get_guid(pb, &g);
  587. avio_rl64(pb);
  588. avio_r8(pb);
  589. avio_r8(pb);
  590. if (url_feof(pb))
  591. return -1;
  592. asf->data_offset = avio_tell(pb);
  593. asf->packet_size_left = 0;
  594. for(i=0; i<128; i++){
  595. int stream_num= asf->asfid2avid[i];
  596. if(stream_num>=0){
  597. AVStream *st = s->streams[stream_num];
  598. if (!st->codec->bit_rate)
  599. st->codec->bit_rate = asf->stream_bitrates[i];
  600. if (asf->dar[i].num > 0 && asf->dar[i].den > 0){
  601. av_reduce(&st->sample_aspect_ratio.num,
  602. &st->sample_aspect_ratio.den,
  603. asf->dar[i].num, asf->dar[i].den, INT_MAX);
  604. } else if ((asf->dar[0].num > 0) && (asf->dar[0].den > 0) && (st->codec->codec_type==AVMEDIA_TYPE_VIDEO)) // Use ASF container value if the stream doesn't AR set.
  605. av_reduce(&st->sample_aspect_ratio.num,
  606. &st->sample_aspect_ratio.den,
  607. asf->dar[0].num, asf->dar[0].den, INT_MAX);
  608. //av_log(s, AV_LOG_INFO, "i=%d, st->codec->codec_type:%d, dar %d:%d sar=%d:%d\n", i, st->codec->codec_type, dar[i].num, dar[i].den, st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
  609. // copy and convert language codes to the frontend
  610. if (asf->streams[i].stream_language_index < 128) {
  611. const char *rfc1766 = asf->stream_languages[asf->streams[i].stream_language_index];
  612. if (rfc1766 && strlen(rfc1766) > 1) {
  613. const char primary_tag[3] = { rfc1766[0], rfc1766[1], '\0' }; // ignore country code if any
  614. const char *iso6392 = av_convert_lang_to(primary_tag, AV_LANG_ISO639_2_BIBL);
  615. if (iso6392)
  616. av_dict_set(&st->metadata, "language", iso6392, 0);
  617. }
  618. }
  619. }
  620. }
  621. ff_metadata_conv(&s->metadata, NULL, ff_asf_metadata_conv);
  622. return 0;
  623. }
  624. #define DO_2BITS(bits, var, defval) \
  625. switch (bits & 3) \
  626. { \
  627. case 3: var = avio_rl32(pb); rsize += 4; break; \
  628. case 2: var = avio_rl16(pb); rsize += 2; break; \
  629. case 1: var = avio_r8(pb); rsize++; break; \
  630. default: var = defval; break; \
  631. }
  632. /**
  633. * Load a single ASF packet into the demuxer.
  634. * @param s demux context
  635. * @param pb context to read data from
  636. * @return 0 on success, <0 on error
  637. */
  638. static int ff_asf_get_packet(AVFormatContext *s, AVIOContext *pb)
  639. {
  640. ASFContext *asf = s->priv_data;
  641. uint32_t packet_length, padsize;
  642. int rsize = 8;
  643. int c, d, e, off;
  644. // if we do not know packet size, allow skipping up to 32 kB
  645. off= 32768;
  646. if (s->packet_size > 0)
  647. off= (avio_tell(pb) - s->data_offset) % s->packet_size + 3;
  648. c=d=e=-1;
  649. while(off-- > 0){
  650. c=d; d=e;
  651. e= avio_r8(pb);
  652. if(c == 0x82 && !d && !e)
  653. break;
  654. }
  655. if (c != 0x82) {
  656. /**
  657. * This code allows handling of -EAGAIN at packet boundaries (i.e.
  658. * if the packet sync code above triggers -EAGAIN). This does not
  659. * imply complete -EAGAIN handling support at random positions in
  660. * the stream.
  661. */
  662. if (pb->error == AVERROR(EAGAIN))
  663. return AVERROR(EAGAIN);
  664. if (!url_feof(pb))
  665. av_log(s, AV_LOG_ERROR, "ff asf bad header %x at:%"PRId64"\n", c, avio_tell(pb));
  666. }
  667. if ((c & 0x8f) == 0x82) {
  668. if (d || e) {
  669. if (!url_feof(pb))
  670. av_log(s, AV_LOG_ERROR, "ff asf bad non zero\n");
  671. return -1;
  672. }
  673. c= avio_r8(pb);
  674. d= avio_r8(pb);
  675. rsize+=3;
  676. }else{
  677. avio_seek(pb, -1, SEEK_CUR); //FIXME
  678. }
  679. asf->packet_flags = c;
  680. asf->packet_property = d;
  681. DO_2BITS(asf->packet_flags >> 5, packet_length, s->packet_size);
  682. DO_2BITS(asf->packet_flags >> 1, padsize, 0); // sequence ignored
  683. DO_2BITS(asf->packet_flags >> 3, padsize, 0); // padding length
  684. //the following checks prevent overflows and infinite loops
  685. if(!packet_length || packet_length >= (1U<<29)){
  686. av_log(s, AV_LOG_ERROR, "invalid packet_length %d at:%"PRId64"\n", packet_length, avio_tell(pb));
  687. return -1;
  688. }
  689. if(padsize >= packet_length){
  690. av_log(s, AV_LOG_ERROR, "invalid padsize %d at:%"PRId64"\n", padsize, avio_tell(pb));
  691. return -1;
  692. }
  693. asf->packet_timestamp = avio_rl32(pb);
  694. avio_rl16(pb); /* duration */
  695. // rsize has at least 11 bytes which have to be present
  696. if (asf->packet_flags & 0x01) {
  697. asf->packet_segsizetype = avio_r8(pb); rsize++;
  698. asf->packet_segments = asf->packet_segsizetype & 0x3f;
  699. } else {
  700. asf->packet_segments = 1;
  701. asf->packet_segsizetype = 0x80;
  702. }
  703. asf->packet_size_left = packet_length - padsize - rsize;
  704. if (packet_length < asf->hdr.min_pktsize)
  705. padsize += asf->hdr.min_pktsize - packet_length;
  706. asf->packet_padsize = padsize;
  707. av_dlog(s, "packet: size=%d padsize=%d left=%d\n", s->packet_size, asf->packet_padsize, asf->packet_size_left);
  708. return 0;
  709. }
  710. /**
  711. *
  712. * @return <0 if error
  713. */
  714. static int asf_read_frame_header(AVFormatContext *s, AVIOContext *pb){
  715. ASFContext *asf = s->priv_data;
  716. int rsize = 1;
  717. int num = avio_r8(pb);
  718. int64_t ts0;
  719. asf->packet_segments--;
  720. asf->packet_key_frame = num >> 7;
  721. asf->stream_index = asf->asfid2avid[num & 0x7f];
  722. // sequence should be ignored!
  723. DO_2BITS(asf->packet_property >> 4, asf->packet_seq, 0);
  724. DO_2BITS(asf->packet_property >> 2, asf->packet_frag_offset, 0);
  725. DO_2BITS(asf->packet_property, asf->packet_replic_size, 0);
  726. //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);
  727. if (rsize+asf->packet_replic_size > asf->packet_size_left) {
  728. av_log(s, AV_LOG_ERROR, "packet_replic_size %d is invalid\n", asf->packet_replic_size);
  729. return -1;
  730. }
  731. if (asf->packet_replic_size >= 8) {
  732. asf->packet_obj_size = avio_rl32(pb);
  733. if(asf->packet_obj_size >= (1<<24) || asf->packet_obj_size <= 0){
  734. av_log(s, AV_LOG_ERROR, "packet_obj_size invalid\n");
  735. return -1;
  736. }
  737. asf->packet_frag_timestamp = avio_rl32(pb); // timestamp
  738. if(asf->packet_replic_size >= 8+38+4){
  739. // for(i=0; i<asf->packet_replic_size-8; i++)
  740. // av_log(s, AV_LOG_DEBUG, "%02X ",avio_r8(pb));
  741. // av_log(s, AV_LOG_DEBUG, "\n");
  742. avio_skip(pb, 10);
  743. ts0= avio_rl64(pb);
  744. avio_skip(pb, 8);;
  745. avio_skip(pb, 12);
  746. avio_rl32(pb);
  747. avio_skip(pb, asf->packet_replic_size - 8 - 38 - 4);
  748. if(ts0!= -1) asf->packet_frag_timestamp= ts0/10000;
  749. else asf->packet_frag_timestamp= AV_NOPTS_VALUE;
  750. }else
  751. avio_skip(pb, asf->packet_replic_size - 8);
  752. rsize += asf->packet_replic_size; // FIXME - check validity
  753. } else if (asf->packet_replic_size==1){
  754. // multipacket - frag_offset is beginning timestamp
  755. asf->packet_time_start = asf->packet_frag_offset;
  756. asf->packet_frag_offset = 0;
  757. asf->packet_frag_timestamp = asf->packet_timestamp;
  758. asf->packet_time_delta = avio_r8(pb);
  759. rsize++;
  760. }else if(asf->packet_replic_size!=0){
  761. av_log(s, AV_LOG_ERROR, "unexpected packet_replic_size of %d\n", asf->packet_replic_size);
  762. return -1;
  763. }
  764. if (asf->packet_flags & 0x01) {
  765. DO_2BITS(asf->packet_segsizetype >> 6, asf->packet_frag_size, 0); // 0 is illegal
  766. if (rsize > asf->packet_size_left) {
  767. av_log(s, AV_LOG_ERROR, "packet_replic_size is invalid\n");
  768. return -1;
  769. } else if(asf->packet_frag_size > asf->packet_size_left - rsize){
  770. if (asf->packet_frag_size > asf->packet_size_left - rsize + asf->packet_padsize) {
  771. av_log(s, AV_LOG_ERROR, "packet_frag_size is invalid (%d-%d)\n", asf->packet_size_left, rsize);
  772. return -1;
  773. } else {
  774. int diff = asf->packet_frag_size - (asf->packet_size_left - rsize);
  775. asf->packet_size_left += diff;
  776. asf->packet_padsize -= diff;
  777. }
  778. }
  779. //printf("Fragsize %d\n", asf->packet_frag_size);
  780. } else {
  781. asf->packet_frag_size = asf->packet_size_left - rsize;
  782. //printf("Using rest %d %d %d\n", asf->packet_frag_size, asf->packet_size_left, rsize);
  783. }
  784. if (asf->packet_replic_size == 1) {
  785. asf->packet_multi_size = asf->packet_frag_size;
  786. if (asf->packet_multi_size > asf->packet_size_left)
  787. return -1;
  788. }
  789. asf->packet_size_left -= rsize;
  790. //printf("___objsize____ %d %d rs:%d\n", asf->packet_obj_size, asf->packet_frag_offset, rsize);
  791. return 0;
  792. }
  793. /**
  794. * Parse data from individual ASF packets (which were previously loaded
  795. * with asf_get_packet()).
  796. * @param s demux context
  797. * @param pb context to read data from
  798. * @param pkt pointer to store packet data into
  799. * @return 0 if data was stored in pkt, <0 on error or 1 if more ASF
  800. * packets need to be loaded (through asf_get_packet())
  801. */
  802. static int ff_asf_parse_packet(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
  803. {
  804. ASFContext *asf = s->priv_data;
  805. ASFStream *asf_st = 0;
  806. for (;;) {
  807. int ret;
  808. if(url_feof(pb))
  809. return AVERROR_EOF;
  810. if (asf->packet_size_left < FRAME_HEADER_SIZE
  811. || asf->packet_segments < 1) {
  812. //asf->packet_size_left <= asf->packet_padsize) {
  813. int ret = asf->packet_size_left + asf->packet_padsize;
  814. //printf("PacketLeftSize:%d Pad:%d Pos:%"PRId64"\n", asf->packet_size_left, asf->packet_padsize, avio_tell(pb));
  815. assert(ret>=0);
  816. /* fail safe */
  817. avio_skip(pb, ret);
  818. asf->packet_pos= avio_tell(pb);
  819. if (asf->data_object_size != (uint64_t)-1 &&
  820. (asf->packet_pos - asf->data_object_offset >= asf->data_object_size))
  821. return AVERROR_EOF; /* Do not exceed the size of the data object */
  822. return 1;
  823. }
  824. if (asf->packet_time_start == 0) {
  825. if(asf_read_frame_header(s, pb) < 0){
  826. asf->packet_segments= 0;
  827. continue;
  828. }
  829. if (asf->stream_index < 0
  830. || s->streams[asf->stream_index]->discard >= AVDISCARD_ALL
  831. || (!asf->packet_key_frame && s->streams[asf->stream_index]->discard >= AVDISCARD_NONKEY)
  832. ) {
  833. asf->packet_time_start = 0;
  834. /* unhandled packet (should not happen) */
  835. avio_skip(pb, asf->packet_frag_size);
  836. asf->packet_size_left -= asf->packet_frag_size;
  837. if(asf->stream_index < 0)
  838. av_log(s, AV_LOG_ERROR, "ff asf skip %d (unknown stream)\n", asf->packet_frag_size);
  839. continue;
  840. }
  841. asf->asf_st = s->streams[asf->stream_index]->priv_data;
  842. }
  843. asf_st = asf->asf_st;
  844. if (asf->packet_replic_size == 1) {
  845. // frag_offset is here used as the beginning timestamp
  846. asf->packet_frag_timestamp = asf->packet_time_start;
  847. asf->packet_time_start += asf->packet_time_delta;
  848. asf->packet_obj_size = asf->packet_frag_size = avio_r8(pb);
  849. asf->packet_size_left--;
  850. asf->packet_multi_size--;
  851. if (asf->packet_multi_size < asf->packet_obj_size)
  852. {
  853. asf->packet_time_start = 0;
  854. avio_skip(pb, asf->packet_multi_size);
  855. asf->packet_size_left -= asf->packet_multi_size;
  856. continue;
  857. }
  858. asf->packet_multi_size -= asf->packet_obj_size;
  859. //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);
  860. }
  861. if( /*asf->packet_frag_size == asf->packet_obj_size*/
  862. asf_st->frag_offset + asf->packet_frag_size <= asf_st->pkt.size
  863. && asf_st->frag_offset + asf->packet_frag_size > asf->packet_obj_size){
  864. av_log(s, AV_LOG_INFO, "ignoring invalid packet_obj_size (%d %d %d %d)\n",
  865. asf_st->frag_offset, asf->packet_frag_size,
  866. asf->packet_obj_size, asf_st->pkt.size);
  867. asf->packet_obj_size= asf_st->pkt.size;
  868. }
  869. if ( asf_st->pkt.size != asf->packet_obj_size
  870. || asf_st->frag_offset + asf->packet_frag_size > asf_st->pkt.size) { //FIXME is this condition sufficient?
  871. if(asf_st->pkt.data){
  872. av_log(s, AV_LOG_INFO, "freeing incomplete packet size %d, new %d\n", asf_st->pkt.size, asf->packet_obj_size);
  873. asf_st->frag_offset = 0;
  874. av_free_packet(&asf_st->pkt);
  875. }
  876. /* new packet */
  877. av_new_packet(&asf_st->pkt, asf->packet_obj_size);
  878. asf_st->seq = asf->packet_seq;
  879. asf_st->pkt.dts = asf->packet_frag_timestamp - asf->hdr.preroll;
  880. asf_st->pkt.stream_index = asf->stream_index;
  881. asf_st->pkt.pos =
  882. asf_st->packet_pos= asf->packet_pos;
  883. //printf("new packet: stream:%d key:%d packet_key:%d audio:%d size:%d\n",
  884. //asf->stream_index, asf->packet_key_frame, asf_st->pkt.flags & AV_PKT_FLAG_KEY,
  885. //s->streams[asf->stream_index]->codec->codec_type == AVMEDIA_TYPE_AUDIO, asf->packet_obj_size);
  886. if (s->streams[asf->stream_index]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
  887. asf->packet_key_frame = 1;
  888. if (asf->packet_key_frame)
  889. asf_st->pkt.flags |= AV_PKT_FLAG_KEY;
  890. }
  891. /* read data */
  892. //printf("READ PACKET s:%d os:%d o:%d,%d l:%d DATA:%p\n",
  893. // s->packet_size, asf_st->pkt.size, asf->packet_frag_offset,
  894. // asf_st->frag_offset, asf->packet_frag_size, asf_st->pkt.data);
  895. asf->packet_size_left -= asf->packet_frag_size;
  896. if (asf->packet_size_left < 0)
  897. continue;
  898. if( asf->packet_frag_offset >= asf_st->pkt.size
  899. || asf->packet_frag_size > asf_st->pkt.size - asf->packet_frag_offset){
  900. av_log(s, AV_LOG_ERROR, "packet fragment position invalid %u,%u not in %u\n",
  901. asf->packet_frag_offset, asf->packet_frag_size, asf_st->pkt.size);
  902. continue;
  903. }
  904. ret = avio_read(pb, asf_st->pkt.data + asf->packet_frag_offset,
  905. asf->packet_frag_size);
  906. if (ret != asf->packet_frag_size) {
  907. if (ret < 0 || asf->packet_frag_offset + ret == 0)
  908. return ret < 0 ? ret : AVERROR_EOF;
  909. if (asf_st->ds_span > 1) {
  910. // scrambling, we can either drop it completely or fill the remainder
  911. // TODO: should we fill the whole packet instead of just the current
  912. // fragment?
  913. memset(asf_st->pkt.data + asf->packet_frag_offset + ret, 0,
  914. asf->packet_frag_size - ret);
  915. ret = asf->packet_frag_size;
  916. } else
  917. // no scrambling, so we can return partial packets
  918. av_shrink_packet(&asf_st->pkt, asf->packet_frag_offset + ret);
  919. }
  920. if (s->key && s->keylen == 20)
  921. ff_asfcrypt_dec(s->key, asf_st->pkt.data + asf->packet_frag_offset,
  922. ret);
  923. asf_st->frag_offset += ret;
  924. /* test if whole packet is read */
  925. if (asf_st->frag_offset == asf_st->pkt.size) {
  926. //workaround for macroshit radio DVR-MS files
  927. if( s->streams[asf->stream_index]->codec->codec_id == CODEC_ID_MPEG2VIDEO
  928. && asf_st->pkt.size > 100){
  929. int i;
  930. for(i=0; i<asf_st->pkt.size && !asf_st->pkt.data[i]; i++);
  931. if(i == asf_st->pkt.size){
  932. av_log(s, AV_LOG_DEBUG, "discarding ms fart\n");
  933. asf_st->frag_offset = 0;
  934. av_free_packet(&asf_st->pkt);
  935. continue;
  936. }
  937. }
  938. /* return packet */
  939. if (asf_st->ds_span > 1) {
  940. if(asf_st->pkt.size != asf_st->ds_packet_size * asf_st->ds_span){
  941. 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);
  942. }else{
  943. /* packet descrambling */
  944. uint8_t *newdata = av_malloc(asf_st->pkt.size + FF_INPUT_BUFFER_PADDING_SIZE);
  945. if (newdata) {
  946. int offset = 0;
  947. memset(newdata + asf_st->pkt.size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  948. while (offset < asf_st->pkt.size) {
  949. int off = offset / asf_st->ds_chunk_size;
  950. int row = off / asf_st->ds_span;
  951. int col = off % asf_st->ds_span;
  952. int idx = row + col * asf_st->ds_packet_size / asf_st->ds_chunk_size;
  953. //printf("off:%d row:%d col:%d idx:%d\n", off, row, col, idx);
  954. assert(offset + asf_st->ds_chunk_size <= asf_st->pkt.size);
  955. assert(idx+1 <= asf_st->pkt.size / asf_st->ds_chunk_size);
  956. memcpy(newdata + offset,
  957. asf_st->pkt.data + idx * asf_st->ds_chunk_size,
  958. asf_st->ds_chunk_size);
  959. offset += asf_st->ds_chunk_size;
  960. }
  961. av_free(asf_st->pkt.data);
  962. asf_st->pkt.data = newdata;
  963. }
  964. }
  965. }
  966. asf_st->frag_offset = 0;
  967. *pkt= asf_st->pkt;
  968. //printf("packet %d %d\n", asf_st->pkt.size, asf->packet_frag_size);
  969. asf_st->pkt.size = 0;
  970. asf_st->pkt.data = 0;
  971. break; // packet completed
  972. }
  973. }
  974. return 0;
  975. }
  976. static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
  977. {
  978. ASFContext *asf = s->priv_data;
  979. for (;;) {
  980. int ret;
  981. /* parse cached packets, if any */
  982. if ((ret = ff_asf_parse_packet(s, s->pb, pkt)) <= 0)
  983. return ret;
  984. if ((ret = ff_asf_get_packet(s, s->pb)) < 0)
  985. assert(asf->packet_size_left < FRAME_HEADER_SIZE || asf->packet_segments < 1);
  986. asf->packet_time_start = 0;
  987. }
  988. }
  989. // Added to support seeking after packets have been read
  990. // If information is not reset, read_packet fails due to
  991. // leftover information from previous reads
  992. static void asf_reset_header(AVFormatContext *s)
  993. {
  994. ASFContext *asf = s->priv_data;
  995. ASFStream *asf_st;
  996. int i;
  997. asf->packet_size_left = 0;
  998. asf->packet_segments = 0;
  999. asf->packet_flags = 0;
  1000. asf->packet_property = 0;
  1001. asf->packet_timestamp = 0;
  1002. asf->packet_segsizetype = 0;
  1003. asf->packet_segments = 0;
  1004. asf->packet_seq = 0;
  1005. asf->packet_replic_size = 0;
  1006. asf->packet_key_frame = 0;
  1007. asf->packet_padsize = 0;
  1008. asf->packet_frag_offset = 0;
  1009. asf->packet_frag_size = 0;
  1010. asf->packet_frag_timestamp = 0;
  1011. asf->packet_multi_size = 0;
  1012. asf->packet_obj_size = 0;
  1013. asf->packet_time_delta = 0;
  1014. asf->packet_time_start = 0;
  1015. for(i=0; i<s->nb_streams; i++){
  1016. asf_st= s->streams[i]->priv_data;
  1017. av_free_packet(&asf_st->pkt);
  1018. asf_st->frag_offset=0;
  1019. asf_st->seq=0;
  1020. }
  1021. asf->asf_st= NULL;
  1022. }
  1023. static int asf_read_close(AVFormatContext *s)
  1024. {
  1025. int i;
  1026. asf_reset_header(s);
  1027. for(i=0;i<s->nb_streams;i++) {
  1028. AVStream *st = s->streams[i];
  1029. av_free(st->codec->palctrl);
  1030. }
  1031. return 0;
  1032. }
  1033. static int64_t asf_read_pts(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit)
  1034. {
  1035. AVPacket pkt1, *pkt = &pkt1;
  1036. ASFStream *asf_st;
  1037. int64_t pts;
  1038. int64_t pos= *ppos;
  1039. int i;
  1040. int64_t start_pos[ASF_MAX_STREAMS];
  1041. for(i=0; i<s->nb_streams; i++){
  1042. start_pos[i]= pos;
  1043. }
  1044. if (s->packet_size > 0)
  1045. pos= (pos+s->packet_size-1-s->data_offset)/s->packet_size*s->packet_size+ s->data_offset;
  1046. *ppos= pos;
  1047. if (avio_seek(s->pb, pos, SEEK_SET) < 0)
  1048. return AV_NOPTS_VALUE;
  1049. //printf("asf_read_pts\n");
  1050. asf_reset_header(s);
  1051. for(;;){
  1052. if (av_read_frame(s, pkt) < 0){
  1053. av_log(s, AV_LOG_INFO, "asf_read_pts failed\n");
  1054. return AV_NOPTS_VALUE;
  1055. }
  1056. pts= pkt->pts;
  1057. av_free_packet(pkt);
  1058. if(pkt->flags&AV_PKT_FLAG_KEY){
  1059. i= pkt->stream_index;
  1060. asf_st= s->streams[i]->priv_data;
  1061. // assert((asf_st->packet_pos - s->data_offset) % s->packet_size == 0);
  1062. pos= asf_st->packet_pos;
  1063. av_add_index_entry(s->streams[i], pos, pts, pkt->size, pos - start_pos[i] + 1, AVINDEX_KEYFRAME);
  1064. start_pos[i]= asf_st->packet_pos + 1;
  1065. if(pkt->stream_index == stream_index)
  1066. break;
  1067. }
  1068. }
  1069. *ppos= pos;
  1070. //printf("found keyframe at %"PRId64" stream %d stamp:%"PRId64"\n", *ppos, stream_index, pts);
  1071. return pts;
  1072. }
  1073. static void asf_build_simple_index(AVFormatContext *s, int stream_index)
  1074. {
  1075. ff_asf_guid g;
  1076. ASFContext *asf = s->priv_data;
  1077. int64_t current_pos= avio_tell(s->pb);
  1078. int i;
  1079. if(avio_seek(s->pb, asf->data_object_offset + asf->data_object_size, SEEK_SET) < 0)
  1080. return;
  1081. ff_get_guid(s->pb, &g);
  1082. /* the data object can be followed by other top-level objects,
  1083. skip them until the simple index object is reached */
  1084. while (ff_guidcmp(&g, &index_guid)) {
  1085. int64_t gsize= avio_rl64(s->pb);
  1086. if (gsize < 24 || url_feof(s->pb)) {
  1087. avio_seek(s->pb, current_pos, SEEK_SET);
  1088. return;
  1089. }
  1090. avio_skip(s->pb, gsize-24);
  1091. ff_get_guid(s->pb, &g);
  1092. }
  1093. {
  1094. int64_t itime, last_pos=-1;
  1095. int pct, ict;
  1096. int64_t av_unused gsize= avio_rl64(s->pb);
  1097. ff_get_guid(s->pb, &g);
  1098. itime=avio_rl64(s->pb);
  1099. pct=avio_rl32(s->pb);
  1100. ict=avio_rl32(s->pb);
  1101. av_log(s, AV_LOG_DEBUG, "itime:0x%"PRIx64", pct:%d, ict:%d\n",itime,pct,ict);
  1102. for (i=0;i<ict;i++){
  1103. int pktnum=avio_rl32(s->pb);
  1104. int pktct =avio_rl16(s->pb);
  1105. int64_t pos = s->data_offset + s->packet_size*(int64_t)pktnum;
  1106. int64_t index_pts= FFMAX(av_rescale(itime, i, 10000) - asf->hdr.preroll, 0);
  1107. if(pos != last_pos){
  1108. av_log(s, AV_LOG_DEBUG, "pktnum:%d, pktct:%d pts: %"PRId64"\n", pktnum, pktct, index_pts);
  1109. av_add_index_entry(s->streams[stream_index], pos, index_pts, s->packet_size, 0, AVINDEX_KEYFRAME);
  1110. last_pos=pos;
  1111. }
  1112. }
  1113. asf->index_read= 1;
  1114. }
  1115. avio_seek(s->pb, current_pos, SEEK_SET);
  1116. }
  1117. static int asf_read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
  1118. {
  1119. ASFContext *asf = s->priv_data;
  1120. AVStream *st = s->streams[stream_index];
  1121. int64_t pos;
  1122. int index;
  1123. if (s->packet_size <= 0)
  1124. return -1;
  1125. /* Try using the protocol's read_seek if available */
  1126. if(s->pb) {
  1127. int ret = avio_seek_time(s->pb, stream_index, pts, flags);
  1128. if(ret >= 0)
  1129. asf_reset_header(s);
  1130. if (ret != AVERROR(ENOSYS))
  1131. return ret;
  1132. }
  1133. if (!asf->index_read)
  1134. asf_build_simple_index(s, stream_index);
  1135. if((asf->index_read && st->index_entries)){
  1136. index= av_index_search_timestamp(st, pts, flags);
  1137. if(index >= 0) {
  1138. /* find the position */
  1139. pos = st->index_entries[index].pos;
  1140. /* do the seek */
  1141. av_log(s, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos);
  1142. if(avio_seek(s->pb, pos, SEEK_SET) < 0)
  1143. return -1;
  1144. asf_reset_header(s);
  1145. return 0;
  1146. }
  1147. }
  1148. /* no index or seeking by index failed */
  1149. if(av_seek_frame_binary(s, stream_index, pts, flags)<0)
  1150. return -1;
  1151. asf_reset_header(s);
  1152. return 0;
  1153. }
  1154. AVInputFormat ff_asf_demuxer = {
  1155. .name = "asf",
  1156. .long_name = NULL_IF_CONFIG_SMALL("ASF format"),
  1157. .priv_data_size = sizeof(ASFContext),
  1158. .read_probe = asf_probe,
  1159. .read_header = asf_read_header,
  1160. .read_packet = asf_read_packet,
  1161. .read_close = asf_read_close,
  1162. .read_seek = asf_read_seek,
  1163. .read_timestamp = asf_read_pts,
  1164. .flags = AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH,
  1165. };