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.

1103 lines
35KB

  1. /*
  2. * "Real" compatible mux and demux.
  3. * Copyright (c) 2000, 2001 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "avformat.h"
  20. /* in ms */
  21. #define BUFFER_DURATION 0
  22. typedef struct {
  23. int nb_packets;
  24. int packet_total_size;
  25. int packet_max_size;
  26. /* codec related output */
  27. int bit_rate;
  28. float frame_rate;
  29. int nb_frames; /* current frame number */
  30. int total_frames; /* total number of frames */
  31. int num;
  32. AVCodecContext *enc;
  33. } StreamInfo;
  34. typedef struct {
  35. StreamInfo streams[2];
  36. StreamInfo *audio_stream, *video_stream;
  37. int data_pos; /* position of the data after the header */
  38. int nb_packets;
  39. int old_format;
  40. int current_stream;
  41. int remaining_len;
  42. /// Audio descrambling matrix parameters
  43. uint8_t *audiobuf; ///< place to store reordered audio data
  44. int64_t audiotimestamp; ///< Audio packet timestamp
  45. int sub_packet_cnt; // Subpacket counter, used while reading
  46. int sub_packet_size, sub_packet_h, coded_framesize; ///< Descrambling parameters from container
  47. int audio_stream_num; ///< Stream number for audio packets
  48. int audio_pkt_cnt; ///< Output packet counter
  49. int audio_framesize; /// Audio frame size from container
  50. } RMContext;
  51. #ifdef CONFIG_MUXERS
  52. static void put_str(ByteIOContext *s, const char *tag)
  53. {
  54. put_be16(s,strlen(tag));
  55. while (*tag) {
  56. put_byte(s, *tag++);
  57. }
  58. }
  59. static void put_str8(ByteIOContext *s, const char *tag)
  60. {
  61. put_byte(s, strlen(tag));
  62. while (*tag) {
  63. put_byte(s, *tag++);
  64. }
  65. }
  66. static void rv10_write_header(AVFormatContext *ctx,
  67. int data_size, int index_pos)
  68. {
  69. RMContext *rm = ctx->priv_data;
  70. ByteIOContext *s = &ctx->pb;
  71. StreamInfo *stream;
  72. unsigned char *data_offset_ptr, *start_ptr;
  73. const char *desc, *mimetype;
  74. int nb_packets, packet_total_size, packet_max_size, size, packet_avg_size, i;
  75. int bit_rate, v, duration, flags, data_pos;
  76. start_ptr = s->buf_ptr;
  77. put_tag(s, ".RMF");
  78. put_be32(s,18); /* header size */
  79. put_be16(s,0);
  80. put_be32(s,0);
  81. put_be32(s,4 + ctx->nb_streams); /* num headers */
  82. put_tag(s,"PROP");
  83. put_be32(s, 50);
  84. put_be16(s, 0);
  85. packet_max_size = 0;
  86. packet_total_size = 0;
  87. nb_packets = 0;
  88. bit_rate = 0;
  89. duration = 0;
  90. for(i=0;i<ctx->nb_streams;i++) {
  91. StreamInfo *stream = &rm->streams[i];
  92. bit_rate += stream->bit_rate;
  93. if (stream->packet_max_size > packet_max_size)
  94. packet_max_size = stream->packet_max_size;
  95. nb_packets += stream->nb_packets;
  96. packet_total_size += stream->packet_total_size;
  97. /* select maximum duration */
  98. v = (int) (1000.0 * (float)stream->total_frames / stream->frame_rate);
  99. if (v > duration)
  100. duration = v;
  101. }
  102. put_be32(s, bit_rate); /* max bit rate */
  103. put_be32(s, bit_rate); /* avg bit rate */
  104. put_be32(s, packet_max_size); /* max packet size */
  105. if (nb_packets > 0)
  106. packet_avg_size = packet_total_size / nb_packets;
  107. else
  108. packet_avg_size = 0;
  109. put_be32(s, packet_avg_size); /* avg packet size */
  110. put_be32(s, nb_packets); /* num packets */
  111. put_be32(s, duration); /* duration */
  112. put_be32(s, BUFFER_DURATION); /* preroll */
  113. put_be32(s, index_pos); /* index offset */
  114. /* computation of data the data offset */
  115. data_offset_ptr = s->buf_ptr;
  116. put_be32(s, 0); /* data offset : will be patched after */
  117. put_be16(s, ctx->nb_streams); /* num streams */
  118. flags = 1 | 2; /* save allowed & perfect play */
  119. if (url_is_streamed(s))
  120. flags |= 4; /* live broadcast */
  121. put_be16(s, flags);
  122. /* comments */
  123. put_tag(s,"CONT");
  124. size = strlen(ctx->title) + strlen(ctx->author) + strlen(ctx->copyright) +
  125. strlen(ctx->comment) + 4 * 2 + 10;
  126. put_be32(s,size);
  127. put_be16(s,0);
  128. put_str(s, ctx->title);
  129. put_str(s, ctx->author);
  130. put_str(s, ctx->copyright);
  131. put_str(s, ctx->comment);
  132. for(i=0;i<ctx->nb_streams;i++) {
  133. int codec_data_size;
  134. stream = &rm->streams[i];
  135. if (stream->enc->codec_type == CODEC_TYPE_AUDIO) {
  136. desc = "The Audio Stream";
  137. mimetype = "audio/x-pn-realaudio";
  138. codec_data_size = 73;
  139. } else {
  140. desc = "The Video Stream";
  141. mimetype = "video/x-pn-realvideo";
  142. codec_data_size = 34;
  143. }
  144. put_tag(s,"MDPR");
  145. size = 10 + 9 * 4 + strlen(desc) + strlen(mimetype) + codec_data_size;
  146. put_be32(s, size);
  147. put_be16(s, 0);
  148. put_be16(s, i); /* stream number */
  149. put_be32(s, stream->bit_rate); /* max bit rate */
  150. put_be32(s, stream->bit_rate); /* avg bit rate */
  151. put_be32(s, stream->packet_max_size); /* max packet size */
  152. if (stream->nb_packets > 0)
  153. packet_avg_size = stream->packet_total_size /
  154. stream->nb_packets;
  155. else
  156. packet_avg_size = 0;
  157. put_be32(s, packet_avg_size); /* avg packet size */
  158. put_be32(s, 0); /* start time */
  159. put_be32(s, BUFFER_DURATION); /* preroll */
  160. /* duration */
  161. if (url_is_streamed(s) || !stream->total_frames)
  162. put_be32(s, (int)(3600 * 1000));
  163. else
  164. put_be32(s, (int)(stream->total_frames * 1000 / stream->frame_rate));
  165. put_str8(s, desc);
  166. put_str8(s, mimetype);
  167. put_be32(s, codec_data_size);
  168. if (stream->enc->codec_type == CODEC_TYPE_AUDIO) {
  169. int coded_frame_size, fscode, sample_rate;
  170. sample_rate = stream->enc->sample_rate;
  171. coded_frame_size = (stream->enc->bit_rate *
  172. stream->enc->frame_size) / (8 * sample_rate);
  173. /* audio codec info */
  174. put_tag(s, ".ra");
  175. put_byte(s, 0xfd);
  176. put_be32(s, 0x00040000); /* version */
  177. put_tag(s, ".ra4");
  178. put_be32(s, 0x01b53530); /* stream length */
  179. put_be16(s, 4); /* unknown */
  180. put_be32(s, 0x39); /* header size */
  181. switch(sample_rate) {
  182. case 48000:
  183. case 24000:
  184. case 12000:
  185. fscode = 1;
  186. break;
  187. default:
  188. case 44100:
  189. case 22050:
  190. case 11025:
  191. fscode = 2;
  192. break;
  193. case 32000:
  194. case 16000:
  195. case 8000:
  196. fscode = 3;
  197. }
  198. put_be16(s, fscode); /* codec additional info, for AC3, seems
  199. to be a frequency code */
  200. /* special hack to compensate rounding errors... */
  201. if (coded_frame_size == 557)
  202. coded_frame_size--;
  203. put_be32(s, coded_frame_size); /* frame length */
  204. put_be32(s, 0x51540); /* unknown */
  205. put_be32(s, 0x249f0); /* unknown */
  206. put_be32(s, 0x249f0); /* unknown */
  207. put_be16(s, 0x01);
  208. /* frame length : seems to be very important */
  209. put_be16(s, coded_frame_size);
  210. put_be32(s, 0); /* unknown */
  211. put_be16(s, stream->enc->sample_rate); /* sample rate */
  212. put_be32(s, 0x10); /* unknown */
  213. put_be16(s, stream->enc->channels);
  214. put_str8(s, "Int0"); /* codec name */
  215. put_str8(s, "dnet"); /* codec name */
  216. put_be16(s, 0); /* title length */
  217. put_be16(s, 0); /* author length */
  218. put_be16(s, 0); /* copyright length */
  219. put_byte(s, 0); /* end of header */
  220. } else {
  221. /* video codec info */
  222. put_be32(s,34); /* size */
  223. if(stream->enc->codec_id == CODEC_ID_RV10)
  224. put_tag(s,"VIDORV10");
  225. else
  226. put_tag(s,"VIDORV20");
  227. put_be16(s, stream->enc->width);
  228. put_be16(s, stream->enc->height);
  229. put_be16(s, (int) stream->frame_rate); /* frames per seconds ? */
  230. put_be32(s,0); /* unknown meaning */
  231. put_be16(s, (int) stream->frame_rate); /* unknown meaning */
  232. put_be32(s,0); /* unknown meaning */
  233. put_be16(s, 8); /* unknown meaning */
  234. /* Seems to be the codec version: only use basic H263. The next
  235. versions seems to add a diffential DC coding as in
  236. MPEG... nothing new under the sun */
  237. if(stream->enc->codec_id == CODEC_ID_RV10)
  238. put_be32(s,0x10000000);
  239. else
  240. put_be32(s,0x20103001);
  241. //put_be32(s,0x10003000);
  242. }
  243. }
  244. /* patch data offset field */
  245. data_pos = s->buf_ptr - start_ptr;
  246. rm->data_pos = data_pos;
  247. data_offset_ptr[0] = data_pos >> 24;
  248. data_offset_ptr[1] = data_pos >> 16;
  249. data_offset_ptr[2] = data_pos >> 8;
  250. data_offset_ptr[3] = data_pos;
  251. /* data stream */
  252. put_tag(s,"DATA");
  253. put_be32(s,data_size + 10 + 8);
  254. put_be16(s,0);
  255. put_be32(s, nb_packets); /* number of packets */
  256. put_be32(s,0); /* next data header */
  257. }
  258. static void write_packet_header(AVFormatContext *ctx, StreamInfo *stream,
  259. int length, int key_frame)
  260. {
  261. int timestamp;
  262. ByteIOContext *s = &ctx->pb;
  263. stream->nb_packets++;
  264. stream->packet_total_size += length;
  265. if (length > stream->packet_max_size)
  266. stream->packet_max_size = length;
  267. put_be16(s,0); /* version */
  268. put_be16(s,length + 12);
  269. put_be16(s, stream->num); /* stream number */
  270. timestamp = (1000 * (float)stream->nb_frames) / stream->frame_rate;
  271. put_be32(s, timestamp); /* timestamp */
  272. put_byte(s, 0); /* reserved */
  273. put_byte(s, key_frame ? 2 : 0); /* flags */
  274. }
  275. static int rm_write_header(AVFormatContext *s)
  276. {
  277. RMContext *rm = s->priv_data;
  278. StreamInfo *stream;
  279. int n;
  280. AVCodecContext *codec;
  281. for(n=0;n<s->nb_streams;n++) {
  282. s->streams[n]->id = n;
  283. codec = s->streams[n]->codec;
  284. stream = &rm->streams[n];
  285. memset(stream, 0, sizeof(StreamInfo));
  286. stream->num = n;
  287. stream->bit_rate = codec->bit_rate;
  288. stream->enc = codec;
  289. switch(codec->codec_type) {
  290. case CODEC_TYPE_AUDIO:
  291. rm->audio_stream = stream;
  292. stream->frame_rate = (float)codec->sample_rate / (float)codec->frame_size;
  293. /* XXX: dummy values */
  294. stream->packet_max_size = 1024;
  295. stream->nb_packets = 0;
  296. stream->total_frames = stream->nb_packets;
  297. break;
  298. case CODEC_TYPE_VIDEO:
  299. rm->video_stream = stream;
  300. stream->frame_rate = (float)codec->time_base.den / (float)codec->time_base.num;
  301. /* XXX: dummy values */
  302. stream->packet_max_size = 4096;
  303. stream->nb_packets = 0;
  304. stream->total_frames = stream->nb_packets;
  305. break;
  306. default:
  307. return -1;
  308. }
  309. }
  310. rv10_write_header(s, 0, 0);
  311. put_flush_packet(&s->pb);
  312. return 0;
  313. }
  314. static int rm_write_audio(AVFormatContext *s, const uint8_t *buf, int size, int flags)
  315. {
  316. uint8_t *buf1;
  317. RMContext *rm = s->priv_data;
  318. ByteIOContext *pb = &s->pb;
  319. StreamInfo *stream = rm->audio_stream;
  320. int i;
  321. /* XXX: suppress this malloc */
  322. buf1= (uint8_t*) av_malloc( size * sizeof(uint8_t) );
  323. write_packet_header(s, stream, size, !!(flags & PKT_FLAG_KEY));
  324. /* for AC3, the words seems to be reversed */
  325. for(i=0;i<size;i+=2) {
  326. buf1[i] = buf[i+1];
  327. buf1[i+1] = buf[i];
  328. }
  329. put_buffer(pb, buf1, size);
  330. put_flush_packet(pb);
  331. stream->nb_frames++;
  332. av_free(buf1);
  333. return 0;
  334. }
  335. static int rm_write_video(AVFormatContext *s, const uint8_t *buf, int size, int flags)
  336. {
  337. RMContext *rm = s->priv_data;
  338. ByteIOContext *pb = &s->pb;
  339. StreamInfo *stream = rm->video_stream;
  340. int key_frame = !!(flags & PKT_FLAG_KEY);
  341. /* XXX: this is incorrect: should be a parameter */
  342. /* Well, I spent some time finding the meaning of these bits. I am
  343. not sure I understood everything, but it works !! */
  344. #if 1
  345. write_packet_header(s, stream, size + 7, key_frame);
  346. /* bit 7: '1' if final packet of a frame converted in several packets */
  347. put_byte(pb, 0x81);
  348. /* bit 7: '1' if I frame. bits 6..0 : sequence number in current
  349. frame starting from 1 */
  350. if (key_frame) {
  351. put_byte(pb, 0x81);
  352. } else {
  353. put_byte(pb, 0x01);
  354. }
  355. put_be16(pb, 0x4000 + (size)); /* total frame size */
  356. put_be16(pb, 0x4000 + (size)); /* offset from the start or the end */
  357. #else
  358. /* full frame */
  359. write_packet_header(s, size + 6);
  360. put_byte(pb, 0xc0);
  361. put_be16(pb, 0x4000 + size); /* total frame size */
  362. put_be16(pb, 0x4000 + packet_number * 126); /* position in stream */
  363. #endif
  364. put_byte(pb, stream->nb_frames & 0xff);
  365. put_buffer(pb, buf, size);
  366. put_flush_packet(pb);
  367. stream->nb_frames++;
  368. return 0;
  369. }
  370. static int rm_write_packet(AVFormatContext *s, AVPacket *pkt)
  371. {
  372. if (s->streams[pkt->stream_index]->codec->codec_type ==
  373. CODEC_TYPE_AUDIO)
  374. return rm_write_audio(s, pkt->data, pkt->size, pkt->flags);
  375. else
  376. return rm_write_video(s, pkt->data, pkt->size, pkt->flags);
  377. }
  378. static int rm_write_trailer(AVFormatContext *s)
  379. {
  380. RMContext *rm = s->priv_data;
  381. int data_size, index_pos, i;
  382. ByteIOContext *pb = &s->pb;
  383. if (!url_is_streamed(&s->pb)) {
  384. /* end of file: finish to write header */
  385. index_pos = url_fseek(pb, 0, SEEK_CUR);
  386. data_size = index_pos - rm->data_pos;
  387. /* index */
  388. put_tag(pb, "INDX");
  389. put_be32(pb, 10 + 10 * s->nb_streams);
  390. put_be16(pb, 0);
  391. for(i=0;i<s->nb_streams;i++) {
  392. put_be32(pb, 0); /* zero indices */
  393. put_be16(pb, i); /* stream number */
  394. put_be32(pb, 0); /* next index */
  395. }
  396. /* undocumented end header */
  397. put_be32(pb, 0);
  398. put_be32(pb, 0);
  399. url_fseek(pb, 0, SEEK_SET);
  400. for(i=0;i<s->nb_streams;i++)
  401. rm->streams[i].total_frames = rm->streams[i].nb_frames;
  402. rv10_write_header(s, data_size, index_pos);
  403. } else {
  404. /* undocumented end header */
  405. put_be32(pb, 0);
  406. put_be32(pb, 0);
  407. }
  408. put_flush_packet(pb);
  409. return 0;
  410. }
  411. #endif //CONFIG_MUXERS
  412. /***************************************************/
  413. static void get_str(ByteIOContext *pb, char *buf, int buf_size)
  414. {
  415. int len, i;
  416. char *q;
  417. len = get_be16(pb);
  418. q = buf;
  419. for(i=0;i<len;i++) {
  420. if (i < buf_size - 1)
  421. *q++ = get_byte(pb);
  422. }
  423. *q = '\0';
  424. }
  425. static void get_str8(ByteIOContext *pb, char *buf, int buf_size)
  426. {
  427. int len, i;
  428. char *q;
  429. len = get_byte(pb);
  430. q = buf;
  431. for(i=0;i<len;i++) {
  432. if (i < buf_size - 1)
  433. *q++ = get_byte(pb);
  434. }
  435. *q = '\0';
  436. }
  437. static void rm_read_audio_stream_info(AVFormatContext *s, AVStream *st,
  438. int read_all)
  439. {
  440. RMContext *rm = s->priv_data;
  441. ByteIOContext *pb = &s->pb;
  442. char buf[256];
  443. uint32_t version;
  444. int i;
  445. /* ra type header */
  446. version = get_be32(pb); /* version */
  447. if (((version >> 16) & 0xff) == 3) {
  448. int64_t startpos = url_ftell(pb);
  449. /* very old version */
  450. for(i = 0; i < 14; i++)
  451. get_byte(pb);
  452. get_str8(pb, s->title, sizeof(s->title));
  453. get_str8(pb, s->author, sizeof(s->author));
  454. get_str8(pb, s->copyright, sizeof(s->copyright));
  455. get_str8(pb, s->comment, sizeof(s->comment));
  456. if ((startpos + (version & 0xffff)) >= url_ftell(pb) + 2) {
  457. // fourcc (should always be "lpcJ")
  458. get_byte(pb);
  459. get_str8(pb, buf, sizeof(buf));
  460. }
  461. // Skip extra header crap (this should never happen)
  462. if ((startpos + (version & 0xffff)) > url_ftell(pb))
  463. url_fskip(pb, (version & 0xffff) + startpos - url_ftell(pb));
  464. st->codec->sample_rate = 8000;
  465. st->codec->channels = 1;
  466. st->codec->codec_type = CODEC_TYPE_AUDIO;
  467. st->codec->codec_id = CODEC_ID_RA_144;
  468. } else {
  469. int flavor, sub_packet_h, coded_framesize, sub_packet_size;
  470. /* old version (4) */
  471. get_be32(pb); /* .ra4 */
  472. get_be32(pb); /* data size */
  473. get_be16(pb); /* version2 */
  474. get_be32(pb); /* header size */
  475. flavor= get_be16(pb); /* add codec info / flavor */
  476. rm->coded_framesize = coded_framesize = get_be32(pb); /* coded frame size */
  477. get_be32(pb); /* ??? */
  478. get_be32(pb); /* ??? */
  479. get_be32(pb); /* ??? */
  480. rm->sub_packet_h = sub_packet_h = get_be16(pb); /* 1 */
  481. st->codec->block_align= get_be16(pb); /* frame size */
  482. rm->sub_packet_size = sub_packet_size = get_be16(pb); /* sub packet size */
  483. get_be16(pb); /* ??? */
  484. if (((version >> 16) & 0xff) == 5) {
  485. get_be16(pb); get_be16(pb); get_be16(pb); }
  486. st->codec->sample_rate = get_be16(pb);
  487. get_be32(pb);
  488. st->codec->channels = get_be16(pb);
  489. if (((version >> 16) & 0xff) == 5) {
  490. get_be32(pb);
  491. buf[0] = get_byte(pb);
  492. buf[1] = get_byte(pb);
  493. buf[2] = get_byte(pb);
  494. buf[3] = get_byte(pb);
  495. buf[4] = 0;
  496. } else {
  497. get_str8(pb, buf, sizeof(buf)); /* desc */
  498. get_str8(pb, buf, sizeof(buf)); /* desc */
  499. }
  500. st->codec->codec_type = CODEC_TYPE_AUDIO;
  501. if (!strcmp(buf, "dnet")) {
  502. st->codec->codec_id = CODEC_ID_AC3;
  503. } else if (!strcmp(buf, "28_8")) {
  504. st->codec->codec_id = CODEC_ID_RA_288;
  505. st->codec->extradata_size= 0;
  506. rm->audio_framesize = st->codec->block_align;
  507. st->codec->block_align = coded_framesize;
  508. rm->audiobuf = av_malloc(rm->audio_framesize * sub_packet_h);
  509. } else if (!strcmp(buf, "cook")) {
  510. int codecdata_length, i;
  511. get_be16(pb); get_byte(pb);
  512. if (((version >> 16) & 0xff) == 5)
  513. get_byte(pb);
  514. codecdata_length = get_be32(pb);
  515. st->codec->codec_id = CODEC_ID_COOK;
  516. st->codec->extradata_size= codecdata_length;
  517. st->codec->extradata= av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  518. for(i = 0; i < codecdata_length; i++)
  519. ((uint8_t*)st->codec->extradata)[i] = get_byte(pb);
  520. rm->audio_framesize = st->codec->block_align;
  521. st->codec->block_align = rm->sub_packet_size;
  522. rm->audiobuf = av_malloc(rm->audio_framesize * sub_packet_h);
  523. } else {
  524. st->codec->codec_id = CODEC_ID_NONE;
  525. pstrcpy(st->codec->codec_name, sizeof(st->codec->codec_name),
  526. buf);
  527. }
  528. if (read_all) {
  529. get_byte(pb);
  530. get_byte(pb);
  531. get_byte(pb);
  532. get_str8(pb, s->title, sizeof(s->title));
  533. get_str8(pb, s->author, sizeof(s->author));
  534. get_str8(pb, s->copyright, sizeof(s->copyright));
  535. get_str8(pb, s->comment, sizeof(s->comment));
  536. }
  537. }
  538. }
  539. static int rm_read_header_old(AVFormatContext *s, AVFormatParameters *ap)
  540. {
  541. RMContext *rm = s->priv_data;
  542. AVStream *st;
  543. rm->old_format = 1;
  544. st = av_new_stream(s, 0);
  545. if (!st)
  546. goto fail;
  547. rm_read_audio_stream_info(s, st, 1);
  548. return 0;
  549. fail:
  550. return -1;
  551. }
  552. static int rm_read_header(AVFormatContext *s, AVFormatParameters *ap)
  553. {
  554. RMContext *rm = s->priv_data;
  555. AVStream *st;
  556. ByteIOContext *pb = &s->pb;
  557. unsigned int tag, v;
  558. int tag_size, size, codec_data_size, i;
  559. int64_t codec_pos;
  560. unsigned int h263_hack_version, start_time, duration;
  561. char buf[128];
  562. int flags = 0;
  563. tag = get_le32(pb);
  564. if (tag == MKTAG('.', 'r', 'a', 0xfd)) {
  565. /* very old .ra format */
  566. return rm_read_header_old(s, ap);
  567. } else if (tag != MKTAG('.', 'R', 'M', 'F')) {
  568. return AVERROR_IO;
  569. }
  570. get_be32(pb); /* header size */
  571. get_be16(pb);
  572. get_be32(pb);
  573. get_be32(pb); /* number of headers */
  574. for(;;) {
  575. if (url_feof(pb))
  576. goto fail;
  577. tag = get_le32(pb);
  578. tag_size = get_be32(pb);
  579. get_be16(pb);
  580. #if 0
  581. printf("tag=%c%c%c%c (%08x) size=%d\n",
  582. (tag) & 0xff,
  583. (tag >> 8) & 0xff,
  584. (tag >> 16) & 0xff,
  585. (tag >> 24) & 0xff,
  586. tag,
  587. tag_size);
  588. #endif
  589. if (tag_size < 10 && tag != MKTAG('D', 'A', 'T', 'A'))
  590. goto fail;
  591. switch(tag) {
  592. case MKTAG('P', 'R', 'O', 'P'):
  593. /* file header */
  594. get_be32(pb); /* max bit rate */
  595. get_be32(pb); /* avg bit rate */
  596. get_be32(pb); /* max packet size */
  597. get_be32(pb); /* avg packet size */
  598. get_be32(pb); /* nb packets */
  599. get_be32(pb); /* duration */
  600. get_be32(pb); /* preroll */
  601. get_be32(pb); /* index offset */
  602. get_be32(pb); /* data offset */
  603. get_be16(pb); /* nb streams */
  604. flags = get_be16(pb); /* flags */
  605. break;
  606. case MKTAG('C', 'O', 'N', 'T'):
  607. get_str(pb, s->title, sizeof(s->title));
  608. get_str(pb, s->author, sizeof(s->author));
  609. get_str(pb, s->copyright, sizeof(s->copyright));
  610. get_str(pb, s->comment, sizeof(s->comment));
  611. break;
  612. case MKTAG('M', 'D', 'P', 'R'):
  613. st = av_new_stream(s, 0);
  614. if (!st)
  615. goto fail;
  616. st->id = get_be16(pb);
  617. get_be32(pb); /* max bit rate */
  618. st->codec->bit_rate = get_be32(pb); /* bit rate */
  619. get_be32(pb); /* max packet size */
  620. get_be32(pb); /* avg packet size */
  621. start_time = get_be32(pb); /* start time */
  622. get_be32(pb); /* preroll */
  623. duration = get_be32(pb); /* duration */
  624. st->start_time = start_time;
  625. st->duration = duration;
  626. get_str8(pb, buf, sizeof(buf)); /* desc */
  627. get_str8(pb, buf, sizeof(buf)); /* mimetype */
  628. codec_data_size = get_be32(pb);
  629. codec_pos = url_ftell(pb);
  630. st->codec->codec_type = CODEC_TYPE_DATA;
  631. av_set_pts_info(st, 64, 1, 1000);
  632. v = get_be32(pb);
  633. if (v == MKTAG(0xfd, 'a', 'r', '.')) {
  634. /* ra type header */
  635. rm_read_audio_stream_info(s, st, 0);
  636. } else {
  637. int fps, fps2;
  638. if (get_le32(pb) != MKTAG('V', 'I', 'D', 'O')) {
  639. fail1:
  640. av_log(st->codec, AV_LOG_ERROR, "Unsupported video codec\n");
  641. goto skip;
  642. }
  643. st->codec->codec_tag = get_le32(pb);
  644. // av_log(NULL, AV_LOG_DEBUG, "%X %X\n", st->codec->codec_tag, MKTAG('R', 'V', '2', '0'));
  645. if ( st->codec->codec_tag != MKTAG('R', 'V', '1', '0')
  646. && st->codec->codec_tag != MKTAG('R', 'V', '2', '0')
  647. && st->codec->codec_tag != MKTAG('R', 'V', '3', '0')
  648. && st->codec->codec_tag != MKTAG('R', 'V', '4', '0'))
  649. goto fail1;
  650. st->codec->width = get_be16(pb);
  651. st->codec->height = get_be16(pb);
  652. st->codec->time_base.num= 1;
  653. fps= get_be16(pb);
  654. st->codec->codec_type = CODEC_TYPE_VIDEO;
  655. get_be32(pb);
  656. fps2= get_be16(pb);
  657. get_be16(pb);
  658. st->codec->extradata_size= codec_data_size - (url_ftell(pb) - codec_pos);
  659. st->codec->extradata= av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  660. get_buffer(pb, st->codec->extradata, st->codec->extradata_size);
  661. // av_log(NULL, AV_LOG_DEBUG, "fps= %d fps2= %d\n", fps, fps2);
  662. st->codec->time_base.den = fps * st->codec->time_base.num;
  663. /* modification of h263 codec version (!) */
  664. #ifdef WORDS_BIGENDIAN
  665. h263_hack_version = ((uint32_t*)st->codec->extradata)[1];
  666. #else
  667. h263_hack_version = bswap_32(((uint32_t*)st->codec->extradata)[1]);
  668. #endif
  669. st->codec->sub_id = h263_hack_version;
  670. switch((h263_hack_version>>28)){
  671. case 1: st->codec->codec_id = CODEC_ID_RV10; break;
  672. case 2: st->codec->codec_id = CODEC_ID_RV20; break;
  673. case 3: st->codec->codec_id = CODEC_ID_RV30; break;
  674. case 4: st->codec->codec_id = CODEC_ID_RV40; break;
  675. default: goto fail1;
  676. }
  677. }
  678. skip:
  679. /* skip codec info */
  680. size = url_ftell(pb) - codec_pos;
  681. url_fskip(pb, codec_data_size - size);
  682. break;
  683. case MKTAG('D', 'A', 'T', 'A'):
  684. goto header_end;
  685. default:
  686. /* unknown tag: skip it */
  687. url_fskip(pb, tag_size - 10);
  688. break;
  689. }
  690. }
  691. header_end:
  692. rm->nb_packets = get_be32(pb); /* number of packets */
  693. if (!rm->nb_packets && (flags & 4))
  694. rm->nb_packets = 3600 * 25;
  695. get_be32(pb); /* next data header */
  696. return 0;
  697. fail:
  698. for(i=0;i<s->nb_streams;i++) {
  699. av_free(s->streams[i]);
  700. }
  701. return AVERROR_IO;
  702. }
  703. static int get_num(ByteIOContext *pb, int *len)
  704. {
  705. int n, n1;
  706. n = get_be16(pb);
  707. (*len)-=2;
  708. if (n >= 0x4000) {
  709. return n - 0x4000;
  710. } else {
  711. n1 = get_be16(pb);
  712. (*len)-=2;
  713. return (n << 16) | n1;
  714. }
  715. }
  716. /* multiple of 20 bytes for ra144 (ugly) */
  717. #define RAW_PACKET_SIZE 1000
  718. static int sync(AVFormatContext *s, int64_t *timestamp, int *flags, int *stream_index, int64_t *pos){
  719. RMContext *rm = s->priv_data;
  720. ByteIOContext *pb = &s->pb;
  721. int len, num, res, i;
  722. AVStream *st;
  723. uint32_t state=0xFFFFFFFF;
  724. while(!url_feof(pb)){
  725. *pos= url_ftell(pb);
  726. if(rm->remaining_len > 0){
  727. num= rm->current_stream;
  728. len= rm->remaining_len;
  729. *timestamp = AV_NOPTS_VALUE;
  730. *flags= 0;
  731. }else{
  732. state= (state<<8) + get_byte(pb);
  733. if(state == MKBETAG('I', 'N', 'D', 'X')){
  734. len = get_be16(pb) - 6;
  735. if(len<0)
  736. continue;
  737. goto skip;
  738. }
  739. if(state > (unsigned)0xFFFF || state < 12)
  740. continue;
  741. len=state;
  742. state= 0xFFFFFFFF;
  743. num = get_be16(pb);
  744. *timestamp = get_be32(pb);
  745. res= get_byte(pb); /* reserved */
  746. *flags = get_byte(pb); /* flags */
  747. len -= 12;
  748. }
  749. for(i=0;i<s->nb_streams;i++) {
  750. st = s->streams[i];
  751. if (num == st->id)
  752. break;
  753. }
  754. if (i == s->nb_streams) {
  755. skip:
  756. /* skip packet if unknown number */
  757. url_fskip(pb, len);
  758. rm->remaining_len -= len;
  759. continue;
  760. }
  761. *stream_index= i;
  762. return len;
  763. }
  764. return -1;
  765. }
  766. static int rm_read_packet(AVFormatContext *s, AVPacket *pkt)
  767. {
  768. RMContext *rm = s->priv_data;
  769. ByteIOContext *pb = &s->pb;
  770. AVStream *st;
  771. int i, len, tmp, j;
  772. int64_t timestamp, pos;
  773. uint8_t *ptr;
  774. int flags;
  775. if (rm->audio_pkt_cnt) {
  776. // If there are queued audio packet return them first
  777. st = s->streams[rm->audio_stream_num];
  778. av_new_packet(pkt, st->codec->block_align);
  779. memcpy(pkt->data, rm->audiobuf + st->codec->block_align *
  780. (rm->sub_packet_h * rm->audio_framesize / st->codec->block_align - rm->audio_pkt_cnt),
  781. st->codec->block_align);
  782. rm->audio_pkt_cnt--;
  783. pkt->flags = 0;
  784. pkt->stream_index = rm->audio_stream_num;
  785. } else if (rm->old_format) {
  786. st = s->streams[0];
  787. if (st->codec->codec_id == CODEC_ID_RA_288) {
  788. int x, y;
  789. for (y = 0; y < rm->sub_packet_h; y++)
  790. for (x = 0; x < rm->sub_packet_h/2; x++)
  791. if (get_buffer(pb, rm->audiobuf+x*2*rm->audio_framesize+y*rm->coded_framesize, rm->coded_framesize) <= 0)
  792. return AVERROR_IO;
  793. rm->audio_stream_num = 0;
  794. rm->audio_pkt_cnt = rm->sub_packet_h * rm->audio_framesize / st->codec->block_align - 1;
  795. // Release first audio packet
  796. av_new_packet(pkt, st->codec->block_align);
  797. memcpy(pkt->data, rm->audiobuf, st->codec->block_align);
  798. pkt->flags |= PKT_FLAG_KEY; // Mark first packet as keyframe
  799. pkt->stream_index = 0;
  800. } else {
  801. /* just read raw bytes */
  802. len = RAW_PACKET_SIZE;
  803. len= av_get_packet(pb, pkt, len);
  804. pkt->stream_index = 0;
  805. if (len <= 0) {
  806. return AVERROR_IO;
  807. }
  808. pkt->size = len;
  809. }
  810. } else {
  811. int seq=1;
  812. resync:
  813. len=sync(s, &timestamp, &flags, &i, &pos);
  814. if(len<0)
  815. return AVERROR_IO;
  816. st = s->streams[i];
  817. if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
  818. int h, pic_num, len2, pos;
  819. h= get_byte(pb); len--;
  820. if(!(h & 0x40)){
  821. seq = get_byte(pb); len--;
  822. }
  823. if((h & 0xc0) == 0x40){
  824. len2= pos= 0;
  825. }else{
  826. len2 = get_num(pb, &len);
  827. pos = get_num(pb, &len);
  828. }
  829. /* picture number */
  830. pic_num= get_byte(pb); len--;
  831. rm->remaining_len= len;
  832. rm->current_stream= st->id;
  833. // av_log(NULL, AV_LOG_DEBUG, "%X len:%d pos:%d len2:%d pic_num:%d\n",h, len, pos, len2, pic_num);
  834. if(len2 && len2<len)
  835. len=len2;
  836. rm->remaining_len-= len;
  837. av_get_packet(pb, pkt, len);
  838. }
  839. if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
  840. if ((st->codec->codec_id == CODEC_ID_RA_288) ||
  841. (st->codec->codec_id == CODEC_ID_COOK)) {
  842. int x;
  843. int sps = rm->sub_packet_size;
  844. int cfs = rm->coded_framesize;
  845. int h = rm->sub_packet_h;
  846. int y = rm->sub_packet_cnt;
  847. int w = rm->audio_framesize;
  848. if (flags & 2)
  849. y = rm->sub_packet_cnt = 0;
  850. if (!y)
  851. rm->audiotimestamp = timestamp;
  852. switch(st->codec->codec_id) {
  853. case CODEC_ID_RA_288:
  854. for (x = 0; x < h/2; x++)
  855. get_buffer(pb, rm->audiobuf+x*2*w+y*cfs, cfs);
  856. break;
  857. case CODEC_ID_COOK:
  858. for (x = 0; x < w/sps; x++)
  859. get_buffer(pb, rm->audiobuf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), sps);
  860. break;
  861. }
  862. if (++(rm->sub_packet_cnt) < h)
  863. goto resync;
  864. else {
  865. rm->sub_packet_cnt = 0;
  866. rm->audio_stream_num = i;
  867. rm->audio_pkt_cnt = h * w / st->codec->block_align - 1;
  868. // Release first audio packet
  869. av_new_packet(pkt, st->codec->block_align);
  870. memcpy(pkt->data, rm->audiobuf, st->codec->block_align);
  871. timestamp = rm->audiotimestamp;
  872. flags = 2; // Mark first packet as keyframe
  873. }
  874. } else
  875. av_get_packet(pb, pkt, len);
  876. }
  877. if( (st->discard >= AVDISCARD_NONKEY && !(flags&2))
  878. || st->discard >= AVDISCARD_ALL){
  879. av_free_packet(pkt);
  880. goto resync;
  881. }
  882. pkt->stream_index = i;
  883. #if 0
  884. if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
  885. if(st->codec->codec_id == CODEC_ID_RV20){
  886. int seq= 128*(pkt->data[2]&0x7F) + (pkt->data[3]>>1);
  887. av_log(NULL, AV_LOG_DEBUG, "%d %Ld %d\n", timestamp, timestamp*512LL/25, seq);
  888. seq |= (timestamp&~0x3FFF);
  889. if(seq - timestamp > 0x2000) seq -= 0x4000;
  890. if(seq - timestamp < -0x2000) seq += 0x4000;
  891. }
  892. }
  893. #endif
  894. pkt->pts= timestamp;
  895. if(flags&2){
  896. pkt->flags |= PKT_FLAG_KEY;
  897. if((seq&0x7F) == 1)
  898. av_add_index_entry(st, pos, timestamp, 0, 0, AVINDEX_KEYFRAME);
  899. }
  900. }
  901. /* for AC3, needs to swap bytes */
  902. if (st->codec->codec_id == CODEC_ID_AC3) {
  903. ptr = pkt->data;
  904. for(j=0;j<len;j+=2) {
  905. tmp = ptr[0];
  906. ptr[0] = ptr[1];
  907. ptr[1] = tmp;
  908. ptr += 2;
  909. }
  910. }
  911. return 0;
  912. }
  913. static int rm_read_close(AVFormatContext *s)
  914. {
  915. RMContext *rm = s->priv_data;
  916. av_free(rm->audiobuf);
  917. return 0;
  918. }
  919. static int rm_probe(AVProbeData *p)
  920. {
  921. /* check file header */
  922. if (p->buf_size <= 32)
  923. return 0;
  924. if ((p->buf[0] == '.' && p->buf[1] == 'R' &&
  925. p->buf[2] == 'M' && p->buf[3] == 'F' &&
  926. p->buf[4] == 0 && p->buf[5] == 0) ||
  927. (p->buf[0] == '.' && p->buf[1] == 'r' &&
  928. p->buf[2] == 'a' && p->buf[3] == 0xfd))
  929. return AVPROBE_SCORE_MAX;
  930. else
  931. return 0;
  932. }
  933. static int64_t rm_read_dts(AVFormatContext *s, int stream_index,
  934. int64_t *ppos, int64_t pos_limit)
  935. {
  936. RMContext *rm = s->priv_data;
  937. int64_t pos, dts;
  938. int stream_index2, flags, len, h;
  939. pos = *ppos;
  940. if(rm->old_format)
  941. return AV_NOPTS_VALUE;
  942. url_fseek(&s->pb, pos, SEEK_SET);
  943. rm->remaining_len=0;
  944. for(;;){
  945. int seq=1;
  946. AVStream *st;
  947. len=sync(s, &dts, &flags, &stream_index2, &pos);
  948. if(len<0)
  949. return AV_NOPTS_VALUE;
  950. st = s->streams[stream_index2];
  951. if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
  952. h= get_byte(&s->pb); len--;
  953. if(!(h & 0x40)){
  954. seq = get_byte(&s->pb); len--;
  955. }
  956. }
  957. if((flags&2) && (seq&0x7F) == 1){
  958. // av_log(s, AV_LOG_DEBUG, "%d %d-%d %Ld %d\n", flags, stream_index2, stream_index, dts, seq);
  959. av_add_index_entry(st, pos, dts, 0, 0, AVINDEX_KEYFRAME);
  960. if(stream_index2 == stream_index)
  961. break;
  962. }
  963. url_fskip(&s->pb, len);
  964. }
  965. *ppos = pos;
  966. return dts;
  967. }
  968. static AVInputFormat rm_iformat = {
  969. "rm",
  970. "rm format",
  971. sizeof(RMContext),
  972. rm_probe,
  973. rm_read_header,
  974. rm_read_packet,
  975. rm_read_close,
  976. NULL,
  977. rm_read_dts,
  978. };
  979. #ifdef CONFIG_MUXERS
  980. static AVOutputFormat rm_oformat = {
  981. "rm",
  982. "rm format",
  983. "application/vnd.rn-realmedia",
  984. "rm,ra",
  985. sizeof(RMContext),
  986. CODEC_ID_AC3,
  987. CODEC_ID_RV10,
  988. rm_write_header,
  989. rm_write_packet,
  990. rm_write_trailer,
  991. };
  992. #endif //CONFIG_MUXERS
  993. int rm_init(void)
  994. {
  995. av_register_input_format(&rm_iformat);
  996. #ifdef CONFIG_MUXERS
  997. av_register_output_format(&rm_oformat);
  998. #endif //CONFIG_MUXERS
  999. return 0;
  1000. }