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.

856 lines
26KB

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