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.

637 lines
15KB

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