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.

602 lines
14KB

  1. /*
  2. * "NUT" Container Format muxer and demuxer (DRAFT-20031003)
  3. * Copyright (c) 2003 Alex Beregszaszi
  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 General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. * NUT DRAFT can be found in MPlayer CVS at DOCS/tech/mpcf.txt
  20. *
  21. * AND http://people.fsn.hu/~alex/nut/ (TeX, pdf, ps, dvi, ..)
  22. *
  23. */
  24. /*
  25. * TODO:
  26. * - checksumming
  27. * - optimal timestamp handling
  28. * - index writing
  29. * - info and index packet reading support
  30. * - startcode searching for broken streams
  31. * - subpacket support
  32. */
  33. //#define DEBUG 1
  34. #include "avformat.h"
  35. #include "mpegaudio.h"
  36. #include "avi.h"
  37. //from /dev/random
  38. #define MAIN_STARTCODE (0xF9526A6200000000ULL + ('N'<<24) + ('U'<<16) + ('T'<<8) + 'M')
  39. #define STREAM_STARTCODE (0xD667773F00000000ULL + ('N'<<24) + ('U'<<16) + ('T'<<8) + 'S')
  40. #define KEYFRAME_STARTCODE (0xCB86308700000000ULL + ('N'<<24) + ('U'<<16) + ('T'<<8) + 'K')
  41. #define INDEX_STARTCODE (0xEBFCDE0E00000000ULL + ('N'<<24) + ('U'<<16) + ('T'<<8) + 'X')
  42. #define INFO_STARTCODE (0xA37B643500000000ULL + ('N'<<24) + ('U'<<16) + ('T'<<8) + 'I')
  43. typedef struct {
  44. int curr_frame_start;
  45. int last_frame_size;
  46. int curr_frame_size;
  47. } NUTContext;
  48. static int bytes_left(ByteIOContext *bc)
  49. {
  50. return bc->buf_end - bc->buf_ptr;
  51. }
  52. static uint64_t get_v(ByteIOContext *bc)
  53. {
  54. uint64_t val = 0;
  55. for(; bytes_left(bc) > 0; )
  56. {
  57. int tmp = get_byte(bc);
  58. if (tmp&0x80)
  59. val= (val<<7) + tmp - 0x80;
  60. else
  61. return (val<<7) + tmp;
  62. }
  63. return -1;
  64. }
  65. static int64_t get_s(ByteIOContext *bc)
  66. {
  67. int64_t v = get_v(bc) + 1;
  68. if (v&1)
  69. return -(v>>1);
  70. else
  71. return (v>>1);
  72. }
  73. static int get_b(ByteIOContext *bc, char *data, int maxlen)
  74. {
  75. int i, len;
  76. len = get_v(bc);
  77. for (i = 0; i < len && i < maxlen; i++)
  78. data[i] = get_byte(bc);
  79. /* skip remaining bytes */
  80. url_fskip(bc, len-i);
  81. return 0;
  82. }
  83. static int get_bi(ByteIOContext *bc)
  84. {
  85. int i, len, val = 0;
  86. len = get_v(bc);
  87. for (i = 0; i < len && i <= 4; i++)
  88. val |= get_byte(bc) << (i * 8);
  89. /* skip remaining bytes */
  90. url_fskip(bc, len-i);
  91. return val;
  92. }
  93. static int get_packetheader(NUTContext *nut, ByteIOContext *bc)
  94. {
  95. nut->curr_frame_start = url_ftell(bc);
  96. nut->curr_frame_size = get_v(bc);
  97. nut->last_frame_size = get_v(bc);
  98. dprintf("Packet: fwd: %d bwd: %d\n",
  99. nut->curr_frame_size, nut->last_frame_size);
  100. return 0;
  101. }
  102. /**
  103. *
  104. */
  105. static int get_length(uint64_t val){
  106. int i;
  107. for (i=7; ; i+=7)
  108. if ((val>>i) == 0)
  109. return i;
  110. return 7; //not reached
  111. }
  112. #ifdef CONFIG_ENCODERS
  113. static int put_v(ByteIOContext *bc, uint64_t val)
  114. {
  115. int i;
  116. // if (bytes_left(s)*8 < 9)
  117. // return -1;
  118. if (bytes_left(bc) < 1)
  119. return -1;
  120. val &= 0x7FFFFFFFFFFFFFFFULL; // FIXME can only encode upto 63 bits currently
  121. i= get_length(val);
  122. for (i-=7; i>0; i-=7){
  123. put_byte(bc, 0x80 | (val>>i));
  124. }
  125. put_byte(bc, val&0x7f);
  126. return 0;
  127. }
  128. static int put_s(ByteIOContext *bc, uint64_t val)
  129. {
  130. if (val<=0)
  131. return put_v(bc, -2*val);
  132. else
  133. return put_v(bc, 2*val-1);
  134. }
  135. static int put_b(ByteIOContext *bc, char *data, int len)
  136. {
  137. int i;
  138. put_v(bc, len);
  139. for (i = 0; i < len; i++)
  140. put_byte(bc, data[i]);
  141. return 0;
  142. }
  143. static int put_bi(ByteIOContext *bc, int val)
  144. {
  145. put_v(bc, 4);
  146. put_le32(bc, val);
  147. return 0;
  148. }
  149. static int put_packetheader(NUTContext *nut, ByteIOContext *bc, int max_size)
  150. {
  151. put_flush_packet(bc);
  152. nut->curr_frame_start = url_ftell(bc);
  153. nut->curr_frame_size = max_size;
  154. /* packet header */
  155. put_v(bc, nut->curr_frame_size); /* forward ptr */
  156. put_v(bc, nut->last_frame_size); /* backward ptr */
  157. dprintf("Packet: fwd: %d, bwd: %d\n",
  158. nut->curr_frame_size, nut->last_frame_size);
  159. nut->last_frame_size = nut->curr_frame_size;
  160. return 0;
  161. }
  162. static int update_packetheader(NUTContext *nut, ByteIOContext *bc, int additional_size){
  163. offset_t start= nut->curr_frame_start;
  164. offset_t cur= url_ftell(bc);
  165. int size= cur - start + additional_size;
  166. assert( size <= nut->curr_frame_size );
  167. url_fseek(bc, start, SEEK_SET);
  168. put_v(bc, size);
  169. if(get_length(size) < get_length(nut->curr_frame_size))
  170. put_byte(bc, 0x80);
  171. nut->curr_frame_size= size;
  172. dprintf("Packet update: size: %d\n", size);
  173. url_fseek(bc, cur, SEEK_SET);
  174. return 0;
  175. }
  176. static int nut_write_header(AVFormatContext *s)
  177. {
  178. NUTContext *nut = s->priv_data;
  179. ByteIOContext *bc = &s->pb;
  180. AVCodecContext *codec;
  181. int i;
  182. /* main header */
  183. put_be64(bc, MAIN_STARTCODE);
  184. put_packetheader(nut, bc, 120);
  185. put_v(bc, 0); /* version */
  186. put_v(bc, s->nb_streams);
  187. put_be32(bc, 0); /* FIXME: checksum */
  188. update_packetheader(nut, bc, 0);
  189. /* stream headers */
  190. for (i = 0; i < s->nb_streams; i++)
  191. {
  192. int nom, denom;
  193. codec = &s->streams[i]->codec;
  194. put_be64(bc, STREAM_STARTCODE);
  195. put_packetheader(nut, bc, 120);
  196. put_v(bc, i /*s->streams[i]->index*/);
  197. put_v(bc, (codec->codec_type == CODEC_TYPE_AUDIO) ? 32 : 0);
  198. if (codec->codec_tag)
  199. put_bi(bc, codec->codec_tag);
  200. else if (codec->codec_type == CODEC_TYPE_VIDEO)
  201. {
  202. int tmp = codec_get_bmp_tag(codec->codec_id);
  203. put_bi(bc, tmp);
  204. }
  205. else if (codec->codec_type == CODEC_TYPE_AUDIO)
  206. {
  207. int tmp = codec_get_wav_tag(codec->codec_id);
  208. put_bi(bc, tmp);
  209. }
  210. if (codec->codec_type == CODEC_TYPE_VIDEO)
  211. {
  212. nom = codec->frame_rate;
  213. denom = codec->frame_rate_base;
  214. }
  215. else
  216. {
  217. nom = codec->sample_rate/8;
  218. denom = 8;
  219. }
  220. put_v(bc, codec->bit_rate);
  221. put_v(bc, 0); /* no language code */
  222. put_v(bc, nom);
  223. put_v(bc, denom);
  224. put_v(bc, 0); /* msb timestamp_shift */
  225. put_v(bc, 0); /* shuffle type */
  226. put_byte(bc, 0); /* flags: 0x1 - fixed_fps, 0x2 - index_present */
  227. put_v(bc, 0); /* no codec specific headers */
  228. switch(codec->codec_type)
  229. {
  230. case CODEC_TYPE_AUDIO:
  231. put_v(bc, (codec->sample_rate * denom) / nom);
  232. put_v(bc, codec->channels);
  233. put_be32(bc, 0); /* FIXME: checksum */
  234. break;
  235. case CODEC_TYPE_VIDEO:
  236. put_v(bc, codec->width);
  237. put_v(bc, codec->height);
  238. put_v(bc, 0); /* aspected w */
  239. put_v(bc, 0); /* aspected h */
  240. put_v(bc, 0); /* csp type -- unknown */
  241. put_be32(bc, 0); /* FIXME: checksum */
  242. break;
  243. default:
  244. break;
  245. }
  246. update_packetheader(nut, bc, 0);
  247. }
  248. #if 0
  249. /* info header */
  250. put_be64(bc, INFO_STARTCODE);
  251. put_packetheader(nut, bc, 16+strlen(s->author)+strlen(s->title)+
  252. strlen(s->comment)+strlen(s->copyright));
  253. if (s->author[0])
  254. {
  255. put_v(bc, 5); /* type */
  256. put_b(bc, s->author, strlen(s->author));
  257. }
  258. if (s->title[0])
  259. {
  260. put_v(bc, 6); /* type */
  261. put_b(bc, s->title, strlen(s->title));
  262. }
  263. if (s->comment[0])
  264. {
  265. put_v(bc, 7); /* type */
  266. put_b(bc, s->comment, strlen(s->comment));
  267. }
  268. if (s->copyright[0])
  269. {
  270. put_v(bc, 8); /* type */
  271. put_b(bc, s->copyright, strlen(s->copyright));
  272. }
  273. /* encoder */
  274. put_v(bc, 9); /* type */
  275. put_b(bc, LIBAVFORMAT_IDENT "\0", strlen(LIBAVFORMAT_IDENT));
  276. put_v(bc, 0); /* eof info */
  277. put_be32(bc, 0); /* FIXME: checksum */
  278. update_packetheader(nut, bc, 0);
  279. #endif
  280. put_flush_packet(bc);
  281. return 0;
  282. }
  283. static int nut_write_packet(AVFormatContext *s, int stream_index,
  284. const uint8_t *buf, int size, int64_t pts)
  285. {
  286. NUTContext *nut = s->priv_data;
  287. ByteIOContext *bc = &s->pb;
  288. int key_frame = 0;
  289. int flags;
  290. AVCodecContext *enc;
  291. if (stream_index > s->nb_streams)
  292. return 1;
  293. enc = &s->streams[stream_index]->codec;
  294. key_frame = enc->coded_frame->key_frame;
  295. if (key_frame)
  296. put_be64(bc, KEYFRAME_STARTCODE);
  297. flags=0;
  298. flags<<=2; flags|=1; //priority
  299. flags<<=1; flags|=0; //checksum
  300. flags<<=1; flags|=0; //msb_timestamp_flag
  301. flags<<=2; flags|=1; //subpacket_type
  302. flags<<=1; flags|=0; //reserved
  303. put_byte(bc, flags);
  304. put_packetheader(nut, bc, size+20);
  305. put_v(bc, stream_index);
  306. put_s(bc, pts); /* lsb_timestamp */
  307. update_packetheader(nut, bc, size);
  308. put_buffer(bc, buf, size);
  309. put_flush_packet(bc);
  310. return 0;
  311. }
  312. static int nut_write_trailer(AVFormatContext *s)
  313. {
  314. ByteIOContext *bc = &s->pb;
  315. #if 0
  316. int i;
  317. /* WRITE INDEX */
  318. for (i = 0; s->nb_streams; i++)
  319. {
  320. put_be64(bc, INDEX_STARTCODE);
  321. put_packetheader(nut, bc, 64);
  322. put_v(bc, s->streams[i]->id);
  323. put_v(bc, ...);
  324. put_be32(bc, 0); /* FIXME: checksum */
  325. update_packetheader(nut, bc, 0);
  326. }
  327. #endif
  328. put_flush_packet(bc);
  329. return 0;
  330. }
  331. #endif //CONFIG_ENCODERS
  332. static int nut_probe(AVProbeData *p)
  333. {
  334. int i;
  335. uint64_t code;
  336. code = 0xff;
  337. for (i = 0; i < p->buf_size; i++) {
  338. int c = p->buf[i];
  339. code = (code << 8) | c;
  340. if (code == MAIN_STARTCODE)
  341. return AVPROBE_SCORE_MAX;
  342. }
  343. return 0;
  344. }
  345. static int nut_read_header(AVFormatContext *s, AVFormatParameters *ap)
  346. {
  347. NUTContext *nut = s->priv_data;
  348. ByteIOContext *bc = &s->pb;
  349. uint64_t tmp;
  350. int cur_stream, nb_streams;
  351. /* main header */
  352. tmp = get_be64(bc);
  353. if (tmp != MAIN_STARTCODE)
  354. fprintf(stderr, "damaged? startcode!=1 (%Ld)\n", tmp);
  355. get_packetheader(nut, bc);
  356. tmp = get_v(bc);
  357. if (tmp != 0)
  358. fprintf(stderr, "bad version (%Ld)\n", tmp);
  359. nb_streams = get_v(bc);
  360. get_be32(bc); /* checkusm */
  361. s->bit_rate = 0;
  362. /* stream header */
  363. for (cur_stream = 0; cur_stream < nb_streams; cur_stream++)
  364. {
  365. int class, nom, denom;
  366. AVStream *st;
  367. tmp = get_be64(bc);
  368. if (tmp != STREAM_STARTCODE)
  369. fprintf(stderr, "damaged? startcode!=1 (%Ld)\n", tmp);
  370. get_packetheader(nut, bc);
  371. st = av_new_stream(s, get_v(bc));
  372. if (!st)
  373. return AVERROR_NOMEM;
  374. class = get_v(bc);
  375. tmp = get_bi(bc);
  376. switch(class)
  377. {
  378. case 0:
  379. st->codec.codec_type = CODEC_TYPE_VIDEO;
  380. st->codec.codec_id = codec_get_bmp_id(tmp);
  381. if (st->codec.codec_id == CODEC_ID_NONE)
  382. fprintf(stderr, "Unknown codec?!\n");
  383. break;
  384. case 32:
  385. st->codec.codec_type = CODEC_TYPE_AUDIO;
  386. st->codec.codec_id = codec_get_wav_id(tmp);
  387. if (st->codec.codec_id == CODEC_ID_NONE)
  388. fprintf(stderr, "Unknown codec?!\n");
  389. break;
  390. default:
  391. fprintf(stderr, "Unknown stream class (%d)\n", class);
  392. return -1;
  393. }
  394. s->bit_rate += get_v(bc);
  395. get_b(bc, NULL, 0); /* language code */
  396. nom = get_v(bc);
  397. denom = get_v(bc);
  398. get_v(bc); /* FIXME: msb timestamp base */
  399. get_v(bc); /* shuffle type */
  400. get_byte(bc); /* flags */
  401. /* codec specific data headers */
  402. while(get_v(bc) != 0)
  403. url_fskip(bc, get_v(bc));
  404. if (class == 0) /* VIDEO */
  405. {
  406. st->codec.width = get_v(bc);
  407. st->codec.height = get_v(bc);
  408. get_v(bc); /* aspected w */
  409. get_v(bc); /* aspected h */
  410. get_v(bc); /* csp type */
  411. get_be32(bc); /* checksum */
  412. st->codec.frame_rate = nom;
  413. st->codec.frame_rate_base = denom;
  414. }
  415. if (class == 32) /* AUDIO */
  416. {
  417. st->codec.sample_rate = (get_v(bc) * nom) / denom;
  418. st->codec.channels = get_v(bc);
  419. get_be32(bc); /* checksum */
  420. }
  421. }
  422. return 0;
  423. }
  424. static int nut_read_packet(AVFormatContext *s, AVPacket *pkt)
  425. {
  426. NUTContext *nut = s->priv_data;
  427. ByteIOContext *bc = &s->pb;
  428. int id, timestamp, size;
  429. int key_frame = 0;
  430. uint64_t tmp;
  431. if (url_feof(bc))
  432. return -1;
  433. tmp = get_byte(bc);
  434. if (tmp & 0x80) /* zero bit set? */
  435. {
  436. tmp<<=8 ; tmp |= get_byte(bc);
  437. tmp<<=16; tmp |= get_be16(bc);
  438. tmp<<=32; tmp |= get_be32(bc);
  439. if (tmp == KEYFRAME_STARTCODE)
  440. {
  441. key_frame = 1;
  442. tmp = get_byte(bc); /* flags */
  443. }
  444. else
  445. fprintf(stderr, "error in zero bit / startcode %LX\n", tmp);
  446. }
  447. get_packetheader(nut, bc);
  448. #if 0
  449. if (((tmp & 0x60)>>5) > 3) /* priority <= 3 */
  450. fprintf(stderr, "sanity check failed!\n");
  451. #endif
  452. id = get_v(bc);
  453. timestamp = get_s(bc);
  454. size = (nut->curr_frame_size - (url_ftell(bc)-nut->curr_frame_start));
  455. dprintf("flags: 0x%Lx, timestamp: %d, packet size: %d\n", tmp, timestamp, size);
  456. if (size < 0)
  457. return -1;
  458. av_new_packet(pkt, size);
  459. get_buffer(bc, pkt->data, size);
  460. pkt->stream_index = id;
  461. if (key_frame)
  462. pkt->flags |= PKT_FLAG_KEY;
  463. pkt->pts = timestamp;
  464. return 0;
  465. }
  466. static AVInputFormat nut_iformat = {
  467. "nut",
  468. "nut format",
  469. sizeof(NUTContext),
  470. nut_probe,
  471. nut_read_header,
  472. nut_read_packet,
  473. // nut_read_close,
  474. // nut_read_seek,
  475. .extensions = "nut",
  476. };
  477. #ifdef CONFIG_ENCODERS
  478. static AVOutputFormat nut_oformat = {
  479. "nut",
  480. "nut format",
  481. "video/x-nut",
  482. "nut",
  483. sizeof(NUTContext),
  484. #ifdef CONFIG_VORBIS
  485. CODEC_ID_VORBIS,
  486. #elif defined(CONFIG_MP3LAME)
  487. CODEC_ID_MP3,
  488. #else
  489. CODEC_ID_MP2, /* AC3 needs liba52 decoder */
  490. #endif
  491. CODEC_ID_MPEG4,
  492. nut_write_header,
  493. nut_write_packet,
  494. nut_write_trailer,
  495. };
  496. #endif //CONFIG_ENCODERS
  497. int nut_init(void)
  498. {
  499. av_register_input_format(&nut_iformat);
  500. #ifdef CONFIG_ENCODERS
  501. av_register_output_format(&nut_oformat);
  502. #endif //CONFIG_ENCODERS
  503. return 0;
  504. }