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.

1080 lines
34KB

  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. /// 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[128];
  443. uint32_t version;
  444. int i;
  445. /* ra type header */
  446. version = get_be32(pb); /* version */
  447. if (((version >> 16) & 0xff) == 3) {
  448. /* very old version */
  449. for(i = 0; i < 14; i++)
  450. get_byte(pb);
  451. get_str8(pb, s->title, sizeof(s->title));
  452. get_str8(pb, s->author, sizeof(s->author));
  453. get_str8(pb, s->copyright, sizeof(s->copyright));
  454. get_str8(pb, s->comment, sizeof(s->comment));
  455. get_byte(pb);
  456. get_str8(pb, buf, sizeof(buf));
  457. st->codec->sample_rate = 8000;
  458. st->codec->channels = 1;
  459. st->codec->codec_type = CODEC_TYPE_AUDIO;
  460. st->codec->codec_id = CODEC_ID_RA_144;
  461. } else {
  462. int flavor, sub_packet_h, coded_framesize, sub_packet_size;
  463. /* old version (4) */
  464. get_be32(pb); /* .ra4 */
  465. get_be32(pb); /* data size */
  466. get_be16(pb); /* version2 */
  467. get_be32(pb); /* header size */
  468. flavor= get_be16(pb); /* add codec info / flavor */
  469. rm->coded_framesize = coded_framesize = get_be32(pb); /* coded frame size */
  470. get_be32(pb); /* ??? */
  471. get_be32(pb); /* ??? */
  472. get_be32(pb); /* ??? */
  473. rm->sub_packet_h = sub_packet_h = get_be16(pb); /* 1 */
  474. st->codec->block_align= get_be16(pb); /* frame size */
  475. rm->sub_packet_size = sub_packet_size = get_be16(pb); /* sub packet size */
  476. get_be16(pb); /* ??? */
  477. if (((version >> 16) & 0xff) == 5) {
  478. get_be16(pb); get_be16(pb); get_be16(pb); }
  479. st->codec->sample_rate = get_be16(pb);
  480. get_be32(pb);
  481. st->codec->channels = get_be16(pb);
  482. if (((version >> 16) & 0xff) == 5) {
  483. get_be32(pb);
  484. buf[0] = get_byte(pb);
  485. buf[1] = get_byte(pb);
  486. buf[2] = get_byte(pb);
  487. buf[3] = get_byte(pb);
  488. buf[4] = 0;
  489. } else {
  490. get_str8(pb, buf, sizeof(buf)); /* desc */
  491. get_str8(pb, buf, sizeof(buf)); /* desc */
  492. }
  493. st->codec->codec_type = CODEC_TYPE_AUDIO;
  494. if (!strcmp(buf, "dnet")) {
  495. st->codec->codec_id = CODEC_ID_AC3;
  496. } else if (!strcmp(buf, "28_8")) {
  497. st->codec->codec_id = CODEC_ID_RA_288;
  498. st->codec->extradata_size= 0;
  499. rm->audio_framesize = st->codec->block_align;
  500. st->codec->block_align = coded_framesize;
  501. rm->audiobuf = av_malloc(rm->audio_framesize * sub_packet_h);
  502. } else if (!strcmp(buf, "cook")) {
  503. int codecdata_length, i;
  504. get_be16(pb); get_byte(pb);
  505. if (((version >> 16) & 0xff) == 5)
  506. get_byte(pb);
  507. codecdata_length = get_be32(pb);
  508. st->codec->codec_id = CODEC_ID_COOK;
  509. st->codec->extradata_size= codecdata_length;
  510. st->codec->extradata= av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  511. for(i = 0; i < codecdata_length; i++)
  512. ((uint8_t*)st->codec->extradata)[i] = get_byte(pb);
  513. rm->audio_framesize = st->codec->block_align;
  514. st->codec->block_align = rm->sub_packet_size;
  515. rm->audiobuf = av_malloc(rm->audio_framesize * sub_packet_h);
  516. } else {
  517. st->codec->codec_id = CODEC_ID_NONE;
  518. pstrcpy(st->codec->codec_name, sizeof(st->codec->codec_name),
  519. buf);
  520. }
  521. if (read_all) {
  522. get_byte(pb);
  523. get_byte(pb);
  524. get_byte(pb);
  525. get_str8(pb, s->title, sizeof(s->title));
  526. get_str8(pb, s->author, sizeof(s->author));
  527. get_str8(pb, s->copyright, sizeof(s->copyright));
  528. get_str8(pb, s->comment, sizeof(s->comment));
  529. }
  530. }
  531. }
  532. static int rm_read_header_old(AVFormatContext *s, AVFormatParameters *ap)
  533. {
  534. RMContext *rm = s->priv_data;
  535. AVStream *st;
  536. rm->old_format = 1;
  537. st = av_new_stream(s, 0);
  538. if (!st)
  539. goto fail;
  540. rm_read_audio_stream_info(s, st, 1);
  541. return 0;
  542. fail:
  543. return -1;
  544. }
  545. static int rm_read_header(AVFormatContext *s, AVFormatParameters *ap)
  546. {
  547. RMContext *rm = s->priv_data;
  548. AVStream *st;
  549. ByteIOContext *pb = &s->pb;
  550. unsigned int tag, v;
  551. int tag_size, size, codec_data_size, i;
  552. int64_t codec_pos;
  553. unsigned int h263_hack_version, start_time, duration;
  554. char buf[128];
  555. int flags = 0;
  556. tag = get_le32(pb);
  557. if (tag == MKTAG('.', 'r', 'a', 0xfd)) {
  558. /* very old .ra format */
  559. return rm_read_header_old(s, ap);
  560. } else if (tag != MKTAG('.', 'R', 'M', 'F')) {
  561. return AVERROR_IO;
  562. }
  563. get_be32(pb); /* header size */
  564. get_be16(pb);
  565. get_be32(pb);
  566. get_be32(pb); /* number of headers */
  567. for(;;) {
  568. if (url_feof(pb))
  569. goto fail;
  570. tag = get_le32(pb);
  571. tag_size = get_be32(pb);
  572. get_be16(pb);
  573. #if 0
  574. printf("tag=%c%c%c%c (%08x) size=%d\n",
  575. (tag) & 0xff,
  576. (tag >> 8) & 0xff,
  577. (tag >> 16) & 0xff,
  578. (tag >> 24) & 0xff,
  579. tag,
  580. tag_size);
  581. #endif
  582. if (tag_size < 10 && tag != MKTAG('D', 'A', 'T', 'A'))
  583. goto fail;
  584. switch(tag) {
  585. case MKTAG('P', 'R', 'O', 'P'):
  586. /* file header */
  587. get_be32(pb); /* max bit rate */
  588. get_be32(pb); /* avg bit rate */
  589. get_be32(pb); /* max packet size */
  590. get_be32(pb); /* avg packet size */
  591. get_be32(pb); /* nb packets */
  592. get_be32(pb); /* duration */
  593. get_be32(pb); /* preroll */
  594. get_be32(pb); /* index offset */
  595. get_be32(pb); /* data offset */
  596. get_be16(pb); /* nb streams */
  597. flags = get_be16(pb); /* flags */
  598. break;
  599. case MKTAG('C', 'O', 'N', 'T'):
  600. get_str(pb, s->title, sizeof(s->title));
  601. get_str(pb, s->author, sizeof(s->author));
  602. get_str(pb, s->copyright, sizeof(s->copyright));
  603. get_str(pb, s->comment, sizeof(s->comment));
  604. break;
  605. case MKTAG('M', 'D', 'P', 'R'):
  606. st = av_new_stream(s, 0);
  607. if (!st)
  608. goto fail;
  609. st->id = get_be16(pb);
  610. get_be32(pb); /* max bit rate */
  611. st->codec->bit_rate = get_be32(pb); /* bit rate */
  612. get_be32(pb); /* max packet size */
  613. get_be32(pb); /* avg packet size */
  614. start_time = get_be32(pb); /* start time */
  615. get_be32(pb); /* preroll */
  616. duration = get_be32(pb); /* duration */
  617. st->start_time = start_time;
  618. st->duration = duration;
  619. get_str8(pb, buf, sizeof(buf)); /* desc */
  620. get_str8(pb, buf, sizeof(buf)); /* mimetype */
  621. codec_data_size = get_be32(pb);
  622. codec_pos = url_ftell(pb);
  623. st->codec->codec_type = CODEC_TYPE_DATA;
  624. av_set_pts_info(st, 64, 1, 1000);
  625. v = get_be32(pb);
  626. if (v == MKTAG(0xfd, 'a', 'r', '.')) {
  627. /* ra type header */
  628. rm_read_audio_stream_info(s, st, 0);
  629. } else {
  630. int fps, fps2;
  631. if (get_le32(pb) != MKTAG('V', 'I', 'D', 'O')) {
  632. fail1:
  633. av_log(st->codec, AV_LOG_ERROR, "Unsupported video codec\n");
  634. goto skip;
  635. }
  636. st->codec->codec_tag = get_le32(pb);
  637. // av_log(NULL, AV_LOG_DEBUG, "%X %X\n", st->codec->codec_tag, MKTAG('R', 'V', '2', '0'));
  638. if ( st->codec->codec_tag != MKTAG('R', 'V', '1', '0')
  639. && st->codec->codec_tag != MKTAG('R', 'V', '2', '0')
  640. && st->codec->codec_tag != MKTAG('R', 'V', '3', '0')
  641. && st->codec->codec_tag != MKTAG('R', 'V', '4', '0'))
  642. goto fail1;
  643. st->codec->width = get_be16(pb);
  644. st->codec->height = get_be16(pb);
  645. st->codec->time_base.num= 1;
  646. fps= get_be16(pb);
  647. st->codec->codec_type = CODEC_TYPE_VIDEO;
  648. get_be32(pb);
  649. fps2= get_be16(pb);
  650. get_be16(pb);
  651. st->codec->extradata_size= codec_data_size - (url_ftell(pb) - codec_pos);
  652. st->codec->extradata= av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  653. get_buffer(pb, st->codec->extradata, st->codec->extradata_size);
  654. // av_log(NULL, AV_LOG_DEBUG, "fps= %d fps2= %d\n", fps, fps2);
  655. st->codec->time_base.den = fps * st->codec->time_base.num;
  656. /* modification of h263 codec version (!) */
  657. #ifdef WORDS_BIGENDIAN
  658. h263_hack_version = ((uint32_t*)st->codec->extradata)[1];
  659. #else
  660. h263_hack_version = bswap_32(((uint32_t*)st->codec->extradata)[1]);
  661. #endif
  662. st->codec->sub_id = h263_hack_version;
  663. switch((h263_hack_version>>28)){
  664. case 1: st->codec->codec_id = CODEC_ID_RV10; break;
  665. case 2: st->codec->codec_id = CODEC_ID_RV20; break;
  666. case 3: st->codec->codec_id = CODEC_ID_RV30; break;
  667. case 4: st->codec->codec_id = CODEC_ID_RV40; break;
  668. default: goto fail1;
  669. }
  670. }
  671. skip:
  672. /* skip codec info */
  673. size = url_ftell(pb) - codec_pos;
  674. url_fskip(pb, codec_data_size - size);
  675. break;
  676. case MKTAG('D', 'A', 'T', 'A'):
  677. goto header_end;
  678. default:
  679. /* unknown tag: skip it */
  680. url_fskip(pb, tag_size - 10);
  681. break;
  682. }
  683. }
  684. header_end:
  685. rm->nb_packets = get_be32(pb); /* number of packets */
  686. if (!rm->nb_packets && (flags & 4))
  687. rm->nb_packets = 3600 * 25;
  688. get_be32(pb); /* next data header */
  689. return 0;
  690. fail:
  691. for(i=0;i<s->nb_streams;i++) {
  692. av_free(s->streams[i]);
  693. }
  694. return AVERROR_IO;
  695. }
  696. static int get_num(ByteIOContext *pb, int *len)
  697. {
  698. int n, n1;
  699. n = get_be16(pb);
  700. (*len)-=2;
  701. if (n >= 0x4000) {
  702. return n - 0x4000;
  703. } else {
  704. n1 = get_be16(pb);
  705. (*len)-=2;
  706. return (n << 16) | n1;
  707. }
  708. }
  709. /* multiple of 20 bytes for ra144 (ugly) */
  710. #define RAW_PACKET_SIZE 1000
  711. static int sync(AVFormatContext *s, int64_t *timestamp, int *flags, int *stream_index, int64_t *pos){
  712. RMContext *rm = s->priv_data;
  713. ByteIOContext *pb = &s->pb;
  714. int len, num, res, i;
  715. AVStream *st;
  716. uint32_t state=0xFFFFFFFF;
  717. while(!url_feof(pb)){
  718. *pos= url_ftell(pb);
  719. if(rm->remaining_len > 0){
  720. num= rm->current_stream;
  721. len= rm->remaining_len;
  722. *timestamp = AV_NOPTS_VALUE;
  723. *flags= 0;
  724. }else{
  725. state= (state<<8) + get_byte(pb);
  726. if(state == MKBETAG('I', 'N', 'D', 'X')){
  727. len = get_be16(pb) - 6;
  728. if(len<0)
  729. continue;
  730. goto skip;
  731. }
  732. if(state > (unsigned)0xFFFF || state < 12)
  733. continue;
  734. len=state;
  735. state= 0xFFFFFFFF;
  736. num = get_be16(pb);
  737. *timestamp = get_be32(pb);
  738. res= get_byte(pb); /* reserved */
  739. *flags = get_byte(pb); /* flags */
  740. len -= 12;
  741. }
  742. for(i=0;i<s->nb_streams;i++) {
  743. st = s->streams[i];
  744. if (num == st->id)
  745. break;
  746. }
  747. if (i == s->nb_streams) {
  748. skip:
  749. /* skip packet if unknown number */
  750. url_fskip(pb, len);
  751. rm->remaining_len -= len;
  752. continue;
  753. }
  754. *stream_index= i;
  755. return len;
  756. }
  757. return -1;
  758. }
  759. static int rm_read_packet(AVFormatContext *s, AVPacket *pkt)
  760. {
  761. RMContext *rm = s->priv_data;
  762. ByteIOContext *pb = &s->pb;
  763. AVStream *st;
  764. int i, len, tmp, j;
  765. int64_t timestamp, pos;
  766. uint8_t *ptr;
  767. int flags;
  768. if (rm->old_format) {
  769. /* just read raw bytes */
  770. len = RAW_PACKET_SIZE;
  771. len= av_get_packet(pb, pkt, len);
  772. pkt->stream_index = 0;
  773. if (len <= 0) {
  774. return AVERROR_IO;
  775. }
  776. pkt->size = len;
  777. st = s->streams[0];
  778. } else if (rm->audio_pkt_cnt) {
  779. // If there are queued audio packet return them first
  780. st = s->streams[rm->audio_stream_num];
  781. av_new_packet(pkt, st->codec->block_align);
  782. memcpy(pkt->data, rm->audiobuf + st->codec->block_align *
  783. (rm->sub_packet_h * rm->audio_framesize / st->codec->block_align - rm->audio_pkt_cnt),
  784. st->codec->block_align);
  785. rm->audio_pkt_cnt--;
  786. pkt->flags = 0;
  787. pkt->stream_index = rm->audio_stream_num;
  788. } else {
  789. int seq=1;
  790. resync:
  791. len=sync(s, &timestamp, &flags, &i, &pos);
  792. if(len<0)
  793. return AVERROR_IO;
  794. st = s->streams[i];
  795. if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
  796. int h, pic_num, len2, pos;
  797. h= get_byte(pb); len--;
  798. if(!(h & 0x40)){
  799. seq = get_byte(pb); len--;
  800. }
  801. if((h & 0xc0) == 0x40){
  802. len2= pos= 0;
  803. }else{
  804. len2 = get_num(pb, &len);
  805. pos = get_num(pb, &len);
  806. }
  807. /* picture number */
  808. pic_num= get_byte(pb); len--;
  809. rm->remaining_len= len;
  810. rm->current_stream= st->id;
  811. // av_log(NULL, AV_LOG_DEBUG, "%X len:%d pos:%d len2:%d pic_num:%d\n",h, len, pos, len2, pic_num);
  812. if(len2 && len2<len)
  813. len=len2;
  814. rm->remaining_len-= len;
  815. av_get_packet(pb, pkt, len);
  816. }
  817. if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
  818. if ((st->codec->codec_id == CODEC_ID_RA_288) ||
  819. (st->codec->codec_id == CODEC_ID_COOK)) {
  820. int x;
  821. int sps = rm->sub_packet_size;
  822. int cfs = rm->coded_framesize;
  823. int h = rm->sub_packet_h;
  824. int y = rm->sub_packet_cnt;
  825. int w = rm->audio_framesize;
  826. if (flags & 2)
  827. y = rm->sub_packet_cnt = 0;
  828. if (!y)
  829. rm->audiotimestamp = timestamp;
  830. switch(st->codec->codec_id) {
  831. case CODEC_ID_RA_288:
  832. for (x = 0; x < h/2; x++)
  833. get_buffer(pb, rm->audiobuf+x*2*w+y*cfs, cfs);
  834. break;
  835. case CODEC_ID_COOK:
  836. for (x = 0; x < w/sps; x++)
  837. get_buffer(pb, rm->audiobuf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), sps);
  838. break;
  839. }
  840. if (++(rm->sub_packet_cnt) < h)
  841. goto resync;
  842. else {
  843. rm->sub_packet_cnt = 0;
  844. rm->audio_stream_num = i;
  845. rm->audio_pkt_cnt = h * w / st->codec->block_align - 1;
  846. // Release first audio packet
  847. av_new_packet(pkt, st->codec->block_align);
  848. memcpy(pkt->data, rm->audiobuf, st->codec->block_align);
  849. timestamp = rm->audiotimestamp;
  850. flags = 2; // Mark first packet as keyframe
  851. }
  852. } else
  853. av_get_packet(pb, pkt, len);
  854. }
  855. if( (st->discard >= AVDISCARD_NONKEY && !(flags&2))
  856. || st->discard >= AVDISCARD_ALL){
  857. av_free_packet(pkt);
  858. goto resync;
  859. }
  860. pkt->stream_index = i;
  861. #if 0
  862. if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
  863. if(st->codec->codec_id == CODEC_ID_RV20){
  864. int seq= 128*(pkt->data[2]&0x7F) + (pkt->data[3]>>1);
  865. av_log(NULL, AV_LOG_DEBUG, "%d %Ld %d\n", timestamp, timestamp*512LL/25, seq);
  866. seq |= (timestamp&~0x3FFF);
  867. if(seq - timestamp > 0x2000) seq -= 0x4000;
  868. if(seq - timestamp < -0x2000) seq += 0x4000;
  869. }
  870. }
  871. #endif
  872. pkt->pts= timestamp;
  873. if(flags&2){
  874. pkt->flags |= PKT_FLAG_KEY;
  875. if((seq&0x7F) == 1)
  876. av_add_index_entry(st, pos, timestamp, 0, AVINDEX_KEYFRAME);
  877. }
  878. }
  879. /* for AC3, needs to swap bytes */
  880. if (st->codec->codec_id == CODEC_ID_AC3) {
  881. ptr = pkt->data;
  882. for(j=0;j<len;j+=2) {
  883. tmp = ptr[0];
  884. ptr[0] = ptr[1];
  885. ptr[1] = tmp;
  886. ptr += 2;
  887. }
  888. }
  889. return 0;
  890. }
  891. static int rm_read_close(AVFormatContext *s)
  892. {
  893. RMContext *rm = s->priv_data;
  894. av_free(rm->audiobuf);
  895. return 0;
  896. }
  897. static int rm_probe(AVProbeData *p)
  898. {
  899. /* check file header */
  900. if (p->buf_size <= 32)
  901. return 0;
  902. if ((p->buf[0] == '.' && p->buf[1] == 'R' &&
  903. p->buf[2] == 'M' && p->buf[3] == 'F' &&
  904. p->buf[4] == 0 && p->buf[5] == 0) ||
  905. (p->buf[0] == '.' && p->buf[1] == 'r' &&
  906. p->buf[2] == 'a' && p->buf[3] == 0xfd))
  907. return AVPROBE_SCORE_MAX;
  908. else
  909. return 0;
  910. }
  911. static int64_t rm_read_dts(AVFormatContext *s, int stream_index,
  912. int64_t *ppos, int64_t pos_limit)
  913. {
  914. RMContext *rm = s->priv_data;
  915. int64_t pos, dts;
  916. int stream_index2, flags, len, h;
  917. pos = *ppos;
  918. if(rm->old_format)
  919. return AV_NOPTS_VALUE;
  920. url_fseek(&s->pb, pos, SEEK_SET);
  921. rm->remaining_len=0;
  922. for(;;){
  923. int seq=1;
  924. AVStream *st;
  925. len=sync(s, &dts, &flags, &stream_index2, &pos);
  926. if(len<0)
  927. return AV_NOPTS_VALUE;
  928. st = s->streams[stream_index2];
  929. if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
  930. h= get_byte(&s->pb); len--;
  931. if(!(h & 0x40)){
  932. seq = get_byte(&s->pb); len--;
  933. }
  934. }
  935. if((flags&2) && (seq&0x7F) == 1){
  936. // av_log(s, AV_LOG_DEBUG, "%d %d-%d %Ld %d\n", flags, stream_index2, stream_index, dts, seq);
  937. av_add_index_entry(st, pos, dts, 0, AVINDEX_KEYFRAME);
  938. if(stream_index2 == stream_index)
  939. break;
  940. }
  941. url_fskip(&s->pb, len);
  942. }
  943. *ppos = pos;
  944. return dts;
  945. }
  946. static AVInputFormat rm_iformat = {
  947. "rm",
  948. "rm format",
  949. sizeof(RMContext),
  950. rm_probe,
  951. rm_read_header,
  952. rm_read_packet,
  953. rm_read_close,
  954. NULL,
  955. rm_read_dts,
  956. };
  957. #ifdef CONFIG_MUXERS
  958. static AVOutputFormat rm_oformat = {
  959. "rm",
  960. "rm format",
  961. "application/vnd.rn-realmedia",
  962. "rm,ra",
  963. sizeof(RMContext),
  964. CODEC_ID_AC3,
  965. CODEC_ID_RV10,
  966. rm_write_header,
  967. rm_write_packet,
  968. rm_write_trailer,
  969. };
  970. #endif //CONFIG_MUXERS
  971. int rm_init(void)
  972. {
  973. av_register_input_format(&rm_iformat);
  974. #ifdef CONFIG_MUXERS
  975. av_register_output_format(&rm_oformat);
  976. #endif //CONFIG_MUXERS
  977. return 0;
  978. }