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.

995 lines
31KB

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