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.

590 lines
13KB

  1. /*
  2. * NUT (de)muxer based on initial draft
  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. * Compatible with draft version 20030906
  22. *
  23. */
  24. /*
  25. * TODO:
  26. * - checksumming
  27. * - correct timestamp handling
  28. * - index writing
  29. * - info and index packet reading support
  30. * - startcode searching for broken streams
  31. * - subpacket support
  32. * - handling of codec specific headers
  33. */
  34. //#define DEBUG 1
  35. #include "avformat.h"
  36. #include "mpegaudio.h"
  37. #include "avi.h"
  38. //from /dev/random
  39. #define MAIN_STARTCODE (0xF9526A6200000000ULL + ('N'<<24) + ('U'<<16) + ('T'<<8) + 'M')
  40. #define STREAM_STARTCODE (0xD667773F00000000ULL + ('N'<<24) + ('U'<<16) + ('T'<<8) + 'S')
  41. #define KEYFRAME_STARTCODE (0xCB86308700000000ULL + ('N'<<24) + ('U'<<16) + ('T'<<8) + 'K')
  42. #define INDEX_STARTCODE (0xEBFCDE0E00000000ULL + ('N'<<24) + ('U'<<16) + ('T'<<8) + 'X')
  43. #define INFO_STARTCODE (0xA37B643500000000ULL + ('N'<<24) + ('U'<<16) + ('T'<<8) + 'I')
  44. typedef struct {
  45. int curr_frame_start;
  46. int last_frame_size;
  47. int curr_frame_size;
  48. } NUTContext;
  49. static int bytes_left(ByteIOContext *bc)
  50. {
  51. return bc->buf_end - bc->buf_ptr;
  52. }
  53. static uint64_t get_v(ByteIOContext *bc)
  54. {
  55. uint64_t val = 0;
  56. for(; bytes_left(bc) > 0; )
  57. {
  58. int tmp = get_byte(bc);
  59. if (tmp&0x80)
  60. val= (val<<7) + tmp - 0x80;
  61. else
  62. return (val<<7) + tmp;
  63. }
  64. return -1;
  65. }
  66. static int64_t get_s(ByteIOContext *bc)
  67. {
  68. int64_t v = get_v(bc) + 1;
  69. if (v&1)
  70. return -(v>>1);
  71. else
  72. return (v>>1);
  73. }
  74. static int get_b(ByteIOContext *bc, char *data, int maxlen)
  75. {
  76. int i, len;
  77. len = get_v(bc);
  78. for (i = 0; i < len && i < maxlen; i++)
  79. data[i] = get_byte(bc);
  80. /* skip remaining bytes */
  81. for (; i < len; i++)
  82. get_byte(bc);
  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. for (; i < len; i++)
  93. get_byte(bc);
  94. return val;
  95. }
  96. static int get_packetheader(NUTContext *nut, ByteIOContext *bc)
  97. {
  98. nut->curr_frame_start = url_ftell(bc);
  99. nut->curr_frame_size = get_v(bc);
  100. nut->last_frame_size = get_v(bc);
  101. dprintf("Packet: fwd: %d bwd: %d\n",
  102. nut->curr_frame_size, nut->last_frame_size);
  103. return 0;
  104. }
  105. /**
  106. *
  107. */
  108. static int get_length(uint64_t val){
  109. int i;
  110. for (i=7; ; i+=7)
  111. if ((val>>i) == 0)
  112. return i;
  113. return 7; //not reached
  114. }
  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. /* stream headers */
  192. for (i = 0; i < s->nb_streams; i++)
  193. {
  194. int nom, denom;
  195. codec = &s->streams[i]->codec;
  196. put_be64(bc, STREAM_STARTCODE);
  197. put_packetheader(nut, bc, 120);
  198. put_v(bc, i /*s->streams[i]->index*/);
  199. put_v(bc, (codec->codec_type == CODEC_TYPE_AUDIO) ? 32 : 0);
  200. if (codec->codec_tag)
  201. put_bi(bc, codec->codec_tag);
  202. else if (codec->codec_type == CODEC_TYPE_VIDEO)
  203. {
  204. int tmp = codec_get_bmp_tag(codec->codec_id);
  205. put_bi(bc, tmp);
  206. nom = codec->frame_rate;
  207. denom = codec->frame_rate_base;
  208. }
  209. else if (codec->codec_type == CODEC_TYPE_AUDIO)
  210. {
  211. int tmp = codec_get_wav_tag(codec->codec_id);
  212. put_bi(bc, tmp);
  213. nom = codec->sample_rate/8;
  214. denom = 8;
  215. }
  216. put_v(bc, codec->bit_rate);
  217. put_v(bc, 0); /* no language code */
  218. put_v(bc, nom);
  219. put_v(bc, denom);
  220. put_v(bc, 0); /* msb timestamp_shift */
  221. put_v(bc, 0); /* shuffle type */
  222. put_byte(bc, 0); /* flags: 0x1 - fixed_fps, 0x2 - index_present */
  223. put_v(bc, 0); /* no codec specific headers */
  224. switch(codec->codec_type)
  225. {
  226. case CODEC_TYPE_AUDIO:
  227. put_v(bc, (codec->sample_rate * denom) / nom);
  228. put_v(bc, codec->channels);
  229. put_be32(bc, 0); /* FIXME: checksum */
  230. break;
  231. case CODEC_TYPE_VIDEO:
  232. put_v(bc, codec->width);
  233. put_v(bc, codec->height);
  234. put_v(bc, 0); /* aspected w */
  235. put_v(bc, 0); /* aspected h */
  236. put_v(bc, 0); /* csp type -- unknown */
  237. put_be32(bc, 0); /* FIXME: checksum */
  238. break;
  239. default:
  240. break;
  241. }
  242. update_packetheader(nut, bc, 0);
  243. }
  244. #if 0
  245. /* info header */
  246. put_be64(bc, INFO_STARTCODE);
  247. put_packetheader(nut, bc, 16+strlen(s->author)+strlen(s->title)+
  248. strlen(s->comment)+strlen(s->copyright));
  249. if (s->author[0])
  250. {
  251. put_v(bc, 5); /* type */
  252. put_b(bc, s->author, strlen(s->author));
  253. }
  254. if (s->title[0])
  255. {
  256. put_v(bc, 6); /* type */
  257. put_b(bc, s->title, strlen(s->title));
  258. }
  259. if (s->comment[0])
  260. {
  261. put_v(bc, 7); /* type */
  262. put_b(bc, s->comment, strlen(s->comment));
  263. }
  264. if (s->copyright[0])
  265. {
  266. put_v(bc, 8); /* type */
  267. put_b(bc, s->copyright, strlen(s->copyright));
  268. }
  269. /* encoder */
  270. put_v(bc, 9); /* type */
  271. put_b(bc, LIBAVFORMAT_IDENT "\0", strlen(LIBAVFORMAT_IDENT));
  272. put_v(bc, 0); /* eof info */
  273. put_be32(bc, 0); /* FIXME: checksum */
  274. update_packetheader(nut, bc, 0);
  275. #endif
  276. put_flush_packet(bc);
  277. return 0;
  278. }
  279. static int nut_write_packet(AVFormatContext *s, int stream_index,
  280. const uint8_t *buf, int size, int64_t pts)
  281. {
  282. NUTContext *nut = s->priv_data;
  283. ByteIOContext *bc = &s->pb;
  284. int key_frame = 0;
  285. int flags;
  286. AVCodecContext *enc;
  287. if (stream_index > s->nb_streams)
  288. return 1;
  289. enc = &s->streams[stream_index]->codec;
  290. key_frame = enc->coded_frame->key_frame;
  291. if (key_frame)
  292. put_be64(bc, KEYFRAME_STARTCODE);
  293. flags=0;
  294. flags<<=2; flags|=1; //priority
  295. flags<<=1; flags|=0; //checksum
  296. flags<<=1; flags|=0; //msb_timestamp_flag
  297. flags<<=2; flags|=1; //subpacket_type
  298. flags<<=1; flags|=0; //reserved
  299. put_byte(bc, flags);
  300. put_packetheader(nut, bc, size+20);
  301. put_v(bc, stream_index);
  302. put_s(bc, pts); /* lsb_timestamp */
  303. update_packetheader(nut, bc, size);
  304. put_buffer(bc, buf, size);
  305. put_flush_packet(bc);
  306. return 0;
  307. }
  308. static int nut_write_trailer(AVFormatContext *s)
  309. {
  310. ByteIOContext *bc = &s->pb;
  311. #if 0
  312. int i;
  313. /* WRITE INDEX */
  314. for (i = 0; s->nb_streams; i++)
  315. {
  316. put_be64(bc, INDEX_STARTCODE);
  317. put_packetheader(nut, bc, 64);
  318. put_v(bc, s->streams[i]->id);
  319. put_v(bc, ...);
  320. put_be32(bc, 0); /* FIXME: checksum */
  321. update_packetheader(nut, bc, 0);
  322. }
  323. #endif
  324. put_flush_packet(bc);
  325. return 0;
  326. }
  327. static int nut_probe(AVProbeData *p)
  328. {
  329. int i;
  330. uint64_t code;
  331. code = 0xff;
  332. for (i = 0; i < p->buf_size; i++) {
  333. int c = p->buf[i];
  334. code = (code << 8) | c;
  335. if (code == MAIN_STARTCODE)
  336. return AVPROBE_SCORE_MAX;
  337. }
  338. return 0;
  339. }
  340. static int nut_read_header(AVFormatContext *s, AVFormatParameters *ap)
  341. {
  342. NUTContext *nut = s->priv_data;
  343. ByteIOContext *bc = &s->pb;
  344. uint64_t tmp;
  345. int cur_stream, nb_streams;
  346. /* main header */
  347. tmp = get_be64(bc);
  348. if (tmp != MAIN_STARTCODE)
  349. fprintf(stderr, "damaged? startcode!=1 (%Ld)\n", tmp);
  350. get_packetheader(nut, bc);
  351. tmp = get_v(bc);
  352. if (tmp != 0)
  353. fprintf(stderr, "bad version (%Ld)\n", tmp);
  354. nb_streams = get_v(bc);
  355. get_be32(bc); /* checkusm */
  356. s->bit_rate = 0;
  357. /* stream header */
  358. for (cur_stream = 0; cur_stream < nb_streams; cur_stream++)
  359. {
  360. int class, nom, denom;
  361. AVStream *st;
  362. tmp = get_be64(bc);
  363. if (tmp != STREAM_STARTCODE)
  364. fprintf(stderr, "damaged? startcode!=1 (%Ld)\n", tmp);
  365. get_packetheader(nut, bc);
  366. st = av_new_stream(s, get_v(bc));
  367. if (!st)
  368. return AVERROR_NOMEM;
  369. class = get_v(bc);
  370. tmp = get_bi(bc);
  371. switch(class)
  372. {
  373. case 0:
  374. st->codec.codec_type = CODEC_TYPE_VIDEO;
  375. st->codec.codec_id = codec_get_bmp_id(tmp);
  376. if (st->codec.codec_id == CODEC_ID_NONE)
  377. fprintf(stderr, "Unknown codec?!\n");
  378. break;
  379. case 32:
  380. st->codec.codec_type = CODEC_TYPE_AUDIO;
  381. st->codec.codec_id = codec_get_wav_id(tmp);
  382. if (st->codec.codec_id == CODEC_ID_NONE)
  383. fprintf(stderr, "Unknown codec?!\n");
  384. break;
  385. default:
  386. fprintf(stderr, "Unknown stream class (%d)\n", class);
  387. return -1;
  388. }
  389. s->bit_rate += get_v(bc);
  390. get_b(bc, NULL, 0); /* language code */
  391. nom = get_v(bc);
  392. denom = get_v(bc);
  393. get_v(bc); /* FIXME: msb timestamp base */
  394. get_v(bc); /* shuffle type */
  395. get_byte(bc); /* flags */
  396. get_v(bc); /* FIXME: codec specific data headers */
  397. if (class == 0) /* VIDEO */
  398. {
  399. st->codec.width = get_v(bc);
  400. st->codec.height = get_v(bc);
  401. get_v(bc); /* aspected w */
  402. get_v(bc); /* aspected h */
  403. get_v(bc); /* csp type */
  404. get_be32(bc); /* checksum */
  405. st->codec.frame_rate = nom;
  406. st->codec.frame_rate_base = denom;
  407. }
  408. if (class == 32) /* AUDIO */
  409. {
  410. st->codec.sample_rate = (get_v(bc) * nom) / denom;
  411. st->codec.channels = get_v(bc);
  412. get_be32(bc); /* checksum */
  413. }
  414. }
  415. return 0;
  416. }
  417. static int nut_read_packet(AVFormatContext *s, AVPacket *pkt)
  418. {
  419. NUTContext *nut = s->priv_data;
  420. ByteIOContext *bc = &s->pb;
  421. int id, timestamp, size;
  422. int key_frame = 0;
  423. uint64_t tmp;
  424. if (url_feof(bc))
  425. return -1;
  426. tmp = get_byte(bc);
  427. if (tmp & 0x80) /* zero bit set? */
  428. {
  429. tmp<<=8 ; tmp |= get_byte(bc);
  430. tmp<<=16; tmp |= get_be16(bc);
  431. tmp<<=32; tmp |= get_be32(bc);
  432. if (tmp == KEYFRAME_STARTCODE)
  433. {
  434. key_frame = 1;
  435. tmp = get_byte(bc); /* flags */
  436. }
  437. else
  438. fprintf(stderr, "error in zero bit / startcode %LX\n", tmp);
  439. }
  440. get_packetheader(nut, bc);
  441. #if 0
  442. if (((tmp & 0x60)>>5) > 3) /* priority <= 3 */
  443. fprintf(stderr, "sanity check failed!\n");
  444. #endif
  445. id = get_v(bc);
  446. timestamp = get_s(bc);
  447. size = (nut->curr_frame_size - (url_ftell(bc)-nut->curr_frame_start));
  448. dprintf("flags: 0x%Lx, timestamp: %d, packet size: %d\n", tmp, timestamp, size);
  449. if (size < 0)
  450. return -1;
  451. av_new_packet(pkt, size);
  452. get_buffer(bc, pkt->data, size);
  453. pkt->stream_index = id;
  454. if (key_frame)
  455. pkt->flags |= PKT_FLAG_KEY;
  456. pkt->pts = timestamp;
  457. return 0;
  458. }
  459. static AVInputFormat nut_iformat = {
  460. "nut",
  461. "nut format",
  462. sizeof(NUTContext),
  463. nut_probe,
  464. nut_read_header,
  465. nut_read_packet,
  466. // nut_read_close,
  467. // nut_read_seek,
  468. .extensions = "nut",
  469. };
  470. static AVOutputFormat nut_oformat = {
  471. "nut",
  472. "nut format",
  473. "video/x-nut",
  474. "nut",
  475. sizeof(NUTContext),
  476. #ifdef CONFIG_VORBIS
  477. CODEC_ID_VORBIS,
  478. #elif defined(CONFIG_MP3LAME)
  479. CODEC_ID_MP3,
  480. #else
  481. CODEC_ID_MP2, /* AC3 needs liba52 decoder */
  482. #endif
  483. CODEC_ID_MPEG4,
  484. nut_write_header,
  485. nut_write_packet,
  486. nut_write_trailer,
  487. };
  488. int nut_init(void)
  489. {
  490. av_register_input_format(&nut_iformat);
  491. av_register_output_format(&nut_oformat);
  492. return 0;
  493. }