You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

945 lines
31KB

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