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.

1152 lines
38KB

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