rmdec.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  1. /*
  2. * "Real" compatible demuxer.
  3. * Copyright (c) 2000, 2001 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/avstring.h"
  22. #include "libavutil/intreadwrite.h"
  23. #include "libavutil/dict.h"
  24. #include "avformat.h"
  25. #include "internal.h"
  26. #include "riff.h"
  27. #include "rm.h"
  28. #define DEINT_ID_GENR MKTAG('g', 'e', 'n', 'r') ///< interleaving for Cooker/Atrac
  29. #define DEINT_ID_INT0 MKTAG('I', 'n', 't', '0') ///< no interleaving needed
  30. #define DEINT_ID_INT4 MKTAG('I', 'n', 't', '4') ///< interleaving for 28.8
  31. #define DEINT_ID_SIPR MKTAG('s', 'i', 'p', 'r') ///< interleaving for Sipro
  32. #define DEINT_ID_VBRF MKTAG('v', 'b', 'r', 'f') ///< VBR case for AAC
  33. #define DEINT_ID_VBRS MKTAG('v', 'b', 'r', 's') ///< VBR case for AAC
  34. struct RMStream {
  35. AVPacket pkt; ///< place to store merged video frame / reordered audio data
  36. int videobufsize; ///< current assembled frame size
  37. int videobufpos; ///< position for the next slice in the video buffer
  38. int curpic_num; ///< picture number of current frame
  39. int cur_slice, slices;
  40. int64_t pktpos; ///< first slice position in file
  41. /// Audio descrambling matrix parameters
  42. int64_t audiotimestamp; ///< Audio packet timestamp
  43. int sub_packet_cnt; // Subpacket counter, used while reading
  44. int sub_packet_size, sub_packet_h, coded_framesize; ///< Descrambling parameters from container
  45. int audio_framesize; /// Audio frame size from container
  46. int sub_packet_lengths[16]; /// Length of each subpacket
  47. int32_t deint_id; ///< deinterleaver used in audio stream
  48. };
  49. typedef struct {
  50. int nb_packets;
  51. int old_format;
  52. int current_stream;
  53. int remaining_len;
  54. int audio_stream_num; ///< Stream number for audio packets
  55. int audio_pkt_cnt; ///< Output packet counter
  56. } RMDemuxContext;
  57. static const unsigned char sipr_swaps[38][2] = {
  58. { 0, 63 }, { 1, 22 }, { 2, 44 }, { 3, 90 },
  59. { 5, 81 }, { 7, 31 }, { 8, 86 }, { 9, 58 },
  60. { 10, 36 }, { 12, 68 }, { 13, 39 }, { 14, 73 },
  61. { 15, 53 }, { 16, 69 }, { 17, 57 }, { 19, 88 },
  62. { 20, 34 }, { 21, 71 }, { 24, 46 }, { 25, 94 },
  63. { 26, 54 }, { 28, 75 }, { 29, 50 }, { 32, 70 },
  64. { 33, 92 }, { 35, 74 }, { 38, 85 }, { 40, 56 },
  65. { 42, 87 }, { 43, 65 }, { 45, 59 }, { 48, 79 },
  66. { 49, 93 }, { 51, 89 }, { 55, 95 }, { 61, 76 },
  67. { 67, 83 }, { 77, 80 }
  68. };
  69. const unsigned char ff_sipr_subpk_size[4] = { 29, 19, 37, 20 };
  70. static inline void get_strl(AVIOContext *pb, char *buf, int buf_size, int len)
  71. {
  72. int i;
  73. char *q, r;
  74. q = buf;
  75. for(i=0;i<len;i++) {
  76. r = avio_r8(pb);
  77. if (i < buf_size - 1)
  78. *q++ = r;
  79. }
  80. if (buf_size > 0) *q = '\0';
  81. }
  82. static void get_str8(AVIOContext *pb, char *buf, int buf_size)
  83. {
  84. get_strl(pb, buf, buf_size, avio_r8(pb));
  85. }
  86. static int rm_read_extradata(AVIOContext *pb, AVCodecContext *avctx, unsigned size)
  87. {
  88. if (size >= 1<<24)
  89. return -1;
  90. avctx->extradata = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
  91. if (!avctx->extradata)
  92. return AVERROR(ENOMEM);
  93. avctx->extradata_size = avio_read(pb, avctx->extradata, size);
  94. memset(avctx->extradata + avctx->extradata_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  95. if (avctx->extradata_size != size)
  96. return AVERROR(EIO);
  97. return 0;
  98. }
  99. static void rm_read_metadata(AVFormatContext *s, AVIOContext *pb, int wide)
  100. {
  101. char buf[1024];
  102. int i;
  103. for (i=0; i<FF_ARRAY_ELEMS(ff_rm_metadata); i++) {
  104. int len = wide ? avio_rb16(pb) : avio_r8(pb);
  105. get_strl(pb, buf, sizeof(buf), len);
  106. av_dict_set(&s->metadata, ff_rm_metadata[i], buf, 0);
  107. }
  108. }
  109. RMStream *ff_rm_alloc_rmstream (void)
  110. {
  111. RMStream *rms = av_mallocz(sizeof(RMStream));
  112. rms->curpic_num = -1;
  113. return rms;
  114. }
  115. void ff_rm_free_rmstream (RMStream *rms)
  116. {
  117. av_free_packet(&rms->pkt);
  118. }
  119. static int rm_read_audio_stream_info(AVFormatContext *s, AVIOContext *pb,
  120. AVStream *st, RMStream *ast, int read_all)
  121. {
  122. char buf[256];
  123. uint32_t version;
  124. int ret;
  125. /* ra type header */
  126. version = avio_rb16(pb); /* version */
  127. if (version == 3) {
  128. int header_size = avio_rb16(pb);
  129. int64_t startpos = avio_tell(pb);
  130. avio_skip(pb, 14);
  131. rm_read_metadata(s, pb, 0);
  132. if ((startpos + header_size) >= avio_tell(pb) + 2) {
  133. // fourcc (should always be "lpcJ")
  134. avio_r8(pb);
  135. get_str8(pb, buf, sizeof(buf));
  136. }
  137. // Skip extra header crap (this should never happen)
  138. if ((startpos + header_size) > avio_tell(pb))
  139. avio_skip(pb, header_size + startpos - avio_tell(pb));
  140. st->codec->sample_rate = 8000;
  141. st->codec->channels = 1;
  142. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  143. st->codec->codec_id = CODEC_ID_RA_144;
  144. ast->deint_id = DEINT_ID_INT0;
  145. } else {
  146. int flavor, sub_packet_h, coded_framesize, sub_packet_size;
  147. int codecdata_length;
  148. /* old version (4) */
  149. avio_skip(pb, 2); /* unused */
  150. avio_rb32(pb); /* .ra4 */
  151. avio_rb32(pb); /* data size */
  152. avio_rb16(pb); /* version2 */
  153. avio_rb32(pb); /* header size */
  154. flavor= avio_rb16(pb); /* add codec info / flavor */
  155. ast->coded_framesize = coded_framesize = avio_rb32(pb); /* coded frame size */
  156. avio_rb32(pb); /* ??? */
  157. avio_rb32(pb); /* ??? */
  158. avio_rb32(pb); /* ??? */
  159. ast->sub_packet_h = sub_packet_h = avio_rb16(pb); /* 1 */
  160. st->codec->block_align= avio_rb16(pb); /* frame size */
  161. ast->sub_packet_size = sub_packet_size = avio_rb16(pb); /* sub packet size */
  162. avio_rb16(pb); /* ??? */
  163. if (version == 5) {
  164. avio_rb16(pb); avio_rb16(pb); avio_rb16(pb);
  165. }
  166. st->codec->sample_rate = avio_rb16(pb);
  167. avio_rb32(pb);
  168. st->codec->channels = avio_rb16(pb);
  169. if (version == 5) {
  170. ast->deint_id = avio_rl32(pb);
  171. avio_read(pb, buf, 4);
  172. buf[4] = 0;
  173. } else {
  174. get_str8(pb, buf, sizeof(buf)); /* desc */
  175. ast->deint_id = AV_RL32(buf);
  176. get_str8(pb, buf, sizeof(buf)); /* desc */
  177. }
  178. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  179. st->codec->codec_tag = AV_RL32(buf);
  180. st->codec->codec_id = ff_codec_get_id(ff_rm_codec_tags,
  181. st->codec->codec_tag);
  182. switch (st->codec->codec_id) {
  183. case CODEC_ID_AC3:
  184. st->need_parsing = AVSTREAM_PARSE_FULL;
  185. break;
  186. case CODEC_ID_RA_288:
  187. st->codec->extradata_size= 0;
  188. ast->audio_framesize = st->codec->block_align;
  189. st->codec->block_align = coded_framesize;
  190. break;
  191. case CODEC_ID_COOK:
  192. case CODEC_ID_ATRAC3:
  193. case CODEC_ID_SIPR:
  194. avio_rb16(pb); avio_r8(pb);
  195. if (version == 5)
  196. avio_r8(pb);
  197. codecdata_length = avio_rb32(pb);
  198. if(codecdata_length + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
  199. av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
  200. return -1;
  201. }
  202. ast->audio_framesize = st->codec->block_align;
  203. if (st->codec->codec_id == CODEC_ID_SIPR) {
  204. if (flavor > 3) {
  205. av_log(s, AV_LOG_ERROR, "bad SIPR file flavor %d\n",
  206. flavor);
  207. return -1;
  208. }
  209. st->codec->block_align = ff_sipr_subpk_size[flavor];
  210. } else {
  211. if(sub_packet_size <= 0){
  212. av_log(s, AV_LOG_ERROR, "sub_packet_size is invalid\n");
  213. return -1;
  214. }
  215. st->codec->block_align = ast->sub_packet_size;
  216. }
  217. if ((ret = rm_read_extradata(pb, st->codec, codecdata_length)) < 0)
  218. return ret;
  219. break;
  220. case CODEC_ID_AAC:
  221. avio_rb16(pb); avio_r8(pb);
  222. if (version == 5)
  223. avio_r8(pb);
  224. codecdata_length = avio_rb32(pb);
  225. if(codecdata_length + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
  226. av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
  227. return -1;
  228. }
  229. if (codecdata_length >= 1) {
  230. avio_r8(pb);
  231. if ((ret = rm_read_extradata(pb, st->codec, codecdata_length - 1)) < 0)
  232. return ret;
  233. }
  234. break;
  235. default:
  236. av_strlcpy(st->codec->codec_name, buf, sizeof(st->codec->codec_name));
  237. }
  238. if (ast->deint_id == DEINT_ID_INT4 ||
  239. ast->deint_id == DEINT_ID_GENR ||
  240. ast->deint_id == DEINT_ID_SIPR) {
  241. if (st->codec->block_align <= 0 ||
  242. ast->audio_framesize * sub_packet_h > (unsigned)INT_MAX ||
  243. ast->audio_framesize * sub_packet_h < st->codec->block_align)
  244. return AVERROR_INVALIDDATA;
  245. if (av_new_packet(&ast->pkt, ast->audio_framesize * sub_packet_h) < 0)
  246. return AVERROR(ENOMEM);
  247. }
  248. switch (ast->deint_id) {
  249. case DEINT_ID_INT4:
  250. if (ast->coded_framesize > ast->audio_framesize ||
  251. sub_packet_h <= 1 ||
  252. ast->coded_framesize * sub_packet_h > (2 + (sub_packet_h & 1)) * ast->audio_framesize)
  253. return AVERROR_INVALIDDATA;
  254. break;
  255. case DEINT_ID_GENR:
  256. if (ast->sub_packet_size <= 0 ||
  257. ast->sub_packet_size > ast->audio_framesize)
  258. return AVERROR_INVALIDDATA;
  259. break;
  260. case DEINT_ID_SIPR:
  261. case DEINT_ID_INT0:
  262. case DEINT_ID_VBRS:
  263. case DEINT_ID_VBRF:
  264. break;
  265. default:
  266. av_log(NULL,0,"Unknown interleaver %X\n", ast->deint_id);
  267. return AVERROR_INVALIDDATA;
  268. }
  269. if (read_all) {
  270. avio_r8(pb);
  271. avio_r8(pb);
  272. avio_r8(pb);
  273. rm_read_metadata(s, pb, 0);
  274. }
  275. }
  276. return 0;
  277. }
  278. int
  279. ff_rm_read_mdpr_codecdata (AVFormatContext *s, AVIOContext *pb,
  280. AVStream *st, RMStream *rst, int codec_data_size)
  281. {
  282. unsigned int v;
  283. int size;
  284. int64_t codec_pos;
  285. int ret;
  286. if (codec_data_size < 0)
  287. return AVERROR_INVALIDDATA;
  288. avpriv_set_pts_info(st, 64, 1, 1000);
  289. codec_pos = avio_tell(pb);
  290. v = avio_rb32(pb);
  291. if (v == MKTAG(0xfd, 'a', 'r', '.')) {
  292. /* ra type header */
  293. if (rm_read_audio_stream_info(s, pb, st, rst, 0))
  294. return -1;
  295. } else {
  296. int fps;
  297. if (avio_rl32(pb) != MKTAG('V', 'I', 'D', 'O')) {
  298. fail1:
  299. av_log(s, AV_LOG_WARNING, "Unsupported stream type %08x\n", v);
  300. goto skip;
  301. }
  302. st->codec->codec_tag = avio_rl32(pb);
  303. st->codec->codec_id = ff_codec_get_id(ff_rm_codec_tags,
  304. st->codec->codec_tag);
  305. // av_log(s, AV_LOG_DEBUG, "%X %X\n", st->codec->codec_tag, MKTAG('R', 'V', '2', '0'));
  306. if (st->codec->codec_id == CODEC_ID_NONE)
  307. goto fail1;
  308. st->codec->width = avio_rb16(pb);
  309. st->codec->height = avio_rb16(pb);
  310. avio_skip(pb, 2); // looks like bits per sample
  311. avio_skip(pb, 4); // always zero?
  312. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  313. st->need_parsing = AVSTREAM_PARSE_TIMESTAMPS;
  314. fps = avio_rb32(pb);
  315. if ((ret = rm_read_extradata(pb, st->codec, codec_data_size - (avio_tell(pb) - codec_pos))) < 0)
  316. return ret;
  317. if (fps > 0) {
  318. av_reduce(&st->r_frame_rate.den, &st->r_frame_rate.num,
  319. 0x10000, fps, (1 << 30) - 1);
  320. } else if (s->error_recognition & AV_EF_EXPLODE) {
  321. av_log(s, AV_LOG_ERROR, "Invalid framerate\n");
  322. return AVERROR_INVALIDDATA;
  323. }
  324. st->avg_frame_rate = st->r_frame_rate;
  325. }
  326. skip:
  327. /* skip codec info */
  328. size = avio_tell(pb) - codec_pos;
  329. avio_skip(pb, codec_data_size - size);
  330. return 0;
  331. }
  332. /** this function assumes that the demuxer has already seeked to the start
  333. * of the INDX chunk, and will bail out if not. */
  334. static int rm_read_index(AVFormatContext *s)
  335. {
  336. AVIOContext *pb = s->pb;
  337. unsigned int size, n_pkts, str_id, next_off, n, pos, pts;
  338. AVStream *st;
  339. do {
  340. if (avio_rl32(pb) != MKTAG('I','N','D','X'))
  341. return -1;
  342. size = avio_rb32(pb);
  343. if (size < 20)
  344. return -1;
  345. avio_skip(pb, 2);
  346. n_pkts = avio_rb32(pb);
  347. str_id = avio_rb16(pb);
  348. next_off = avio_rb32(pb);
  349. for (n = 0; n < s->nb_streams; n++)
  350. if (s->streams[n]->id == str_id) {
  351. st = s->streams[n];
  352. break;
  353. }
  354. if (n == s->nb_streams) {
  355. av_log(s, AV_LOG_ERROR,
  356. "Invalid stream index %d for index at pos %"PRId64"\n",
  357. str_id, avio_tell(pb));
  358. goto skip;
  359. } else if ((avio_size(pb) - avio_tell(pb)) / 14 < n_pkts) {
  360. av_log(s, AV_LOG_ERROR,
  361. "Nr. of packets in packet index for stream index %d "
  362. "exceeds filesize (%"PRId64" at %"PRId64" = %d)\n",
  363. str_id, avio_size(pb), avio_tell(pb),
  364. (avio_size(pb) - avio_tell(pb)) / 14);
  365. goto skip;
  366. }
  367. for (n = 0; n < n_pkts; n++) {
  368. avio_skip(pb, 2);
  369. pts = avio_rb32(pb);
  370. pos = avio_rb32(pb);
  371. avio_skip(pb, 4); /* packet no. */
  372. av_add_index_entry(st, pos, pts, 0, 0, AVINDEX_KEYFRAME);
  373. }
  374. skip:
  375. if (next_off && avio_tell(pb) < next_off &&
  376. avio_seek(pb, next_off, SEEK_SET) < 0) {
  377. av_log(s, AV_LOG_ERROR,
  378. "Non-linear index detected, not supported\n");
  379. return -1;
  380. }
  381. } while (next_off);
  382. return 0;
  383. }
  384. static int rm_read_header_old(AVFormatContext *s)
  385. {
  386. RMDemuxContext *rm = s->priv_data;
  387. AVStream *st;
  388. rm->old_format = 1;
  389. st = avformat_new_stream(s, NULL);
  390. if (!st)
  391. return -1;
  392. st->priv_data = ff_rm_alloc_rmstream();
  393. return rm_read_audio_stream_info(s, s->pb, st, st->priv_data, 1);
  394. }
  395. static int rm_read_header(AVFormatContext *s, AVFormatParameters *ap)
  396. {
  397. RMDemuxContext *rm = s->priv_data;
  398. AVStream *st;
  399. AVIOContext *pb = s->pb;
  400. unsigned int tag;
  401. int tag_size;
  402. unsigned int start_time, duration;
  403. unsigned int data_off = 0, indx_off = 0;
  404. char buf[128];
  405. int flags = 0;
  406. tag = avio_rl32(pb);
  407. if (tag == MKTAG('.', 'r', 'a', 0xfd)) {
  408. /* very old .ra format */
  409. return rm_read_header_old(s);
  410. } else if (tag != MKTAG('.', 'R', 'M', 'F')) {
  411. return AVERROR(EIO);
  412. }
  413. avio_rb32(pb); /* header size */
  414. avio_rb16(pb);
  415. avio_rb32(pb);
  416. avio_rb32(pb); /* number of headers */
  417. for(;;) {
  418. if (url_feof(pb))
  419. return -1;
  420. tag = avio_rl32(pb);
  421. tag_size = avio_rb32(pb);
  422. avio_rb16(pb);
  423. av_dlog(s, "tag=%c%c%c%c (%08x) size=%d\n",
  424. (tag ) & 0xff,
  425. (tag >> 8) & 0xff,
  426. (tag >> 16) & 0xff,
  427. (tag >> 24) & 0xff,
  428. tag,
  429. tag_size);
  430. if (tag_size < 10 && tag != MKTAG('D', 'A', 'T', 'A'))
  431. return -1;
  432. switch(tag) {
  433. case MKTAG('P', 'R', 'O', 'P'):
  434. /* file header */
  435. avio_rb32(pb); /* max bit rate */
  436. avio_rb32(pb); /* avg bit rate */
  437. avio_rb32(pb); /* max packet size */
  438. avio_rb32(pb); /* avg packet size */
  439. avio_rb32(pb); /* nb packets */
  440. avio_rb32(pb); /* duration */
  441. avio_rb32(pb); /* preroll */
  442. indx_off = avio_rb32(pb); /* index offset */
  443. data_off = avio_rb32(pb); /* data offset */
  444. avio_rb16(pb); /* nb streams */
  445. flags = avio_rb16(pb); /* flags */
  446. break;
  447. case MKTAG('C', 'O', 'N', 'T'):
  448. rm_read_metadata(s, pb, 1);
  449. break;
  450. case MKTAG('M', 'D', 'P', 'R'):
  451. st = avformat_new_stream(s, NULL);
  452. if (!st)
  453. return AVERROR(ENOMEM);
  454. st->id = avio_rb16(pb);
  455. avio_rb32(pb); /* max bit rate */
  456. st->codec->bit_rate = avio_rb32(pb); /* bit rate */
  457. avio_rb32(pb); /* max packet size */
  458. avio_rb32(pb); /* avg packet size */
  459. start_time = avio_rb32(pb); /* start time */
  460. avio_rb32(pb); /* preroll */
  461. duration = avio_rb32(pb); /* duration */
  462. st->start_time = start_time;
  463. st->duration = duration;
  464. get_str8(pb, buf, sizeof(buf)); /* desc */
  465. get_str8(pb, buf, sizeof(buf)); /* mimetype */
  466. st->codec->codec_type = AVMEDIA_TYPE_DATA;
  467. st->priv_data = ff_rm_alloc_rmstream();
  468. if (ff_rm_read_mdpr_codecdata(s, s->pb, st, st->priv_data,
  469. avio_rb32(pb)) < 0)
  470. return -1;
  471. break;
  472. case MKTAG('D', 'A', 'T', 'A'):
  473. goto header_end;
  474. default:
  475. /* unknown tag: skip it */
  476. avio_skip(pb, tag_size - 10);
  477. break;
  478. }
  479. }
  480. header_end:
  481. rm->nb_packets = avio_rb32(pb); /* number of packets */
  482. if (!rm->nb_packets && (flags & 4))
  483. rm->nb_packets = 3600 * 25;
  484. avio_rb32(pb); /* next data header */
  485. if (!data_off)
  486. data_off = avio_tell(pb) - 18;
  487. if (indx_off && pb->seekable && !(s->flags & AVFMT_FLAG_IGNIDX) &&
  488. avio_seek(pb, indx_off, SEEK_SET) >= 0) {
  489. rm_read_index(s);
  490. avio_seek(pb, data_off + 18, SEEK_SET);
  491. }
  492. return 0;
  493. }
  494. static int get_num(AVIOContext *pb, int *len)
  495. {
  496. int n, n1;
  497. n = avio_rb16(pb);
  498. (*len)-=2;
  499. n &= 0x7FFF;
  500. if (n >= 0x4000) {
  501. return n - 0x4000;
  502. } else {
  503. n1 = avio_rb16(pb);
  504. (*len)-=2;
  505. return (n << 16) | n1;
  506. }
  507. }
  508. /* multiple of 20 bytes for ra144 (ugly) */
  509. #define RAW_PACKET_SIZE 1000
  510. static int sync(AVFormatContext *s, int64_t *timestamp, int *flags, int *stream_index, int64_t *pos){
  511. RMDemuxContext *rm = s->priv_data;
  512. AVIOContext *pb = s->pb;
  513. AVStream *st;
  514. uint32_t state=0xFFFFFFFF;
  515. while(!url_feof(pb)){
  516. int len, num, i;
  517. *pos= avio_tell(pb) - 3;
  518. if(rm->remaining_len > 0){
  519. num= rm->current_stream;
  520. len= rm->remaining_len;
  521. *timestamp = AV_NOPTS_VALUE;
  522. *flags= 0;
  523. }else{
  524. state= (state<<8) + avio_r8(pb);
  525. if(state == MKBETAG('I', 'N', 'D', 'X')){
  526. int n_pkts, expected_len;
  527. len = avio_rb32(pb);
  528. avio_skip(pb, 2);
  529. n_pkts = avio_rb32(pb);
  530. expected_len = 20 + n_pkts * 14;
  531. if (len == 20)
  532. /* some files don't add index entries to chunk size... */
  533. len = expected_len;
  534. else if (len != expected_len)
  535. av_log(s, AV_LOG_WARNING,
  536. "Index size %d (%d pkts) is wrong, should be %d.\n",
  537. len, n_pkts, expected_len);
  538. len -= 14; // we already read part of the index header
  539. if(len<0)
  540. continue;
  541. goto skip;
  542. } else if (state == MKBETAG('D','A','T','A')) {
  543. av_log(s, AV_LOG_WARNING,
  544. "DATA tag in middle of chunk, file may be broken.\n");
  545. }
  546. if(state > (unsigned)0xFFFF || state <= 12)
  547. continue;
  548. len=state - 12;
  549. state= 0xFFFFFFFF;
  550. num = avio_rb16(pb);
  551. *timestamp = avio_rb32(pb);
  552. avio_r8(pb); /* reserved */
  553. *flags = avio_r8(pb); /* flags */
  554. }
  555. for(i=0;i<s->nb_streams;i++) {
  556. st = s->streams[i];
  557. if (num == st->id)
  558. break;
  559. }
  560. if (i == s->nb_streams) {
  561. skip:
  562. /* skip packet if unknown number */
  563. avio_skip(pb, len);
  564. rm->remaining_len = 0;
  565. continue;
  566. }
  567. *stream_index= i;
  568. return len;
  569. }
  570. return -1;
  571. }
  572. static int rm_assemble_video_frame(AVFormatContext *s, AVIOContext *pb,
  573. RMDemuxContext *rm, RMStream *vst,
  574. AVPacket *pkt, int len, int *pseq,
  575. int64_t *timestamp)
  576. {
  577. int hdr, seq, pic_num, len2, pos;
  578. int type;
  579. hdr = avio_r8(pb); len--;
  580. type = hdr >> 6;
  581. if(type != 3){ // not frame as a part of packet
  582. seq = avio_r8(pb); len--;
  583. }
  584. if(type != 1){ // not whole frame
  585. len2 = get_num(pb, &len);
  586. pos = get_num(pb, &len);
  587. pic_num = avio_r8(pb); len--;
  588. }
  589. if(len<0)
  590. return -1;
  591. rm->remaining_len = len;
  592. if(type&1){ // frame, not slice
  593. if(type == 3){ // frame as a part of packet
  594. len= len2;
  595. *timestamp = pos;
  596. }
  597. if(rm->remaining_len < len)
  598. return -1;
  599. rm->remaining_len -= len;
  600. if(av_new_packet(pkt, len + 9) < 0)
  601. return AVERROR(EIO);
  602. pkt->data[0] = 0;
  603. AV_WL32(pkt->data + 1, 1);
  604. AV_WL32(pkt->data + 5, 0);
  605. avio_read(pb, pkt->data + 9, len);
  606. return 0;
  607. }
  608. //now we have to deal with single slice
  609. *pseq = seq;
  610. if((seq & 0x7F) == 1 || vst->curpic_num != pic_num){
  611. vst->slices = ((hdr & 0x3F) << 1) + 1;
  612. vst->videobufsize = len2 + 8*vst->slices + 1;
  613. av_free_packet(&vst->pkt); //FIXME this should be output.
  614. if(av_new_packet(&vst->pkt, vst->videobufsize) < 0)
  615. return AVERROR(ENOMEM);
  616. vst->videobufpos = 8*vst->slices + 1;
  617. vst->cur_slice = 0;
  618. vst->curpic_num = pic_num;
  619. vst->pktpos = avio_tell(pb);
  620. }
  621. if(type == 2)
  622. len = FFMIN(len, pos);
  623. if(++vst->cur_slice > vst->slices)
  624. return 1;
  625. AV_WL32(vst->pkt.data - 7 + 8*vst->cur_slice, 1);
  626. AV_WL32(vst->pkt.data - 3 + 8*vst->cur_slice, vst->videobufpos - 8*vst->slices - 1);
  627. if(vst->videobufpos + len > vst->videobufsize)
  628. return 1;
  629. if (avio_read(pb, vst->pkt.data + vst->videobufpos, len) != len)
  630. return AVERROR(EIO);
  631. vst->videobufpos += len;
  632. rm->remaining_len-= len;
  633. if (type == 2 || vst->videobufpos == vst->videobufsize) {
  634. vst->pkt.data[0] = vst->cur_slice-1;
  635. *pkt= vst->pkt;
  636. vst->pkt.data= NULL;
  637. vst->pkt.size= 0;
  638. if(vst->slices != vst->cur_slice) //FIXME find out how to set slices correct from the begin
  639. memmove(pkt->data + 1 + 8*vst->cur_slice, pkt->data + 1 + 8*vst->slices,
  640. vst->videobufpos - 1 - 8*vst->slices);
  641. pkt->size = vst->videobufpos + 8*(vst->cur_slice - vst->slices);
  642. pkt->pts = AV_NOPTS_VALUE;
  643. pkt->pos = vst->pktpos;
  644. vst->slices = 0;
  645. return 0;
  646. }
  647. return 1;
  648. }
  649. static inline void
  650. rm_ac3_swap_bytes (AVStream *st, AVPacket *pkt)
  651. {
  652. uint8_t *ptr;
  653. int j;
  654. if (st->codec->codec_id == CODEC_ID_AC3) {
  655. ptr = pkt->data;
  656. for (j=0;j<pkt->size;j+=2) {
  657. FFSWAP(int, ptr[0], ptr[1]);
  658. ptr += 2;
  659. }
  660. }
  661. }
  662. /**
  663. * Perform 4-bit block reordering for SIPR data.
  664. * @todo This can be optimized, e.g. use memcpy() if data blocks are aligned
  665. */
  666. void ff_rm_reorder_sipr_data(uint8_t *buf, int sub_packet_h, int framesize)
  667. {
  668. int n, bs = sub_packet_h * framesize * 2 / 96; // nibbles per subpacket
  669. for (n = 0; n < 38; n++) {
  670. int j;
  671. int i = bs * sipr_swaps[n][0];
  672. int o = bs * sipr_swaps[n][1];
  673. /* swap 4bit-nibbles of block 'i' with 'o' */
  674. for (j = 0; j < bs; j++, i++, o++) {
  675. int x = (buf[i >> 1] >> (4 * (i & 1))) & 0xF,
  676. y = (buf[o >> 1] >> (4 * (o & 1))) & 0xF;
  677. buf[o >> 1] = (x << (4 * (o & 1))) |
  678. (buf[o >> 1] & (0xF << (4 * !(o & 1))));
  679. buf[i >> 1] = (y << (4 * (i & 1))) |
  680. (buf[i >> 1] & (0xF << (4 * !(i & 1))));
  681. }
  682. }
  683. }
  684. int
  685. ff_rm_parse_packet (AVFormatContext *s, AVIOContext *pb,
  686. AVStream *st, RMStream *ast, int len, AVPacket *pkt,
  687. int *seq, int flags, int64_t timestamp)
  688. {
  689. RMDemuxContext *rm = s->priv_data;
  690. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  691. rm->current_stream= st->id;
  692. if(rm_assemble_video_frame(s, pb, rm, ast, pkt, len, seq, &timestamp))
  693. return -1; //got partial frame
  694. } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  695. if ((ast->deint_id == DEINT_ID_GENR) ||
  696. (ast->deint_id == DEINT_ID_INT4) ||
  697. (ast->deint_id == DEINT_ID_SIPR)) {
  698. int x;
  699. int sps = ast->sub_packet_size;
  700. int cfs = ast->coded_framesize;
  701. int h = ast->sub_packet_h;
  702. int y = ast->sub_packet_cnt;
  703. int w = ast->audio_framesize;
  704. if (flags & 2)
  705. y = ast->sub_packet_cnt = 0;
  706. if (!y)
  707. ast->audiotimestamp = timestamp;
  708. switch (ast->deint_id) {
  709. case DEINT_ID_INT4:
  710. for (x = 0; x < h/2; x++)
  711. avio_read(pb, ast->pkt.data+x*2*w+y*cfs, cfs);
  712. break;
  713. case DEINT_ID_GENR:
  714. for (x = 0; x < w/sps; x++)
  715. avio_read(pb, ast->pkt.data+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), sps);
  716. break;
  717. case DEINT_ID_SIPR:
  718. avio_read(pb, ast->pkt.data + y * w, w);
  719. break;
  720. }
  721. if (++(ast->sub_packet_cnt) < h)
  722. return -1;
  723. if (ast->deint_id == DEINT_ID_SIPR)
  724. ff_rm_reorder_sipr_data(ast->pkt.data, h, w);
  725. ast->sub_packet_cnt = 0;
  726. rm->audio_stream_num = st->index;
  727. rm->audio_pkt_cnt = h * w / st->codec->block_align;
  728. } else if ((ast->deint_id == DEINT_ID_VBRF) ||
  729. (ast->deint_id == DEINT_ID_VBRS)) {
  730. int x;
  731. rm->audio_stream_num = st->index;
  732. ast->sub_packet_cnt = (avio_rb16(pb) & 0xf0) >> 4;
  733. if (ast->sub_packet_cnt) {
  734. for (x = 0; x < ast->sub_packet_cnt; x++)
  735. ast->sub_packet_lengths[x] = avio_rb16(pb);
  736. rm->audio_pkt_cnt = ast->sub_packet_cnt;
  737. ast->audiotimestamp = timestamp;
  738. } else
  739. return -1;
  740. } else {
  741. av_get_packet(pb, pkt, len);
  742. rm_ac3_swap_bytes(st, pkt);
  743. }
  744. } else
  745. av_get_packet(pb, pkt, len);
  746. pkt->stream_index = st->index;
  747. #if 0
  748. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  749. if(st->codec->codec_id == CODEC_ID_RV20){
  750. int seq= 128*(pkt->data[2]&0x7F) + (pkt->data[3]>>1);
  751. av_log(s, AV_LOG_DEBUG, "%d %"PRId64" %d\n", *timestamp, *timestamp*512LL/25, seq);
  752. seq |= (timestamp&~0x3FFF);
  753. if(seq - timestamp > 0x2000) seq -= 0x4000;
  754. if(seq - timestamp < -0x2000) seq += 0x4000;
  755. }
  756. }
  757. #endif
  758. pkt->pts = timestamp;
  759. if (flags & 2)
  760. pkt->flags |= AV_PKT_FLAG_KEY;
  761. return st->codec->codec_type == AVMEDIA_TYPE_AUDIO ? rm->audio_pkt_cnt : 0;
  762. }
  763. int
  764. ff_rm_retrieve_cache (AVFormatContext *s, AVIOContext *pb,
  765. AVStream *st, RMStream *ast, AVPacket *pkt)
  766. {
  767. RMDemuxContext *rm = s->priv_data;
  768. assert (rm->audio_pkt_cnt > 0);
  769. if (ast->deint_id == DEINT_ID_VBRF ||
  770. ast->deint_id == DEINT_ID_VBRS)
  771. av_get_packet(pb, pkt, ast->sub_packet_lengths[ast->sub_packet_cnt - rm->audio_pkt_cnt]);
  772. else {
  773. av_new_packet(pkt, st->codec->block_align);
  774. memcpy(pkt->data, ast->pkt.data + st->codec->block_align * //FIXME avoid this
  775. (ast->sub_packet_h * ast->audio_framesize / st->codec->block_align - rm->audio_pkt_cnt),
  776. st->codec->block_align);
  777. }
  778. rm->audio_pkt_cnt--;
  779. if ((pkt->pts = ast->audiotimestamp) != AV_NOPTS_VALUE) {
  780. ast->audiotimestamp = AV_NOPTS_VALUE;
  781. pkt->flags = AV_PKT_FLAG_KEY;
  782. } else
  783. pkt->flags = 0;
  784. pkt->stream_index = st->index;
  785. return rm->audio_pkt_cnt;
  786. }
  787. static int rm_read_packet(AVFormatContext *s, AVPacket *pkt)
  788. {
  789. RMDemuxContext *rm = s->priv_data;
  790. AVStream *st;
  791. int i, len, res, seq = 1;
  792. int64_t timestamp, pos;
  793. int flags;
  794. for (;;) {
  795. if (rm->audio_pkt_cnt) {
  796. // If there are queued audio packet return them first
  797. st = s->streams[rm->audio_stream_num];
  798. ff_rm_retrieve_cache(s, s->pb, st, st->priv_data, pkt);
  799. flags = 0;
  800. } else {
  801. if (rm->old_format) {
  802. RMStream *ast;
  803. st = s->streams[0];
  804. ast = st->priv_data;
  805. timestamp = AV_NOPTS_VALUE;
  806. len = !ast->audio_framesize ? RAW_PACKET_SIZE :
  807. ast->coded_framesize * ast->sub_packet_h / 2;
  808. flags = (seq++ == 1) ? 2 : 0;
  809. pos = avio_tell(s->pb);
  810. } else {
  811. len=sync(s, &timestamp, &flags, &i, &pos);
  812. if (len > 0)
  813. st = s->streams[i];
  814. }
  815. if(len<0 || url_feof(s->pb))
  816. return AVERROR(EIO);
  817. res = ff_rm_parse_packet (s, s->pb, st, st->priv_data, len, pkt,
  818. &seq, flags, timestamp);
  819. if((flags&2) && (seq&0x7F) == 1)
  820. av_add_index_entry(st, pos, timestamp, 0, 0, AVINDEX_KEYFRAME);
  821. if (res)
  822. continue;
  823. }
  824. if( (st->discard >= AVDISCARD_NONKEY && !(flags&2))
  825. || st->discard >= AVDISCARD_ALL){
  826. av_free_packet(pkt);
  827. } else
  828. break;
  829. }
  830. return 0;
  831. }
  832. static int rm_read_close(AVFormatContext *s)
  833. {
  834. int i;
  835. for (i=0;i<s->nb_streams;i++)
  836. ff_rm_free_rmstream(s->streams[i]->priv_data);
  837. return 0;
  838. }
  839. static int rm_probe(AVProbeData *p)
  840. {
  841. /* check file header */
  842. if ((p->buf[0] == '.' && p->buf[1] == 'R' &&
  843. p->buf[2] == 'M' && p->buf[3] == 'F' &&
  844. p->buf[4] == 0 && p->buf[5] == 0) ||
  845. (p->buf[0] == '.' && p->buf[1] == 'r' &&
  846. p->buf[2] == 'a' && p->buf[3] == 0xfd))
  847. return AVPROBE_SCORE_MAX;
  848. else
  849. return 0;
  850. }
  851. static int64_t rm_read_dts(AVFormatContext *s, int stream_index,
  852. int64_t *ppos, int64_t pos_limit)
  853. {
  854. RMDemuxContext *rm = s->priv_data;
  855. int64_t pos, dts;
  856. int stream_index2, flags, len, h;
  857. pos = *ppos;
  858. if(rm->old_format)
  859. return AV_NOPTS_VALUE;
  860. if (avio_seek(s->pb, pos, SEEK_SET) < 0)
  861. return AV_NOPTS_VALUE;
  862. rm->remaining_len=0;
  863. for(;;){
  864. int seq=1;
  865. AVStream *st;
  866. len=sync(s, &dts, &flags, &stream_index2, &pos);
  867. if(len<0)
  868. return AV_NOPTS_VALUE;
  869. st = s->streams[stream_index2];
  870. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  871. h= avio_r8(s->pb); len--;
  872. if(!(h & 0x40)){
  873. seq = avio_r8(s->pb); len--;
  874. }
  875. }
  876. if((flags&2) && (seq&0x7F) == 1){
  877. // av_log(s, AV_LOG_DEBUG, "%d %d-%d %"PRId64" %d\n", flags, stream_index2, stream_index, dts, seq);
  878. av_add_index_entry(st, pos, dts, 0, 0, AVINDEX_KEYFRAME);
  879. if(stream_index2 == stream_index)
  880. break;
  881. }
  882. avio_skip(s->pb, len);
  883. }
  884. *ppos = pos;
  885. return dts;
  886. }
  887. static int rm_read_seek(AVFormatContext *s, int stream_index,
  888. int64_t pts, int flags)
  889. {
  890. RMDemuxContext *rm = s->priv_data;
  891. if (ff_seek_frame_binary(s, stream_index, pts, flags) < 0)
  892. return -1;
  893. rm->audio_pkt_cnt = 0;
  894. return 0;
  895. }
  896. AVInputFormat ff_rm_demuxer = {
  897. .name = "rm",
  898. .long_name = NULL_IF_CONFIG_SMALL("RealMedia format"),
  899. .priv_data_size = sizeof(RMDemuxContext),
  900. .read_probe = rm_probe,
  901. .read_header = rm_read_header,
  902. .read_packet = rm_read_packet,
  903. .read_close = rm_read_close,
  904. .read_timestamp = rm_read_dts,
  905. .read_seek = rm_read_seek,
  906. };
  907. AVInputFormat ff_rdt_demuxer = {
  908. .name = "rdt",
  909. .long_name = NULL_IF_CONFIG_SMALL("RDT demuxer"),
  910. .priv_data_size = sizeof(RMDemuxContext),
  911. .read_close = rm_read_close,
  912. .flags = AVFMT_NOFILE,
  913. };