asfdec.c 52 KB

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