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.

1145 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. switch(((uint8_t*)st->codec->extradata)[4]>>4){
  699. case 1: st->codec->codec_id = CODEC_ID_RV10; break;
  700. case 2: st->codec->codec_id = CODEC_ID_RV20; break;
  701. case 3: st->codec->codec_id = CODEC_ID_RV30; break;
  702. case 4: st->codec->codec_id = CODEC_ID_RV40; break;
  703. default: goto fail1;
  704. }
  705. }
  706. skip:
  707. /* skip codec info */
  708. size = url_ftell(pb) - codec_pos;
  709. url_fskip(pb, codec_data_size - size);
  710. break;
  711. case MKTAG('D', 'A', 'T', 'A'):
  712. goto header_end;
  713. default:
  714. /* unknown tag: skip it */
  715. url_fskip(pb, tag_size - 10);
  716. break;
  717. }
  718. }
  719. header_end:
  720. rm->nb_packets = get_be32(pb); /* number of packets */
  721. if (!rm->nb_packets && (flags & 4))
  722. rm->nb_packets = 3600 * 25;
  723. get_be32(pb); /* next data header */
  724. return 0;
  725. fail:
  726. for(i=0;i<s->nb_streams;i++) {
  727. av_free(s->streams[i]);
  728. }
  729. return AVERROR_IO;
  730. }
  731. static int get_num(ByteIOContext *pb, int *len)
  732. {
  733. int n, n1;
  734. n = get_be16(pb);
  735. (*len)-=2;
  736. if (n >= 0x4000) {
  737. return n - 0x4000;
  738. } else {
  739. n1 = get_be16(pb);
  740. (*len)-=2;
  741. return (n << 16) | n1;
  742. }
  743. }
  744. /* multiple of 20 bytes for ra144 (ugly) */
  745. #define RAW_PACKET_SIZE 1000
  746. static int sync(AVFormatContext *s, int64_t *timestamp, int *flags, int *stream_index, int64_t *pos){
  747. RMContext *rm = s->priv_data;
  748. ByteIOContext *pb = &s->pb;
  749. int len, num, res, i;
  750. AVStream *st;
  751. uint32_t state=0xFFFFFFFF;
  752. while(!url_feof(pb)){
  753. *pos= url_ftell(pb);
  754. if(rm->remaining_len > 0){
  755. num= rm->current_stream;
  756. len= rm->remaining_len;
  757. *timestamp = AV_NOPTS_VALUE;
  758. *flags= 0;
  759. }else{
  760. state= (state<<8) + get_byte(pb);
  761. if(state == MKBETAG('I', 'N', 'D', 'X')){
  762. len = get_be16(pb) - 6;
  763. if(len<0)
  764. continue;
  765. goto skip;
  766. }
  767. if(state > (unsigned)0xFFFF || state < 12)
  768. continue;
  769. len=state;
  770. state= 0xFFFFFFFF;
  771. num = get_be16(pb);
  772. *timestamp = get_be32(pb);
  773. res= get_byte(pb); /* reserved */
  774. *flags = get_byte(pb); /* flags */
  775. len -= 12;
  776. }
  777. for(i=0;i<s->nb_streams;i++) {
  778. st = s->streams[i];
  779. if (num == st->id)
  780. break;
  781. }
  782. if (i == s->nb_streams) {
  783. skip:
  784. /* skip packet if unknown number */
  785. url_fskip(pb, len);
  786. rm->remaining_len -= len;
  787. continue;
  788. }
  789. *stream_index= i;
  790. return len;
  791. }
  792. return -1;
  793. }
  794. static int rm_read_packet(AVFormatContext *s, AVPacket *pkt)
  795. {
  796. RMContext *rm = s->priv_data;
  797. ByteIOContext *pb = &s->pb;
  798. AVStream *st;
  799. int i, len, tmp, j;
  800. int64_t timestamp, pos;
  801. uint8_t *ptr;
  802. int flags;
  803. if (rm->audio_pkt_cnt) {
  804. // If there are queued audio packet return them first
  805. st = s->streams[rm->audio_stream_num];
  806. if (st->codec->codec_id == CODEC_ID_AAC)
  807. av_get_packet(pb, pkt, rm->sub_packet_lengths[rm->sub_packet_cnt - rm->audio_pkt_cnt]);
  808. else {
  809. av_new_packet(pkt, st->codec->block_align);
  810. memcpy(pkt->data, rm->audiobuf + st->codec->block_align *
  811. (rm->sub_packet_h * rm->audio_framesize / st->codec->block_align - rm->audio_pkt_cnt),
  812. st->codec->block_align);
  813. }
  814. rm->audio_pkt_cnt--;
  815. pkt->flags = 0;
  816. pkt->stream_index = rm->audio_stream_num;
  817. } else if (rm->old_format) {
  818. st = s->streams[0];
  819. if (st->codec->codec_id == CODEC_ID_RA_288) {
  820. int x, y;
  821. for (y = 0; y < rm->sub_packet_h; y++)
  822. for (x = 0; x < rm->sub_packet_h/2; x++)
  823. if (get_buffer(pb, rm->audiobuf+x*2*rm->audio_framesize+y*rm->coded_framesize, rm->coded_framesize) <= 0)
  824. return AVERROR_IO;
  825. rm->audio_stream_num = 0;
  826. rm->audio_pkt_cnt = rm->sub_packet_h * rm->audio_framesize / st->codec->block_align - 1;
  827. // Release first audio packet
  828. av_new_packet(pkt, st->codec->block_align);
  829. memcpy(pkt->data, rm->audiobuf, st->codec->block_align);
  830. pkt->flags |= PKT_FLAG_KEY; // Mark first packet as keyframe
  831. pkt->stream_index = 0;
  832. } else {
  833. /* just read raw bytes */
  834. len = RAW_PACKET_SIZE;
  835. len= av_get_packet(pb, pkt, len);
  836. pkt->stream_index = 0;
  837. if (len <= 0) {
  838. return AVERROR_IO;
  839. }
  840. pkt->size = len;
  841. }
  842. } else {
  843. int seq=1;
  844. resync:
  845. len=sync(s, &timestamp, &flags, &i, &pos);
  846. if(len<0)
  847. return AVERROR_IO;
  848. st = s->streams[i];
  849. if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
  850. int h, pic_num, len2, pos;
  851. h= get_byte(pb); len--;
  852. if(!(h & 0x40)){
  853. seq = get_byte(pb); len--;
  854. }
  855. if((h & 0xc0) == 0x40){
  856. len2= pos= 0;
  857. }else{
  858. len2 = get_num(pb, &len);
  859. pos = get_num(pb, &len);
  860. }
  861. /* picture number */
  862. pic_num= get_byte(pb); len--;
  863. rm->remaining_len= len;
  864. rm->current_stream= st->id;
  865. // av_log(NULL, AV_LOG_DEBUG, "%X len:%d pos:%d len2:%d pic_num:%d\n",h, len, pos, len2, pic_num);
  866. if(len2 && len2<len)
  867. len=len2;
  868. rm->remaining_len-= len;
  869. av_get_packet(pb, pkt, len);
  870. }
  871. if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
  872. if ((st->codec->codec_id == CODEC_ID_RA_288) ||
  873. (st->codec->codec_id == CODEC_ID_COOK)) {
  874. int x;
  875. int sps = rm->sub_packet_size;
  876. int cfs = rm->coded_framesize;
  877. int h = rm->sub_packet_h;
  878. int y = rm->sub_packet_cnt;
  879. int w = rm->audio_framesize;
  880. if (flags & 2)
  881. y = rm->sub_packet_cnt = 0;
  882. if (!y)
  883. rm->audiotimestamp = timestamp;
  884. switch(st->codec->codec_id) {
  885. case CODEC_ID_RA_288:
  886. for (x = 0; x < h/2; x++)
  887. get_buffer(pb, rm->audiobuf+x*2*w+y*cfs, cfs);
  888. break;
  889. case CODEC_ID_COOK:
  890. for (x = 0; x < w/sps; x++)
  891. get_buffer(pb, rm->audiobuf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), sps);
  892. break;
  893. }
  894. if (++(rm->sub_packet_cnt) < h)
  895. goto resync;
  896. else {
  897. rm->sub_packet_cnt = 0;
  898. rm->audio_stream_num = i;
  899. rm->audio_pkt_cnt = h * w / st->codec->block_align - 1;
  900. // Release first audio packet
  901. av_new_packet(pkt, st->codec->block_align);
  902. memcpy(pkt->data, rm->audiobuf, st->codec->block_align);
  903. timestamp = rm->audiotimestamp;
  904. flags = 2; // Mark first packet as keyframe
  905. }
  906. } else if (st->codec->codec_id == CODEC_ID_AAC) {
  907. int x;
  908. rm->audio_stream_num = i;
  909. rm->sub_packet_cnt = (get_be16(pb) & 0xf0) >> 4;
  910. if (rm->sub_packet_cnt) {
  911. for (x = 0; x < rm->sub_packet_cnt; x++)
  912. rm->sub_packet_lengths[x] = get_be16(pb);
  913. // Release first audio packet
  914. rm->audio_pkt_cnt = rm->sub_packet_cnt - 1;
  915. av_get_packet(pb, pkt, rm->sub_packet_lengths[0]);
  916. flags = 2; // Mark first packet as keyframe
  917. }
  918. } else
  919. av_get_packet(pb, pkt, len);
  920. }
  921. if( (st->discard >= AVDISCARD_NONKEY && !(flags&2))
  922. || st->discard >= AVDISCARD_ALL){
  923. av_free_packet(pkt);
  924. goto resync;
  925. }
  926. pkt->stream_index = i;
  927. #if 0
  928. if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
  929. if(st->codec->codec_id == CODEC_ID_RV20){
  930. int seq= 128*(pkt->data[2]&0x7F) + (pkt->data[3]>>1);
  931. av_log(NULL, AV_LOG_DEBUG, "%d %Ld %d\n", timestamp, timestamp*512LL/25, seq);
  932. seq |= (timestamp&~0x3FFF);
  933. if(seq - timestamp > 0x2000) seq -= 0x4000;
  934. if(seq - timestamp < -0x2000) seq += 0x4000;
  935. }
  936. }
  937. #endif
  938. pkt->pts= timestamp;
  939. if(flags&2){
  940. pkt->flags |= PKT_FLAG_KEY;
  941. if((seq&0x7F) == 1)
  942. av_add_index_entry(st, pos, timestamp, 0, 0, AVINDEX_KEYFRAME);
  943. }
  944. }
  945. /* for AC3, needs to swap bytes */
  946. if (st->codec->codec_id == CODEC_ID_AC3) {
  947. ptr = pkt->data;
  948. for(j=0;j<len;j+=2) {
  949. tmp = ptr[0];
  950. ptr[0] = ptr[1];
  951. ptr[1] = tmp;
  952. ptr += 2;
  953. }
  954. }
  955. return 0;
  956. }
  957. static int rm_read_close(AVFormatContext *s)
  958. {
  959. RMContext *rm = s->priv_data;
  960. av_free(rm->audiobuf);
  961. return 0;
  962. }
  963. static int rm_probe(AVProbeData *p)
  964. {
  965. /* check file header */
  966. if (p->buf_size <= 32)
  967. return 0;
  968. if ((p->buf[0] == '.' && p->buf[1] == 'R' &&
  969. p->buf[2] == 'M' && p->buf[3] == 'F' &&
  970. p->buf[4] == 0 && p->buf[5] == 0) ||
  971. (p->buf[0] == '.' && p->buf[1] == 'r' &&
  972. p->buf[2] == 'a' && p->buf[3] == 0xfd))
  973. return AVPROBE_SCORE_MAX;
  974. else
  975. return 0;
  976. }
  977. static int64_t rm_read_dts(AVFormatContext *s, int stream_index,
  978. int64_t *ppos, int64_t pos_limit)
  979. {
  980. RMContext *rm = s->priv_data;
  981. int64_t pos, dts;
  982. int stream_index2, flags, len, h;
  983. pos = *ppos;
  984. if(rm->old_format)
  985. return AV_NOPTS_VALUE;
  986. url_fseek(&s->pb, pos, SEEK_SET);
  987. rm->remaining_len=0;
  988. for(;;){
  989. int seq=1;
  990. AVStream *st;
  991. len=sync(s, &dts, &flags, &stream_index2, &pos);
  992. if(len<0)
  993. return AV_NOPTS_VALUE;
  994. st = s->streams[stream_index2];
  995. if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
  996. h= get_byte(&s->pb); len--;
  997. if(!(h & 0x40)){
  998. seq = get_byte(&s->pb); len--;
  999. }
  1000. }
  1001. if((flags&2) && (seq&0x7F) == 1){
  1002. // av_log(s, AV_LOG_DEBUG, "%d %d-%d %Ld %d\n", flags, stream_index2, stream_index, dts, seq);
  1003. av_add_index_entry(st, pos, dts, 0, 0, AVINDEX_KEYFRAME);
  1004. if(stream_index2 == stream_index)
  1005. break;
  1006. }
  1007. url_fskip(&s->pb, len);
  1008. }
  1009. *ppos = pos;
  1010. return dts;
  1011. }
  1012. #ifdef CONFIG_RM_DEMUXER
  1013. AVInputFormat rm_demuxer = {
  1014. "rm",
  1015. "rm format",
  1016. sizeof(RMContext),
  1017. rm_probe,
  1018. rm_read_header,
  1019. rm_read_packet,
  1020. rm_read_close,
  1021. NULL,
  1022. rm_read_dts,
  1023. };
  1024. #endif
  1025. #ifdef CONFIG_RM_MUXER
  1026. AVOutputFormat rm_muxer = {
  1027. "rm",
  1028. "rm format",
  1029. "application/vnd.rn-realmedia",
  1030. "rm,ra",
  1031. sizeof(RMContext),
  1032. CODEC_ID_AC3,
  1033. CODEC_ID_RV10,
  1034. rm_write_header,
  1035. rm_write_packet,
  1036. rm_write_trailer,
  1037. };
  1038. #endif