rmenc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /*
  2. * "Real" compatible muxer.
  3. * Copyright (c) 2000, 2001 Fabrice Bellard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avformat.h"
  22. #include "avio_internal.h"
  23. #include "rm.h"
  24. typedef struct {
  25. int nb_packets;
  26. int packet_total_size;
  27. int packet_max_size;
  28. /* codec related output */
  29. int bit_rate;
  30. float frame_rate;
  31. int nb_frames; /* current frame number */
  32. int total_frames; /* total number of frames */
  33. int num;
  34. AVCodecContext *enc;
  35. } StreamInfo;
  36. typedef struct {
  37. StreamInfo streams[2];
  38. StreamInfo *audio_stream, *video_stream;
  39. int data_pos; /* position of the data after the header */
  40. } RMMuxContext;
  41. /* in ms */
  42. #define BUFFER_DURATION 0
  43. static void put_str(AVIOContext *s, const char *tag)
  44. {
  45. avio_wb16(s,strlen(tag));
  46. while (*tag) {
  47. avio_w8(s, *tag++);
  48. }
  49. }
  50. static void put_str8(AVIOContext *s, const char *tag)
  51. {
  52. avio_w8(s, strlen(tag));
  53. while (*tag) {
  54. avio_w8(s, *tag++);
  55. }
  56. }
  57. static int rv10_write_header(AVFormatContext *ctx,
  58. int data_size, int index_pos)
  59. {
  60. RMMuxContext *rm = ctx->priv_data;
  61. AVIOContext *s = ctx->pb;
  62. StreamInfo *stream;
  63. unsigned char *data_offset_ptr, *start_ptr;
  64. const char *desc, *mimetype;
  65. int nb_packets, packet_total_size, packet_max_size, size, packet_avg_size, i;
  66. int bit_rate, v, duration, flags, data_pos;
  67. AVMetadataTag *tag;
  68. start_ptr = s->buf_ptr;
  69. ffio_wfourcc(s, ".RMF");
  70. avio_wb32(s,18); /* header size */
  71. avio_wb16(s,0);
  72. avio_wb32(s,0);
  73. avio_wb32(s,4 + ctx->nb_streams); /* num headers */
  74. ffio_wfourcc(s,"PROP");
  75. avio_wb32(s, 50);
  76. avio_wb16(s, 0);
  77. packet_max_size = 0;
  78. packet_total_size = 0;
  79. nb_packets = 0;
  80. bit_rate = 0;
  81. duration = 0;
  82. for(i=0;i<ctx->nb_streams;i++) {
  83. StreamInfo *stream = &rm->streams[i];
  84. bit_rate += stream->bit_rate;
  85. if (stream->packet_max_size > packet_max_size)
  86. packet_max_size = stream->packet_max_size;
  87. nb_packets += stream->nb_packets;
  88. packet_total_size += stream->packet_total_size;
  89. /* select maximum duration */
  90. v = (int) (1000.0 * (float)stream->total_frames / stream->frame_rate);
  91. if (v > duration)
  92. duration = v;
  93. }
  94. avio_wb32(s, bit_rate); /* max bit rate */
  95. avio_wb32(s, bit_rate); /* avg bit rate */
  96. avio_wb32(s, packet_max_size); /* max packet size */
  97. if (nb_packets > 0)
  98. packet_avg_size = packet_total_size / nb_packets;
  99. else
  100. packet_avg_size = 0;
  101. avio_wb32(s, packet_avg_size); /* avg packet size */
  102. avio_wb32(s, nb_packets); /* num packets */
  103. avio_wb32(s, duration); /* duration */
  104. avio_wb32(s, BUFFER_DURATION); /* preroll */
  105. avio_wb32(s, index_pos); /* index offset */
  106. /* computation of data the data offset */
  107. data_offset_ptr = s->buf_ptr;
  108. avio_wb32(s, 0); /* data offset : will be patched after */
  109. avio_wb16(s, ctx->nb_streams); /* num streams */
  110. flags = 1 | 2; /* save allowed & perfect play */
  111. if (url_is_streamed(s))
  112. flags |= 4; /* live broadcast */
  113. avio_wb16(s, flags);
  114. /* comments */
  115. ffio_wfourcc(s,"CONT");
  116. size = 4 * 2 + 10;
  117. for(i=0; i<FF_ARRAY_ELEMS(ff_rm_metadata); i++) {
  118. tag = av_metadata_get(ctx->metadata, ff_rm_metadata[i], NULL, 0);
  119. if(tag) size += strlen(tag->value);
  120. }
  121. avio_wb32(s,size);
  122. avio_wb16(s,0);
  123. for(i=0; i<FF_ARRAY_ELEMS(ff_rm_metadata); i++) {
  124. tag = av_metadata_get(ctx->metadata, ff_rm_metadata[i], NULL, 0);
  125. put_str(s, tag ? tag->value : "");
  126. }
  127. for(i=0;i<ctx->nb_streams;i++) {
  128. int codec_data_size;
  129. stream = &rm->streams[i];
  130. if (stream->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
  131. desc = "The Audio Stream";
  132. mimetype = "audio/x-pn-realaudio";
  133. codec_data_size = 73;
  134. } else {
  135. desc = "The Video Stream";
  136. mimetype = "video/x-pn-realvideo";
  137. codec_data_size = 34;
  138. }
  139. ffio_wfourcc(s,"MDPR");
  140. size = 10 + 9 * 4 + strlen(desc) + strlen(mimetype) + codec_data_size;
  141. avio_wb32(s, size);
  142. avio_wb16(s, 0);
  143. avio_wb16(s, i); /* stream number */
  144. avio_wb32(s, stream->bit_rate); /* max bit rate */
  145. avio_wb32(s, stream->bit_rate); /* avg bit rate */
  146. avio_wb32(s, stream->packet_max_size); /* max packet size */
  147. if (stream->nb_packets > 0)
  148. packet_avg_size = stream->packet_total_size /
  149. stream->nb_packets;
  150. else
  151. packet_avg_size = 0;
  152. avio_wb32(s, packet_avg_size); /* avg packet size */
  153. avio_wb32(s, 0); /* start time */
  154. avio_wb32(s, BUFFER_DURATION); /* preroll */
  155. /* duration */
  156. if (url_is_streamed(s) || !stream->total_frames)
  157. avio_wb32(s, (int)(3600 * 1000));
  158. else
  159. avio_wb32(s, (int)(stream->total_frames * 1000 / stream->frame_rate));
  160. put_str8(s, desc);
  161. put_str8(s, mimetype);
  162. avio_wb32(s, codec_data_size);
  163. if (stream->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
  164. int coded_frame_size, fscode, sample_rate;
  165. sample_rate = stream->enc->sample_rate;
  166. coded_frame_size = (stream->enc->bit_rate *
  167. stream->enc->frame_size) / (8 * sample_rate);
  168. /* audio codec info */
  169. avio_write(s, ".ra", 3);
  170. avio_w8(s, 0xfd);
  171. avio_wb32(s, 0x00040000); /* version */
  172. ffio_wfourcc(s, ".ra4");
  173. avio_wb32(s, 0x01b53530); /* stream length */
  174. avio_wb16(s, 4); /* unknown */
  175. avio_wb32(s, 0x39); /* header size */
  176. switch(sample_rate) {
  177. case 48000:
  178. case 24000:
  179. case 12000:
  180. fscode = 1;
  181. break;
  182. default:
  183. case 44100:
  184. case 22050:
  185. case 11025:
  186. fscode = 2;
  187. break;
  188. case 32000:
  189. case 16000:
  190. case 8000:
  191. fscode = 3;
  192. }
  193. avio_wb16(s, fscode); /* codec additional info, for AC-3, seems
  194. to be a frequency code */
  195. /* special hack to compensate rounding errors... */
  196. if (coded_frame_size == 557)
  197. coded_frame_size--;
  198. avio_wb32(s, coded_frame_size); /* frame length */
  199. avio_wb32(s, 0x51540); /* unknown */
  200. avio_wb32(s, 0x249f0); /* unknown */
  201. avio_wb32(s, 0x249f0); /* unknown */
  202. avio_wb16(s, 0x01);
  203. /* frame length : seems to be very important */
  204. avio_wb16(s, coded_frame_size);
  205. avio_wb32(s, 0); /* unknown */
  206. avio_wb16(s, stream->enc->sample_rate); /* sample rate */
  207. avio_wb32(s, 0x10); /* unknown */
  208. avio_wb16(s, stream->enc->channels);
  209. put_str8(s, "Int0"); /* codec name */
  210. if (stream->enc->codec_tag) {
  211. avio_w8(s, 4); /* tag length */
  212. avio_wl32(s, stream->enc->codec_tag);
  213. } else {
  214. av_log(ctx, AV_LOG_ERROR, "Invalid codec tag\n");
  215. return -1;
  216. }
  217. avio_wb16(s, 0); /* title length */
  218. avio_wb16(s, 0); /* author length */
  219. avio_wb16(s, 0); /* copyright length */
  220. avio_w8(s, 0); /* end of header */
  221. } else {
  222. /* video codec info */
  223. avio_wb32(s,34); /* size */
  224. ffio_wfourcc(s, "VIDO");
  225. if(stream->enc->codec_id == CODEC_ID_RV10)
  226. ffio_wfourcc(s,"RV10");
  227. else
  228. ffio_wfourcc(s,"RV20");
  229. avio_wb16(s, stream->enc->width);
  230. avio_wb16(s, stream->enc->height);
  231. avio_wb16(s, (int) stream->frame_rate); /* frames per seconds ? */
  232. avio_wb32(s,0); /* unknown meaning */
  233. avio_wb16(s, (int) stream->frame_rate); /* unknown meaning */
  234. avio_wb32(s,0); /* unknown meaning */
  235. avio_wb16(s, 8); /* unknown meaning */
  236. /* Seems to be the codec version: only use basic H263. The next
  237. versions seems to add a diffential DC coding as in
  238. MPEG... nothing new under the sun */
  239. if(stream->enc->codec_id == CODEC_ID_RV10)
  240. avio_wb32(s,0x10000000);
  241. else
  242. avio_wb32(s,0x20103001);
  243. //avio_wb32(s,0x10003000);
  244. }
  245. }
  246. /* patch data offset field */
  247. data_pos = s->buf_ptr - start_ptr;
  248. rm->data_pos = data_pos;
  249. data_offset_ptr[0] = data_pos >> 24;
  250. data_offset_ptr[1] = data_pos >> 16;
  251. data_offset_ptr[2] = data_pos >> 8;
  252. data_offset_ptr[3] = data_pos;
  253. /* data stream */
  254. ffio_wfourcc(s, "DATA");
  255. avio_wb32(s,data_size + 10 + 8);
  256. avio_wb16(s,0);
  257. avio_wb32(s, nb_packets); /* number of packets */
  258. avio_wb32(s,0); /* next data header */
  259. return 0;
  260. }
  261. static void write_packet_header(AVFormatContext *ctx, StreamInfo *stream,
  262. int length, int key_frame)
  263. {
  264. int timestamp;
  265. AVIOContext *s = ctx->pb;
  266. stream->nb_packets++;
  267. stream->packet_total_size += length;
  268. if (length > stream->packet_max_size)
  269. stream->packet_max_size = length;
  270. avio_wb16(s,0); /* version */
  271. avio_wb16(s,length + 12);
  272. avio_wb16(s, stream->num); /* stream number */
  273. timestamp = (1000 * (float)stream->nb_frames) / stream->frame_rate;
  274. avio_wb32(s, timestamp); /* timestamp */
  275. avio_w8(s, 0); /* reserved */
  276. avio_w8(s, key_frame ? 2 : 0); /* flags */
  277. }
  278. static int rm_write_header(AVFormatContext *s)
  279. {
  280. RMMuxContext *rm = s->priv_data;
  281. StreamInfo *stream;
  282. int n;
  283. AVCodecContext *codec;
  284. for(n=0;n<s->nb_streams;n++) {
  285. s->streams[n]->id = n;
  286. codec = s->streams[n]->codec;
  287. stream = &rm->streams[n];
  288. memset(stream, 0, sizeof(StreamInfo));
  289. stream->num = n;
  290. stream->bit_rate = codec->bit_rate;
  291. stream->enc = codec;
  292. switch(codec->codec_type) {
  293. case AVMEDIA_TYPE_AUDIO:
  294. rm->audio_stream = stream;
  295. stream->frame_rate = (float)codec->sample_rate / (float)codec->frame_size;
  296. /* XXX: dummy values */
  297. stream->packet_max_size = 1024;
  298. stream->nb_packets = 0;
  299. stream->total_frames = stream->nb_packets;
  300. break;
  301. case AVMEDIA_TYPE_VIDEO:
  302. rm->video_stream = stream;
  303. stream->frame_rate = (float)codec->time_base.den / (float)codec->time_base.num;
  304. /* XXX: dummy values */
  305. stream->packet_max_size = 4096;
  306. stream->nb_packets = 0;
  307. stream->total_frames = stream->nb_packets;
  308. break;
  309. default:
  310. return -1;
  311. }
  312. }
  313. if (rv10_write_header(s, 0, 0))
  314. return AVERROR_INVALIDDATA;
  315. avio_flush(s->pb);
  316. return 0;
  317. }
  318. static int rm_write_audio(AVFormatContext *s, const uint8_t *buf, int size, int flags)
  319. {
  320. uint8_t *buf1;
  321. RMMuxContext *rm = s->priv_data;
  322. AVIOContext *pb = s->pb;
  323. StreamInfo *stream = rm->audio_stream;
  324. int i;
  325. /* XXX: suppress this malloc */
  326. buf1= (uint8_t*) av_malloc( size * sizeof(uint8_t) );
  327. write_packet_header(s, stream, size, !!(flags & AV_PKT_FLAG_KEY));
  328. if (stream->enc->codec_id == CODEC_ID_AC3) {
  329. /* for AC-3, the words seem to be reversed */
  330. for(i=0;i<size;i+=2) {
  331. buf1[i] = buf[i+1];
  332. buf1[i+1] = buf[i];
  333. }
  334. avio_write(pb, buf1, size);
  335. } else {
  336. avio_write(pb, buf, size);
  337. }
  338. avio_flush(pb);
  339. stream->nb_frames++;
  340. av_free(buf1);
  341. return 0;
  342. }
  343. static int rm_write_video(AVFormatContext *s, const uint8_t *buf, int size, int flags)
  344. {
  345. RMMuxContext *rm = s->priv_data;
  346. AVIOContext *pb = s->pb;
  347. StreamInfo *stream = rm->video_stream;
  348. int key_frame = !!(flags & AV_PKT_FLAG_KEY);
  349. /* XXX: this is incorrect: should be a parameter */
  350. /* Well, I spent some time finding the meaning of these bits. I am
  351. not sure I understood everything, but it works !! */
  352. #if 1
  353. write_packet_header(s, stream, size + 7 + (size >= 0x4000)*4, key_frame);
  354. /* bit 7: '1' if final packet of a frame converted in several packets */
  355. avio_w8(pb, 0x81);
  356. /* bit 7: '1' if I frame. bits 6..0 : sequence number in current
  357. frame starting from 1 */
  358. if (key_frame) {
  359. avio_w8(pb, 0x81);
  360. } else {
  361. avio_w8(pb, 0x01);
  362. }
  363. if(size >= 0x4000){
  364. avio_wb32(pb, size); /* total frame size */
  365. avio_wb32(pb, size); /* offset from the start or the end */
  366. }else{
  367. avio_wb16(pb, 0x4000 | size); /* total frame size */
  368. avio_wb16(pb, 0x4000 | size); /* offset from the start or the end */
  369. }
  370. #else
  371. /* full frame */
  372. write_packet_header(s, size + 6);
  373. avio_w8(pb, 0xc0);
  374. avio_wb16(pb, 0x4000 + size); /* total frame size */
  375. avio_wb16(pb, 0x4000 + packet_number * 126); /* position in stream */
  376. #endif
  377. avio_w8(pb, stream->nb_frames & 0xff);
  378. avio_write(pb, buf, size);
  379. avio_flush(pb);
  380. stream->nb_frames++;
  381. return 0;
  382. }
  383. static int rm_write_packet(AVFormatContext *s, AVPacket *pkt)
  384. {
  385. if (s->streams[pkt->stream_index]->codec->codec_type ==
  386. AVMEDIA_TYPE_AUDIO)
  387. return rm_write_audio(s, pkt->data, pkt->size, pkt->flags);
  388. else
  389. return rm_write_video(s, pkt->data, pkt->size, pkt->flags);
  390. }
  391. static int rm_write_trailer(AVFormatContext *s)
  392. {
  393. RMMuxContext *rm = s->priv_data;
  394. int data_size, index_pos, i;
  395. AVIOContext *pb = s->pb;
  396. if (!url_is_streamed(s->pb)) {
  397. /* end of file: finish to write header */
  398. index_pos = avio_tell(pb);
  399. data_size = index_pos - rm->data_pos;
  400. /* FIXME: write index */
  401. /* undocumented end header */
  402. avio_wb32(pb, 0);
  403. avio_wb32(pb, 0);
  404. avio_seek(pb, 0, SEEK_SET);
  405. for(i=0;i<s->nb_streams;i++)
  406. rm->streams[i].total_frames = rm->streams[i].nb_frames;
  407. rv10_write_header(s, data_size, 0);
  408. } else {
  409. /* undocumented end header */
  410. avio_wb32(pb, 0);
  411. avio_wb32(pb, 0);
  412. }
  413. avio_flush(pb);
  414. return 0;
  415. }
  416. AVOutputFormat ff_rm_muxer = {
  417. "rm",
  418. NULL_IF_CONFIG_SMALL("RealMedia format"),
  419. "application/vnd.rn-realmedia",
  420. "rm,ra",
  421. sizeof(RMMuxContext),
  422. CODEC_ID_AC3,
  423. CODEC_ID_RV10,
  424. rm_write_header,
  425. rm_write_packet,
  426. rm_write_trailer,
  427. .codec_tag= (const AVCodecTag* const []){ff_rm_codec_tags, 0},
  428. };