rmenc.c 16 KB

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