rmenc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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. /* the header needs at most 7 + 4 + 12 B */
  45. #define MAX_HEADER_SIZE (7 + 4 + 12)
  46. /* UINT16_MAX is the maximal chunk size */
  47. #define MAX_PACKET_SIZE (UINT16_MAX - MAX_HEADER_SIZE)
  48. static void put_str(AVIOContext *s, const char *tag)
  49. {
  50. avio_wb16(s,strlen(tag));
  51. while (*tag) {
  52. avio_w8(s, *tag++);
  53. }
  54. }
  55. static void put_str8(AVIOContext *s, const char *tag)
  56. {
  57. avio_w8(s, strlen(tag));
  58. while (*tag) {
  59. avio_w8(s, *tag++);
  60. }
  61. }
  62. static int rv10_write_header(AVFormatContext *ctx,
  63. int data_size, int index_pos)
  64. {
  65. RMMuxContext *rm = ctx->priv_data;
  66. AVIOContext *s = ctx->pb;
  67. StreamInfo *stream;
  68. unsigned char *data_offset_ptr, *start_ptr;
  69. const char *desc, *mimetype;
  70. int nb_packets, packet_total_size, packet_max_size, size, packet_avg_size, i;
  71. int bit_rate, v, duration, flags, data_pos;
  72. AVDictionaryEntry *tag;
  73. start_ptr = s->buf_ptr;
  74. ffio_wfourcc(s, ".RMF");
  75. avio_wb32(s,18); /* header size */
  76. avio_wb16(s,0);
  77. avio_wb32(s,0);
  78. avio_wb32(s,4 + ctx->nb_streams); /* num headers */
  79. ffio_wfourcc(s,"PROP");
  80. avio_wb32(s, 50);
  81. avio_wb16(s, 0);
  82. packet_max_size = 0;
  83. packet_total_size = 0;
  84. nb_packets = 0;
  85. bit_rate = 0;
  86. duration = 0;
  87. for(i=0;i<ctx->nb_streams;i++) {
  88. StreamInfo *stream = &rm->streams[i];
  89. bit_rate += stream->bit_rate;
  90. if (stream->packet_max_size > packet_max_size)
  91. packet_max_size = stream->packet_max_size;
  92. nb_packets += stream->nb_packets;
  93. packet_total_size += stream->packet_total_size;
  94. /* select maximum duration */
  95. v = (int) (1000.0 * (float)stream->total_frames / stream->frame_rate);
  96. if (v > duration)
  97. duration = v;
  98. }
  99. avio_wb32(s, bit_rate); /* max bit rate */
  100. avio_wb32(s, bit_rate); /* avg bit rate */
  101. avio_wb32(s, packet_max_size); /* max packet size */
  102. if (nb_packets > 0)
  103. packet_avg_size = packet_total_size / nb_packets;
  104. else
  105. packet_avg_size = 0;
  106. avio_wb32(s, packet_avg_size); /* avg packet size */
  107. avio_wb32(s, nb_packets); /* num packets */
  108. avio_wb32(s, duration); /* duration */
  109. avio_wb32(s, BUFFER_DURATION); /* preroll */
  110. avio_wb32(s, index_pos); /* index offset */
  111. /* computation of data the data offset */
  112. data_offset_ptr = s->buf_ptr;
  113. avio_wb32(s, 0); /* data offset : will be patched after */
  114. avio_wb16(s, ctx->nb_streams); /* num streams */
  115. flags = 1 | 2; /* save allowed & perfect play */
  116. if (!s->seekable)
  117. flags |= 4; /* live broadcast */
  118. avio_wb16(s, flags);
  119. /* comments */
  120. ffio_wfourcc(s,"CONT");
  121. size = 4 * 2 + 10;
  122. for(i=0; i<FF_ARRAY_ELEMS(ff_rm_metadata); i++) {
  123. tag = av_dict_get(ctx->metadata, ff_rm_metadata[i], NULL, 0);
  124. if(tag) size += strlen(tag->value);
  125. }
  126. avio_wb32(s,size);
  127. avio_wb16(s,0);
  128. for(i=0; i<FF_ARRAY_ELEMS(ff_rm_metadata); i++) {
  129. tag = av_dict_get(ctx->metadata, ff_rm_metadata[i], NULL, 0);
  130. put_str(s, tag ? tag->value : "");
  131. }
  132. for(i=0;i<ctx->nb_streams;i++) {
  133. int codec_data_size;
  134. stream = &rm->streams[i];
  135. if (stream->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
  136. desc = "The Audio Stream";
  137. mimetype = "audio/x-pn-realaudio";
  138. codec_data_size = 73;
  139. } else {
  140. desc = "The Video Stream";
  141. mimetype = "video/x-pn-realvideo";
  142. codec_data_size = 34;
  143. }
  144. ffio_wfourcc(s,"MDPR");
  145. size = 10 + 9 * 4 + strlen(desc) + strlen(mimetype) + codec_data_size;
  146. avio_wb32(s, size);
  147. avio_wb16(s, 0);
  148. avio_wb16(s, i); /* stream number */
  149. avio_wb32(s, stream->bit_rate); /* max bit rate */
  150. avio_wb32(s, stream->bit_rate); /* avg bit rate */
  151. avio_wb32(s, stream->packet_max_size); /* max packet size */
  152. if (stream->nb_packets > 0)
  153. packet_avg_size = stream->packet_total_size /
  154. stream->nb_packets;
  155. else
  156. packet_avg_size = 0;
  157. avio_wb32(s, packet_avg_size); /* avg packet size */
  158. avio_wb32(s, 0); /* start time */
  159. avio_wb32(s, BUFFER_DURATION); /* preroll */
  160. /* duration */
  161. if (!s->seekable || !stream->total_frames)
  162. avio_wb32(s, (int)(3600 * 1000));
  163. else
  164. avio_wb32(s, (int)(stream->total_frames * 1000 / stream->frame_rate));
  165. put_str8(s, desc);
  166. put_str8(s, mimetype);
  167. avio_wb32(s, codec_data_size);
  168. if (stream->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
  169. int coded_frame_size, fscode, sample_rate;
  170. sample_rate = stream->enc->sample_rate;
  171. coded_frame_size = (stream->enc->bit_rate *
  172. stream->enc->frame_size) / (8 * sample_rate);
  173. /* audio codec info */
  174. avio_write(s, ".ra", 3);
  175. avio_w8(s, 0xfd);
  176. avio_wb32(s, 0x00040000); /* version */
  177. ffio_wfourcc(s, ".ra4");
  178. avio_wb32(s, 0x01b53530); /* stream length */
  179. avio_wb16(s, 4); /* unknown */
  180. avio_wb32(s, 0x39); /* header size */
  181. switch(sample_rate) {
  182. case 48000:
  183. case 24000:
  184. case 12000:
  185. fscode = 1;
  186. break;
  187. default:
  188. case 44100:
  189. case 22050:
  190. case 11025:
  191. fscode = 2;
  192. break;
  193. case 32000:
  194. case 16000:
  195. case 8000:
  196. fscode = 3;
  197. }
  198. avio_wb16(s, fscode); /* codec additional info, for AC-3, seems
  199. to be a frequency code */
  200. /* special hack to compensate rounding errors... */
  201. if (coded_frame_size == 557)
  202. coded_frame_size--;
  203. avio_wb32(s, coded_frame_size); /* frame length */
  204. avio_wb32(s, 0x51540); /* unknown */
  205. avio_wb32(s, 0x249f0); /* unknown */
  206. avio_wb32(s, 0x249f0); /* unknown */
  207. avio_wb16(s, 0x01);
  208. /* frame length : seems to be very important */
  209. avio_wb16(s, coded_frame_size);
  210. avio_wb32(s, 0); /* unknown */
  211. avio_wb16(s, stream->enc->sample_rate); /* sample rate */
  212. avio_wb32(s, 0x10); /* unknown */
  213. avio_wb16(s, stream->enc->channels);
  214. put_str8(s, "Int0"); /* codec name */
  215. if (stream->enc->codec_tag) {
  216. avio_w8(s, 4); /* tag length */
  217. avio_wl32(s, stream->enc->codec_tag);
  218. } else {
  219. av_log(ctx, AV_LOG_ERROR, "Invalid codec tag\n");
  220. return -1;
  221. }
  222. avio_wb16(s, 0); /* title length */
  223. avio_wb16(s, 0); /* author length */
  224. avio_wb16(s, 0); /* copyright length */
  225. avio_w8(s, 0); /* end of header */
  226. } else {
  227. /* video codec info */
  228. avio_wb32(s,34); /* size */
  229. ffio_wfourcc(s, "VIDO");
  230. if(stream->enc->codec_id == CODEC_ID_RV10)
  231. ffio_wfourcc(s,"RV10");
  232. else
  233. ffio_wfourcc(s,"RV20");
  234. avio_wb16(s, stream->enc->width);
  235. avio_wb16(s, stream->enc->height);
  236. avio_wb16(s, (int) stream->frame_rate); /* frames per seconds ? */
  237. avio_wb32(s,0); /* unknown meaning */
  238. avio_wb16(s, (int) stream->frame_rate); /* unknown meaning */
  239. avio_wb32(s,0); /* unknown meaning */
  240. avio_wb16(s, 8); /* unknown meaning */
  241. /* Seems to be the codec version: only use basic H263. The next
  242. versions seems to add a diffential DC coding as in
  243. MPEG... nothing new under the sun */
  244. if(stream->enc->codec_id == CODEC_ID_RV10)
  245. avio_wb32(s,0x10000000);
  246. else
  247. avio_wb32(s,0x20103001);
  248. //avio_wb32(s,0x10003000);
  249. }
  250. }
  251. /* patch data offset field */
  252. data_pos = s->buf_ptr - start_ptr;
  253. rm->data_pos = data_pos;
  254. data_offset_ptr[0] = data_pos >> 24;
  255. data_offset_ptr[1] = data_pos >> 16;
  256. data_offset_ptr[2] = data_pos >> 8;
  257. data_offset_ptr[3] = data_pos;
  258. /* data stream */
  259. ffio_wfourcc(s, "DATA");
  260. avio_wb32(s,data_size + 10 + 8);
  261. avio_wb16(s,0);
  262. avio_wb32(s, nb_packets); /* number of packets */
  263. avio_wb32(s,0); /* next data header */
  264. return 0;
  265. }
  266. static void write_packet_header(AVFormatContext *ctx, StreamInfo *stream,
  267. int length, int key_frame)
  268. {
  269. int timestamp;
  270. AVIOContext *s = ctx->pb;
  271. stream->nb_packets++;
  272. stream->packet_total_size += length;
  273. if (length > stream->packet_max_size)
  274. stream->packet_max_size = length;
  275. avio_wb16(s,0); /* version */
  276. avio_wb16(s,length + 12);
  277. avio_wb16(s, stream->num); /* stream number */
  278. timestamp = (1000 * (float)stream->nb_frames) / stream->frame_rate;
  279. avio_wb32(s, timestamp); /* timestamp */
  280. avio_w8(s, 0); /* reserved */
  281. avio_w8(s, key_frame ? 2 : 0); /* flags */
  282. }
  283. static int rm_write_header(AVFormatContext *s)
  284. {
  285. RMMuxContext *rm = s->priv_data;
  286. StreamInfo *stream;
  287. int n;
  288. AVCodecContext *codec;
  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 == 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. if (size > MAX_PACKET_SIZE) {
  359. av_log_missing_feature(s, "Muxing packets larger than 64 kB", 0);
  360. return AVERROR(ENOSYS);
  361. }
  362. write_packet_header(s, stream, size + 7 + (size >= 0x4000)*4, key_frame);
  363. /* bit 7: '1' if final packet of a frame converted in several packets */
  364. avio_w8(pb, 0x81);
  365. /* bit 7: '1' if I frame. bits 6..0 : sequence number in current
  366. frame starting from 1 */
  367. if (key_frame) {
  368. avio_w8(pb, 0x81);
  369. } else {
  370. avio_w8(pb, 0x01);
  371. }
  372. if(size >= 0x4000){
  373. avio_wb32(pb, size); /* total frame size */
  374. avio_wb32(pb, size); /* offset from the start or the end */
  375. }else{
  376. avio_wb16(pb, 0x4000 | size); /* total frame size */
  377. avio_wb16(pb, 0x4000 | size); /* offset from the start or the end */
  378. }
  379. #else
  380. /* full frame */
  381. write_packet_header(s, size + 6);
  382. avio_w8(pb, 0xc0);
  383. avio_wb16(pb, 0x4000 + size); /* total frame size */
  384. avio_wb16(pb, 0x4000 + packet_number * 126); /* position in stream */
  385. #endif
  386. avio_w8(pb, stream->nb_frames & 0xff);
  387. avio_write(pb, buf, size);
  388. avio_flush(pb);
  389. stream->nb_frames++;
  390. return 0;
  391. }
  392. static int rm_write_packet(AVFormatContext *s, AVPacket *pkt)
  393. {
  394. if (s->streams[pkt->stream_index]->codec->codec_type ==
  395. AVMEDIA_TYPE_AUDIO)
  396. return rm_write_audio(s, pkt->data, pkt->size, pkt->flags);
  397. else
  398. return rm_write_video(s, pkt->data, pkt->size, pkt->flags);
  399. }
  400. static int rm_write_trailer(AVFormatContext *s)
  401. {
  402. RMMuxContext *rm = s->priv_data;
  403. int data_size, index_pos, i;
  404. AVIOContext *pb = s->pb;
  405. if (s->pb->seekable) {
  406. /* end of file: finish to write header */
  407. index_pos = avio_tell(pb);
  408. data_size = index_pos - rm->data_pos;
  409. /* FIXME: write index */
  410. /* undocumented end header */
  411. avio_wb32(pb, 0);
  412. avio_wb32(pb, 0);
  413. avio_seek(pb, 0, SEEK_SET);
  414. for(i=0;i<s->nb_streams;i++)
  415. rm->streams[i].total_frames = rm->streams[i].nb_frames;
  416. rv10_write_header(s, data_size, 0);
  417. } else {
  418. /* undocumented end header */
  419. avio_wb32(pb, 0);
  420. avio_wb32(pb, 0);
  421. }
  422. avio_flush(pb);
  423. return 0;
  424. }
  425. AVOutputFormat ff_rm_muxer = {
  426. .name = "rm",
  427. .long_name = NULL_IF_CONFIG_SMALL("RealMedia format"),
  428. .mime_type = "application/vnd.rn-realmedia",
  429. .extensions = "rm,ra",
  430. .priv_data_size = sizeof(RMMuxContext),
  431. .audio_codec = CODEC_ID_AC3,
  432. .video_codec = CODEC_ID_RV10,
  433. .write_header = rm_write_header,
  434. .write_packet = rm_write_packet,
  435. .write_trailer = rm_write_trailer,
  436. .codec_tag= (const AVCodecTag* const []){ff_rm_codec_tags, 0},
  437. };